Skip to content

Commit bbe5af0

Browse files
authored
Added tasks 69, 71, 77, 80.
1 parent f8edc55 commit bbe5af0

File tree

13 files changed

+364
-1
lines changed

13 files changed

+364
-1
lines changed

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.4'
202202

203203
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
204204
|-|-|-|-|-|-
205+
| 0069 |[Sqrt(x)](src/main/kotlin/g0001_0100/s0069_sqrtx/Solution.kt)| Easy | Top_Interview_Questions, Math, Binary_Search | 153 | 95.75
205206

206207
#### Day 5
207208

@@ -1038,6 +1039,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.4'
10381039
| 0283 |[Move Zeroes](src/main/kotlin/g0201_0300/s0283_move_zeroes/Solution.kt)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Two_Pointers | 516 | 79.07
10391040
| 0001 |[Two Sum](src/main/kotlin/g0001_0100/s0001_two_sum/Solution.kt)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table | 234 | 92.75
10401041
| 0058 |[Length of Last Word](src/main/kotlin/g0001_0100/s0058_length_of_last_word/Solution.kt)| Easy | String | 243 | 63.33
1042+
| 0080 |[Remove Duplicates from Sorted Array II](src/main/kotlin/g0001_0100/s0080_remove_duplicates_from_sorted_array_ii/Solution.kt)| Medium | Array, Two_Pointers | 357 | 44.78
10411043
| 0189 |[Rotate Array](src/main/kotlin/g0101_0200/s0189_rotate_array/Solution.kt)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Math, Two_Pointers | 483 | 86.95
10421044
| 0055 |[Jump Game](src/main/kotlin/g0001_0100/s0055_jump_game/Solution.kt)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Greedy | 670 | 66.67
10431045
| 0075 |[Sort Colors](src/main/kotlin/g0001_0100/s0075_sort_colors/Solution.kt)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Two_Pointers | 198 | 85.66
@@ -1438,6 +1440,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.4'
14381440

14391441
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
14401442
|-|-|-|-|-|-
1443+
| 0077 |[Combinations](src/main/kotlin/g0001_0100/s0077_combinations/Solution.kt)| Medium | Backtracking | 244 | 100.00
14411444
| 0046 |[Permutations](src/main/kotlin/g0001_0100/s0046_permutations/Solution.kt)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Backtracking | 186 | 100.00
14421445

