Skip to content

Commit e2a6821

Browse files
authored
Added tasks 388, 389, 390, 391.
1 parent 8ba14bd commit e2a6821

File tree

13 files changed

+506
-0
lines changed

13 files changed

+506
-0
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.6'
343343

344344
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
345345
|-|-|-|-|-|-
346+
| 0389 |[Find the Difference](src/main/kotlin/g0301_0400/s0389_find_the_difference/Solution.kt)| Easy | String, Hash_Table, Sorting, Bit_Manipulation | 256 | 64.81
346347

347348
#### Day 9 String
348349

@@ -1031,6 +1032,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.6'
10311032
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
10321033
|-|-|-|-|-|-
10331034
| 0191 |[Number of 1 Bits](src.save/main/kotlin/g0101_0200/s0191_number_of_1_bits/Solution.kt)| Easy | Top_Interview_Questions, Bit_Manipulation | 237 | 68.44
1035+
| 0389 |[Find the Difference](src/main/kotlin/g0301_0400/s0389_find_the_difference/Solution.kt)| Easy | String, Hash_Table, Sorting, Bit_Manipulation | 256 | 64.81
10341036
| 0190 |[Reverse Bits](src.save/main/kotlin/g0101_0200/s0190_reverse_bits/Solution.kt)| Easy | Top_Interview_Questions, Bit_Manipulation, Divide_and_Conquer | 198 | 81.82
10351037
| 0338 |[Counting Bits](src/main/kotlin/g0301_0400/s0338_counting_bits/Solution.kt)| Easy | Top_100_Liked_Questions, Dynamic_Programming, Bit_Manipulation | 186 | 99.26
10361038
| 0371 |[Sum of Two Integers](src/main/kotlin/g0301_0400/s0371_sum_of_two_integers/Solution.kt)| Medium | Top_Interview_Questions, Math, Bit_Manipulation | 129 | 95.45
@@ -1614,6 +1616,10 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.6'
16141616
| 0437 |[Path Sum III](src/main/kotlin/g0401_0500/s0437_path_sum_iii/Solution.kt)| Medium | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree, Level_2_Day_7_Tree | 403 | 54.12
16151617
| 0416 |[Partition Equal Subset Sum](src/main/kotlin/g0401_0500/s0416_partition_equal_subset_sum/Solution.kt)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Level_2_Day_13_Dynamic_Programming | 509 | 57.56
16161618
| 0394 |[Decode String](src/main/kotlin/g0301_0400/s0394_decode_string/Solution.kt)| Medium | Top_100_Liked_Questions, String, Stack, Recursion, Level_1_Day_14_Stack, Udemy_Strings | 224 | 64.86
1619+
| 0391 |[Perfect Rectangle](src/main/kotlin/g0301_0400/s0391_perfect_rectangle/Solution.kt)| Hard | Array, Line_Sweep | 897 | 100.00
1620+
| 0390 |[Elimination Game](src/main/kotlin/g0301_0400/s0390_elimination_game/Solution.kt)| Medium | Math, Recursion | 319 | 55.56
1621+
| 0389 |[Find the Difference](src/main/kotlin/g0301_0400/s0389_find_the_difference/Solution.kt)| Easy | String, Hash_Table, Sorting, Bit_Manipulation, Programming_Skills_I_Day_8_String, Udemy_Bit_Manipulation | 256 | 64.81
1622+
| 0388 |[Longest Absolute File Path](src/main/kotlin/g0301_0400/s0388_longest_absolute_file_path/Solution.kt)| Medium | String, Depth_First_Search, Stack | 150 | 100.00
16171623
| 0387 |[First Unique Character in a String](src/main/kotlin/g0301_0400/s0387_first_unique_character_in_a_string/Solution.kt)| Easy | Top_Interview_Questions, String, Hash_Table, Counting, Queue, Data_Structure_I_Day_6_String | 369 | 82.68
16181624
| 0386 |[Lexicographical Numbers](src/main/kotlin/g0301_0400/s0386_lexicographical_numbers/Solution.kt)| Medium | Depth_First_Search, Trie | 463 | 83.33
16191625
| 0385 |[Mini Parser](src/main/kotlin/g0301_0400/s0385_mini_parser/Solution.kt)| Medium | String, Depth_First_Search, Stack | 210 | 100.00
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package g0301_0400.s0388_longest_absolute_file_path
2+
3+
// #Medium #String #Depth_First_Search #Stack
4+
// #2022_11_24_Time_150_ms_(100.00%)_Space_33.6_MB_(100.00%)
5+
6+
import java.util.ArrayDeque
7+
import java.util.Deque
8+
9+
class Solution {
10+
fun lengthLongestPath(input: String): Int {
11+
val stack: Deque<Int> = ArrayDeque()
12+
var longestLen = 0
13+
var currDirLen = 0
14+
var i = 0
15+
var currLevel: Int
16+
var nextLevel = 0
17+
var isFile = false
18+
val period = '.'
19+
val space = ' '
20+
while (i < input.length) {
21+
currLevel = nextLevel
22+
var currStrLen = 0
23+
while (i < input.length &&
24+
(Character.isLetterOrDigit(input[i]) || period == input[i] || space == input[i])
25+
) {
26+
if (period == input[i]) {
27+
isFile = true
28+
}
29+
i++
30+
currStrLen++
31+
}
32+
if (isFile) {
33+
longestLen = Math.max(longestLen, currDirLen + currStrLen)
34+
} else {
35+
currDirLen += currStrLen + 1
36+
stack.push(currStrLen + 1)
37+
}
38+
nextLevel = 0
39+
// increment one to let it pass "\n" and start from "\t"
40+
i = i + 1
41+
while (i < input.length - 1 && input[i] == '\t') {
42+
nextLevel++
43+
i = i + 1
44+
}
45+
if (nextLevel < currLevel) {
46+
var j = 0
47+
if (isFile) {
48+
while (!stack.isEmpty() && j < currLevel - nextLevel) {
49+
currDirLen -= stack.pop()
50+
j++
51+
}
52+
} else {
53+
while (!stack.isEmpty() && j <= currLevel - nextLevel) {
54+
currDirLen -= stack.pop()
55+
j++
56+
}
57+
}
58+
} else if (nextLevel == currLevel && !isFile && !stack.isEmpty()) {
59+
currDirLen -= stack.pop()
60+
}
61+
if (nextLevel == 0) {
62+
currDirLen = 0
63+
stack.clear()
64+
}
65+
isFile = false
66+
}
67+
return longestLen
68+
}
69+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
388\. Longest Absolute File Path
2+
3+
Medium
4+
5+
Suppose we have a file system that stores both files and directories. An example of one system is represented in the following picture:
6+
7+
![](https://assets.leetcode.com/uploads/2020/08/28/mdir.jpg)
8+
9+
Here, we have `dir` as the only directory in the root. `dir` contains two subdirectories, `subdir1` and `subdir2`. `subdir1` contains a file `file1.ext` and subdirectory `subsubdir1`. `subdir2` contains a subdirectory `subsubdir2`, which contains a file `file2.ext`.
10+
11+
In text form, it looks like this (with ⟶ representing the tab character):
12+
13+
dir
14+
⟶ subdir1
15+
⟶ ⟶ file1.ext
16+
⟶ ⟶ subsubdir1
17+
⟶ subdir2
18+
⟶ ⟶ subsubdir2
19+
⟶ ⟶ ⟶ file2.ext
20+
21+
If we were to write this representation in code, it will look like this: `"dir
22+
\tsubdir1
23+
\t\tfile1.ext
24+
\t\tsubsubdir1
25+
\tsubdir2
26+
\t\tsubsubdir2
27+
\t\t\tfile2.ext"`. Note that the `'
28+
'` and `'\t'` are the new-line and tab characters.
29+
30+
Every file and directory has a unique **absolute path** in the file system, which is the order of directories that must be opened to reach the file/directory itself, all concatenated by `'/'s`. Using the above example, the **absolute path** to `file2.ext` is `"dir/subdir2/subsubdir2/file2.ext"`. Each directory name consists of letters, digits, and/or spaces. Each file name is of the form `name.extension`, where `name` and `extension` consist of letters, digits, and/or spaces.
31+
32+
Given a string `input` representing the file system in the explained format, return _the length of the **longest absolute path** to a **file** in the abstracted file system_. If there is no file in the system, return `0`.
33+
34+
**Example 1:**
35+
36+
![](https://assets.leetcode.com/uploads/2020/08/28/dir1.jpg)
37+
38+
**Input:** input = "dir\n\tsubdir1\n\tsubdir2\n\t\tfile.ext"
39+
40+
**Output:** 20
41+
42+
**Explanation:** We have only one file, and the absolute path is "dir/subdir2/file.ext" of length 20.
43+
44+
**Example 2:**
45+
46+
![](https://assets.leetcode.com/uploads/2020/08/28/dir2.jpg)
47+
48+
**Input:** input = input = "dir\n\tsubdir1\n\t\tfile1.ext\n\t\tsubsubdir1\n\tsubdir2\n\t\tsubsubdir2\n\t\t\tfile2.ext"
49+
50+
**Output:** 32
51+
52+
**Explanation:** We have two files: "dir/subdir1/file1.ext" of length 21 "dir/subdir2/subsubdir2/file2.ext" of length 32. We return 32 since it is the longest absolute path to a file.
53+
54+
**Example 3:**
55+
56+
**Input:** input = "a"
57+
58+
**Output:** 0
59+
60+
**Explanation:** We do not have any files, just a single directory named "a".
61+
62+
**Example 4:**
63+
64+
**Input:** input = "file1.txt
65+
file2.txt
66+
longfile.txt"
67+
68+
**Output:** 12
69+
70+
**Explanation:** There are 3 files at the root directory. Since the absolute path for anything at the root directory is just the name itself, the answer is "longfile.txt" with length 12.
71+
72+
**Constraints:**
73+
74+
* <code>1 <= input.length <= 10<sup>4</sup></code>
75+
* `input` may contain lowercase or uppercase English letters, a new line character `'
76+
'`, a tab character `'\t'`, a dot `'.'`, a space `' '`, and digits.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package g0301_0400.s0389_find_the_difference
2+
3+
// #Easy #String #Hash_Table #Sorting #Bit_Manipulation #Programming_Skills_I_Day_8_String
4+
// #Udemy_Bit_Manipulation #2022_11_24_Time_256_ms_(64.81%)_Space_34.8_MB_(100.00%)
5+
6+
class Solution {
7+
fun findTheDifference(s: String, t: String): Char {
8+
var c = 0.toChar()
9+
for (cs in s.toCharArray()) {
10+
c = (c.code xor cs.code).toChar()
11+
}
12+
for (ct in t.toCharArray()) {
13+
c = (c.code xor ct.code).toChar()
14+
}
15+
return c
16+
}
17+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
389\. Find the Difference
2+
3+
Easy
4+
5+
You are given two strings `s` and `t`.
6+
7+
String `t` is generated by random shuffling string `s` and then add one more letter at a random position.
8+
9+
Return the letter that was added to `t`.
10+
11+
**Example 1:**
12+
13+
**Input:** s = "abcd", t = "abcde"
14+
15+
**Output:** "e"
16+
17+
**Explanation:** 'e' is the letter that was added.
18+
19+
**Example 2:**
20+
21+
**Input:** s = "", t = "y"
22+
23+
**Output:** "y"
24+
25+
**Constraints:**
26+
27+
* `0 <= s.length <= 1000`
28+
* `t.length == s.length + 1`
29+
* `s` and `t` consist of lowercase English letters.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package g0301_0400.s0390_elimination_game
2+
3+
// #Medium #Math #Recursion #2022_11_24_Time_319_ms_(55.56%)_Space_35.5_MB_(33.33%)
4+
5+
class Solution {
6+
fun lastRemaining(n: Int): Int {
7+
return if (n == 1) 1 else 2 * (n / 2 - lastRemaining(n / 2) + 1)
8+
}
9+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
390\. Elimination Game
2+
3+
Medium
4+
5+
You have a list `arr` of all integers in the range `[1, n]` sorted in a strictly increasing order. Apply the following algorithm on `arr`:
6+
7+
* Starting from left to right, remove the first number and every other number afterward until you reach the end of the list.
8+
* Repeat the previous step again, but this time from right to left, remove the rightmost number and every other number from the remaining numbers.
9+
* Keep repeating the steps again, alternating left to right and right to left, until a single number remains.
10+
11+
Given the integer `n`, return _the last number that remains in_ `arr`.
12+
13+
**Example 1:**
14+
15+
**Input:** n = 9
16+
17+
**Output:** 6
18+
19+
**Explanation:**
20+
21+
arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]
22+
arr = [2, 4, 6, 8]
23+
arr = [2, 6]
24+
arr = [6]
25+
26+
**Example 2:**
27+
28+
**Input:** n = 1
29+
30+
**Output:** 1
31+
32+
**Constraints:**
33+
34+
* <code>1 <= n <= 10<sup>9</sup></code>
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package g0301_0400.s0391_perfect_rectangle
2+
3+
// #Hard #Array #Line_Sweep #2022_11_24_Time_897_ms_(100.00%)_Space_89.8_MB_(100.00%)
4+
5+
import java.util.Objects
6+
7+
class Solution {
8+
fun isRectangleCover(rectangles: Array<IntArray>): Boolean {
9+
val container: MutableSet<Point> = HashSet()
10+
// add each rectangle area to totalArea
11+
var totalArea = 0
12+
// A rectangle has four points, if a point appears twice, it will be deleted it from the set
13+
for (rectangle in rectangles) {
14+
totalArea += (rectangle[2] - rectangle[0]) * (rectangle[3] - rectangle[1])
15+
val p1 = Point(rectangle[0], rectangle[1])
16+
val p2 = Point(rectangle[2], rectangle[1])
17+
val p3 = Point(rectangle[2], rectangle[3])
18+
val p4 = Point(rectangle[0], rectangle[3])
19+
if (container.contains(p1)) {
20+
container.remove(p1)
21+
} else {
22+
container.add(p1)
23+
}
24+
if (container.contains(p2)) {
25+
container.remove(p2)
26+
} else {
27+
container.add(p2)
28+
}
29+
if (container.contains(p3)) {
30+
container.remove(p3)
31+
} else {
32+
container.add(p3)
33+
}
34+
if (container.contains(p4)) {
35+
container.remove(p4)
36+
} else {
37+
container.add(p4)
38+
}
39+
}
40+
// A perfect rectangle must has four points
41+
if (container.size != 4) {
42+
return false
43+
}
44+
45+
// these four points represent the last perfect rectangle, check this rectangle area to the
46+
// totalArea
47+
var minX = Int.MAX_VALUE
48+
var maxX = Int.MIN_VALUE
49+
var minY = Int.MAX_VALUE
50+
var maxY = Int.MIN_VALUE
51+
for (p in container) {
52+
minX = Math.min(minX, p.x)
53+
maxX = Math.max(maxX, p.x)
54+
minY = Math.min(minY, p.y)
55+
maxY = Math.max(maxY, p.y)
56+
}
57+
return totalArea == (maxX - minX) * (maxY - minY)
58+
}
59+
60+
private class Point(val x: Int, val y: Int) {
61+
override fun equals(o: Any?): Boolean {
62+
if (this === o) {
63+
return true
64+
}
65+
if (o == null || javaClass != o.javaClass) {
66+
return false
67+
}
68+
val point = o as Point
69+
return x == point.x && y == point.y
70+
}
71+
72+
override fun hashCode(): Int {
73+
return Objects.hash(x, y)
74+
}
75+
}
76+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
391\. Perfect Rectangle
2+
3+
Hard
4+
5+
Given an array `rectangles` where <code>rectangles[i] = [x<sub>i</sub>, y<sub>i</sub>, a<sub>i</sub>, b<sub>i</sub>]</code> represents an axis-aligned rectangle. The bottom-left point of the rectangle is <code>(x<sub>i</sub>, y<sub>i</sub>)</code> and the top-right point of it is <code>(a<sub>i</sub>, b<sub>i</sub>)</code>.
6+
7+
Return `true` _if all the rectangles together form an exact cover of a rectangular region_.
8+
9+
**Example 1:**
10+
11+
![](https://assets.leetcode.com/uploads/2021/03/27/perectrec1-plane.jpg)
12+
13+
**Input:** rectangles = [[1,1,3,3],[3,1,4,2],[3,2,4,4],[1,3,2,4],[2,3,3,4]]
14+
15+
**Output:** true
16+
17+
**Explanation:** All 5 rectangles together form an exact cover of a rectangular region.
18+
19+
**Example 2:**
20+
21+
![](https://assets.leetcode.com/uploads/2021/03/27/perfectrec2-plane.jpg)
22+
23+
**Input:** rectangles = [[1,1,2,3],[1,3,2,4],[3,1,4,2],[3,2,4,4]]
24+
25+
**Output:** false
26+
27+
**Explanation:** Because there is a gap between the two rectangular regions.
28+
29+
**Example 3:**
30+
31+
![](https://assets.leetcode.com/uploads/2021/03/27/perfecrrec4-plane.jpg)
32+
33+
**Input:** rectangles = [[1,1,3,3],[3,1,4,2],[1,3,2,4],[2,2,4,4]]
34+
35+
**Output:** false
36+
37+
**Explanation:** Because two of the rectangles overlap with each other.
38+
39+
**Constraints:**
40+
41+
* <code>1 <= rectangles.length <= 2 * 10<sup>4</sup></code>
42+
* `rectangles[i].length == 4`
43+
* <code>-10<sup>5</sup> <= x<sub>i</sub>, y<sub>i</sub>, a<sub>i</sub>, b<sub>i</sub> <= 10<sup>5</sup></code>

0 commit comments

Comments
 (0)