14431446
#### Day 12 Dynamic Programming
@@ -1523,15 +1526,19 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.4'
15231526
| 0096 |[Unique Binary Search Trees](src/main/kotlin/g0001_0100/s0096_unique_binary_search_trees/Solution.kt)| Medium | Top_100_Liked_Questions, Dynamic_Programming, Math, Tree, Binary_Tree, Binary_Search_Tree, Dynamic_Programming_I_Day_11 | 237 | 26.76
15241527
| 0094 |[Largest Rectangle in Histogram](src/main/kotlin/g0001_0100/s0094_binary_tree_inorder_traversal/Solution.kt)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Tree, Binary_Tree, Stack, Data_Structure_I_Day_10_Tree, Udemy_Tree_Stack_Queue | 283 | 17.97
15251528
| 0084 |[Largest Rectangle in Histogram](src/main/kotlin/g0001_0100/s0084_largest_rectangle_in_histogram/Solution.kt)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Array, Stack, Monotonic_Stack | 525 | 100.00
1529+
| 0080 |[Remove Duplicates from Sorted Array II](src/main/kotlin/g0001_0100/s0080_remove_duplicates_from_sorted_array_ii/Solution.kt)| Medium | Array, Two_Pointers, Udemy_Arrays | 357 | 44.78
15261530
| 0079 |[Word Search](src/main/kotlin/g0001_0100/s0079_word_search/Solution.kt)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Matrix, Backtracking, Algorithm_II_Day_11_Recursion_Backtracking | 463 | 68.49
15271531
| 0078 |[Subsets](src/main/kotlin/g0001_0100/s0078_subsets/Solution.kt)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Bit_Manipulation, Backtracking, Algorithm_II_Day_9_Recursion_Backtracking, Udemy_Backtracking/Recursion | 353 | 30.14
1532+
| 0077 |[Combinations](src/main/kotlin/g0001_0100/s0077_combinations/Solution.kt)| Medium | Backtracking, Algorithm_I_Day_11_Recursion_Backtracking | 244 | 100.00
15281533
| 0076 |[Minimum Window Substring](src/main/kotlin/g0001_0100/s0076_minimum_window_substring/Solution.kt)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Sliding_Window, Level_2_Day_14_Sliding_Window/Two_Pointer | 346 | 85.20
15291534
| 0075 |[Sort Colors](src/main/kotlin/g0001_0100/s0075_sort_colors/Solution.kt)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Two_Pointers, Data_Structure_II_Day_2_Array, Udemy_Arrays | 198 | 85.66
15301535
| 0074 |[Search a 2D Matrix](src/main/kotlin/g0001_0100/s0074_search_a_2d_matrix/Solution.kt)| Medium | Top_100_Liked_Questions, Array, Binary_Search, Matrix, Data_Structure_I_Day_5_Array, Algorithm_II_Day_1_Binary_Search, Binary_Search_I_Day_8, Level_2_Day_8_Binary_Search, Udemy_2D_Arrays/Matrix | 290 | 40.17
15311536
| 0073 |[Set Matrix Zeroes](src/main/kotlin/g0001_0100/s0073_set_matrix_zeroes/Solution.kt)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Matrix, Udemy_2D_Arrays/Matrix | 255 | 100.00
15321537
| 0072 |[Edit Distance](src/main/kotlin/g0001_0100/s0072_edit_distance/Solution.kt)| Hard | Top_100_Liked_Questions, String, Dynamic_Programming, Algorithm_II_Day_18_Dynamic_Programming, Dynamic_Programming_I_Day_19, Udemy_Dynamic_Programming | 320 | 63.53
1538+
| 0071 |[Simplify Path](src/main/kotlin/g0001_0100/s0071_simplify_path/Solution.kt)| |||
15331539
| 0070 |[Climbing Stairs](src/main/kotlin/g0001_0100/s0070_climbing_stairs/Solution.kt)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Math, Memoization, Algorithm_I_Day_12_Dynamic_Programming, Dynamic_Programming_I_Day_2, Level_1_Day_10_Dynamic_Programming, Udemy_Dynamic_Programming | 127 | 97.06
1534-
| 0068 |[Add Binary](src/main/kotlin/g0001_0100/s0068_text_justification/Solution.kt)| Hard | Array, String, Simulation | 269 | 50.00
1540+
| 0069 |[Sqrt(x)](src/main/kotlin/g0001_0100/s0069_sqrtx/Solution.kt)| Easy | Top_Interview_Questions, Math, Binary_Search, Binary_Search_I_Day_4 | 153 | 95.75
1541+
| 0068 |[Text Justification](src/main/kotlin/g0001_0100/s0068_text_justification/Solution.kt)| Hard | Array, String, Simulation | 269 | 50.00
15351542
| 0067 |[Add Binary](src/main/kotlin/g0001_0100/s0067_add_binary/Solution.kt)| Easy | String, Math, Bit_Manipulation, Simulation, Programming_Skills_II_Day_5 | 327 | 32.67
15361543
| 0066 |[Plus One](src/main/kotlin/g0001_0100/s0066_plus_one/Solution.kt)| Easy | Top_Interview_Questions, Array, Math, Programming_Skills_II_Day_3, Udemy_Arrays | 303 | 35.18
15371544
| 0065 |[Valid Number](src/main/kotlin/g0001_0100/s0065_valid_number/Solution.kt)| Hard | String | 310 | 81.82
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package g0001_0100.s0069_sqrtx
2+
3+
// #Easy #Top_Interview_Questions #Math #Binary_Search #Binary_Search_I_Day_4
4+
// #2022_09_24_Time_153_ms_(95.75%)_Space_33.2_MB_(99.35%)
5+
6+
class Solution {
7+
fun mySqrt(x: Int): Int {
8+
var start = 1
9+
var end = x / 2
10+
var sqrt = start + (end - start) / 2
11+
if (x == 0) {
12+
return 0
13+
}
14+
while (start <= end) {
15+
if (sqrt == x / sqrt) {
16+
return sqrt
17+
} else if (sqrt > x / sqrt) {
18+
end = sqrt - 1
19+
} else if (sqrt < x / sqrt) {
20+
start = sqrt + 1
21+
}
22+
sqrt = start + (end - start) / 2
23+
}
24+
return if (sqrt > x / sqrt) {
25+
sqrt - 1
26+
} else {
27+
sqrt
28+
}
29+
}
30+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
69\. Sqrt(x)
2+
3+
Easy
4+
5+
Given a non-negative integer `x`, compute and return _the square root of_ `x`.
6+
7+
Since the return type is an integer, the decimal digits are **truncated**, and only **the integer part** of the result is returned.
8+
9+
**Note:** You are not allowed to use any built-in exponent function or operator, such as `pow(x, 0.5)` or <code>x ** 0.5</code>.
10+
11+
**Example 1:**
12+
13+
**Input:** x = 4
14+
15+
**Output:** 2
16+
17+
**Example 2:**
18+
19+
**Input:** x = 8
20+
21+
**Output:** 2
22+
23+
**Explanation:** The square root of 8 is 2.82842..., and since the decimal part is truncated, 2 is returned.
24+
25+
**Constraints:**
26+
27+
* <code>0 <= x <= 2<sup>31</sup> - 1</code>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package g0001_0100.s0071_simplify_path
2+
3+
// #Medium #String #Stack #2022_09_24_Time_318_ms_(64.44%)_Space_37.8_MB_(77.78%)
4+
5+
import java.util.ArrayDeque
6+
import java.util.Deque
7+
8+
class Solution {
9+
fun simplifyPath(path: String): String {
10+
val stk: Deque<String> = ArrayDeque()
11+
var start = 0
12+
while (start < path.length) {
13+
while (start < path.length && path[start] == '/') {
14+
start++
15+
}
16+
var end = start
17+
while (end < path.length && path[end] != '/') {
18+
end++
19+
}
20+
val s = path.substring(start, end)
21+
if (s == "..") {
22+
if (!stk.isEmpty()) {
23+
stk.pop()
24+
}
25+
} else if (s != "." && s != "") {
26+
stk.push(s)
27+
}
28+
start = end + 1
29+
}
30+
val ans = StringBuilder()
31+
while (!stk.isEmpty()) {
32+
ans.insert(0, stk.pop())
33+
ans.insert(0, "/")
34+
}
35+
return if (ans.length > 0) ans.toString() else "/"
36+
}
37+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
71\. Simplify Path
2+
3+
Medium
4+
5+
Given a string `path`, which is an **absolute path** (starting with a slash `'/'`) to a file or directory in a Unix-style file system, convert it to the simplified **canonical path**.
6+
7+
In a Unix-style file system, a period `'.'` refers to the current directory, a double period `'..'` refers to the directory up a level, and any multiple consecutive slashes (i.e. `'//'`) are treated as a single slash `'/'`. For this problem, any other format of periods such as `'...'` are treated as file/directory names.
8+
9+
The **canonical path** should have the following format:
10+
11+
* The path starts with a single slash `'/'`.
12+
* Any two directories are separated by a single slash `'/'`.
13+
* The path does not end with a trailing `'/'`.
14+
* The path only contains the directories on the path from the root directory to the target file or directory (i.e., no period `'.'` or double period `'..'`)
15+
16+
Return _the simplified **canonical path**_.
17+
18+
**Example 1:**
19+
20+
**Input:** path = "/home/"
21+
22+
**Output:** "/home"
23+
24+
**Explanation:** Note that there is no trailing slash after the last directory name.
25+
26+
**Example 2:**
27+
28+
**Input:** path = "/../"
29+
30+
**Output:** "/"
31+
32+
**Explanation:** Going one level up from the root directory is a no-op, as the root level is the highest level you can go.
33+
34+
**Example 3:**
35+
36+
**Input:** path = "/home//foo/"
37+
38+
**Output:** "/home/foo"
39+
40+
**Explanation:** In the canonical path, multiple consecutive slashes are replaced by a single one.
41+
42+
**Constraints:**
43+
44+
* `1 <= path.length <= 3000`
45+
* `path` consists of English letters, digits, period `'.'`, slash `'/'` or `'_'`.
46+
* `path` is a valid absolute Unix path.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package g0001_0100.s0077_combinations
2+
3+
// #Medium #Backtracking #Algorithm_I_Day_11_Recursion_Backtracking
4+
// #2022_09_24_Time_244_ms_(100.00%)_Space_40.5_MB_(99.00%)
5+
6+
import java.util.Stack
7+
8+
class Solution {
9+
fun combine(n: Int, k: Int): List<List<Int>> {
10+
val ans: MutableList<List<Int>> = ArrayList()
11+
// Boundary case
12+
if (n > 20 || k < 1 || k > n) {
13+
return ans
14+
}
15+
backtrack(ans, n, k, 1, Stack())
16+
return ans
17+
}
18+
19+
private fun backtrack(ans: MutableList<List<Int>>, n: Int, k: Int, s: Int, stack: Stack<Int>) {
20+
// Base case
21+
// If k becomes 0
22+
if (k == 0) {
23+
ans.add(ArrayList(stack))
24+
return
25+
}
26+
// Start with s till n-k+1
27+
for (i in s..n - k + 1) {
28+
stack.push(i)
29+
// Update start for recursion and decrease k by 1
30+
backtrack(ans, n, k - 1, i + 1, stack)
31+
stack.pop()
32+
}
33+
}
34+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
77\. Combinations
2+
3+
Medium
4+
5+
Given two integers `n` and `k`, return _all possible combinations of_ `k` _numbers chosen from the range_ `[1, n]`.
6+
7+
You may return the answer in **any order**.
8+
9+
**Example 1:**
10+
11+
**Input:** n = 4, k = 2
12+
13+
**Output:** [[1,2],[1,3],[1,4],[2,3],[2,4],[3,4]]
14+
15+
**Explanation:** There are 4 choose 2 = 6 total combinations. Note that combinations are unordered, i.e., [1,2] and [2,1] are considered to be the same combination.
16+
17+
**Example 2:**
18+
19+
**Input:** n = 1, k = 1
20+
21+
**Output:** [[1]]
22+
23+
**Explanation:** There is 1 choose 1 = 1 total combination.
24+
25+
**Constraints:**
26+
27+
* `1 <= n <= 20`
28+
* `1 <= k <= n`
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package g0001_0100.s0080_remove_duplicates_from_sorted_array_ii
2+
3+
// #Medium #Array #Two_Pointers #Udemy_Arrays
4+
// #2022_09_24_Time_357_ms_(44.78%)_Space_40.7_MB_(25.37%)
5+
6+
class Solution {
7+
fun removeDuplicates(nums: IntArray): Int {
8+
var i = 0
9+
var k = 0
10+
var count = 0
11+
while (i < nums.size - 1) {
12+
++count
13+
if (count <= 2) {
14+
nums[k++] = nums[i]
15+
}
16+
if (nums[i] != nums[i + 1]) {
17+
count = 0
18+
i++
19+
continue
20+
}
21+
i++
22+
}
23+
++count
24+
if (count <= 2) {
25+
nums[k++] = nums[i]
26+
}
27+
return k
28+
}
29+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
80\. Remove Duplicates from Sorted Array II
2+
3+
Medium
4+
5+
Given an integer array `nums` sorted in **non-decreasing order**, remove some duplicates [**in-place**](https://en.wikipedia.org/wiki/In-place_algorithm) such that each unique element appears **at most twice**. The **relative order** of the elements should be kept the **same**.
6+
7+
Since it is impossible to change the length of the array in some languages, you must instead have the result be placed in the **first part** of the array `nums`. More formally, if there are `k` elements after removing the duplicates, then the first `k` elements of `nums` should hold the final result. It does not matter what you leave beyond the first `k` elements.
8+
9+
Return `k` _after placing the final result in the first_ `k` _slots of_ `nums`.
10+
11+
Do **not** allocate extra space for another array. You must do this by **modifying the input array [in-place](https://en.wikipedia.org/wiki/In-place_algorithm)** with O(1) extra memory.
12+
13+
**Custom Judge:**
14+
15+
The judge will test your solution with the following code:
16+
17+
int[] nums = [...]; // Input array int[] expectedNums = [...]; // The expected answer with correct length int k = removeDuplicates(nums); // Calls your implementation assert k == expectedNums.length; for (int i = 0; i < k; i++) { assert nums[i] == expectedNums[i]; }
18+
19+
If all assertions pass, then your solution will be **accepted**.
20+
21+
**Example 1:**
22+
23+
**Input:** nums = [1,1,1,2,2,3]
24+
25+
**Output:** 5, nums = [1,1,2,2,3,\_]
26+
27+
**Explanation:** Your function should return k = 5, with the first five elements of nums being 1, 1, 2, 2 and 3 respectively. It does not matter what you leave beyond the returned k (hence they are underscores).
28+
29+
**Example 2:**
30+
31+
**Input:** nums = [0,0,1,1,1,1,2,3,3]
32+
33+
**Output:** 7, nums = [0,0,1,1,2,3,3,\_,\_]
34+
35+
**Explanation:** Your function should return k = 7, with the first seven elements of nums being 0, 0, 1, 1, 2, 3 and 3 respectively. It does not matter what you leave beyond the returned k (hence they are underscores).
36+
37+
**Constraints:**
38+
39+
* <code>1 <= nums.length <= 3 * 10<sup>4</sup></code>
40+
* <code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code>
41+
* `nums` is sorted in **non-decreasing** order.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package g0001_0100.s0069_sqrtx
2+
3+
import org.hamcrest.CoreMatchers.equalTo
4+
import org.hamcrest.MatcherAssert.assertThat
5+
import org.junit.jupiter.api.Test
6+
7+
internal class SolutionTest {
8+
@Test
9+
fun mySqrt() {
10+
assertThat(Solution().mySqrt(4), equalTo(2))
11+
}
12+
13+
@Test
14+
fun mySqrt2() {
15+
assertThat(Solution().mySqrt(8), equalTo(2))
16+
}
17+
}

0 commit comments

Comments
 (0)