Skip to content

Commit 346f5e8

Browse files
authored
Added tasks 81, 82, 83, 85.
1 parent bbe5af0 commit 346f5e8

File tree

13 files changed

+405
-1
lines changed

13 files changed

+405
-1
lines changed

README.md

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

7070
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
7171
|-|-|-|-|-|-
72+
| 0082 |[Remove Duplicates from Sorted List II](src/main/kotlin/g0001_0100/s0082_remove_duplicates_from_sorted_list_ii/Solution.kt)| Medium | Two_Pointers, Linked_List | 241 | 91.04
7273
| 0015 |[3Sum](src/main/kotlin/g0001_0100/s0015_3sum/Solution.kt)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Two_Pointers | 1059 | 55.28
7374

7475
#### Day 4 Two Pointers
@@ -312,6 +313,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.4'
312313

313314
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
314315
|-|-|-|-|-|-
316+
| 0081 |[Search in Rotated Sorted Array II](src/main/kotlin/g0001_0100/s0081_search_in_rotated_sorted_array_ii/Solution.kt)| Medium | Array, Binary_Search | 352 | 42.31
315317

316318
#### Day 13
317319

@@ -1206,6 +1208,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.4'
12061208
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
12071209
|-|-|-|-|-|-
12081210
| 0206 |[Reverse Linked List](src/main/kotlin/g0201_0300/s0206_reverse_linked_list/Solution.kt)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Linked_List, Recursion | 309 | 15.90
1211+
| 0083 |[Remove Duplicates from Sorted List](src/main/kotlin/g0001_0100/s0083_remove_duplicates_from_sorted_list/Solution.kt)| Easy | Linked_List | 274 | 77.82
12091212

12101213
#### Day 9 Stack Queue
12111214

@@ -1316,6 +1319,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.4'
13161319
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
13171320
|-|-|-|-|-|-
13181321
| 0160 |[Intersection of Two Linked Lists](src/main/kotlin/g0101_0200/s0160_intersection_of_two_linked_lists/Solution.kt)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Hash_Table, Two_Pointers, Linked_List | 262 | 83.50
1322+
| 0082 |[Remove Duplicates from Sorted List II](src/main/kotlin/g0001_0100/s0082_remove_duplicates_from_sorted_list_ii/Solution.kt)| Medium | Two_Pointers, Linked_List | 241 | 91.04
13191323

13201324
#### Day 12 Linked List
13211325

@@ -1525,7 +1529,11 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.4'
15251529
| 0098 |[Validate Binary Search Tree](src/main/kotlin/g0001_0100/s0098_validate_binary_search_tree/Solution.kt)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Tree, Binary_Tree, Binary_Search_Tree, Data_Structure_I_Day_14_Tree, Level_1_Day_8_Binary_Search_Tree, Udemy_Tree_Stack_Queue | 330 | 41.38
15261530
| 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
15271531
| 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
1532+
| 0085 |[Maximal Rectangle](src/main/kotlin/g0001_0100/s0085_maximal_rectangle/Solution.kt)| Hard | Array, Dynamic_Programming, Matrix, Stack, Monotonic_Stack | 463 | 55.17
15281533
| 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
1534+
| 0083 |[Remove Duplicates from Sorted List](src/main/kotlin/g0001_0100/s0083_remove_duplicates_from_sorted_list/Solution.kt)| Easy | Linked_List, Data_Structure_I_Day_8_Linked_List | 274 | 77.82
1535+
| 0082 |[Remove Duplicates from Sorted List II](src/main/kotlin/g0001_0100/s0082_remove_duplicates_from_sorted_list_ii/Solution.kt)| Medium | Two_Pointers, Linked_List, Data_Structure_II_Day_11_Linked_List, Algorithm_II_Day_3_Two_Pointers | 241 | 91.04
1536+
| 0081 |[Search in Rotated Sorted Array II](src/main/kotlin/g0001_0100/s0081_search_in_rotated_sorted_array_ii/Solution.kt)| Medium | Array, Binary_Search, Binary_Search_II_Day_12 | 352 | 42.31
15291537
| 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
15301538
| 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
15311539
| 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
@@ -1535,7 +1543,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.4'
15351543
| 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
15361544
| 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
15371545
| 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)| |||
1546+
| 0071 |[Simplify Path](src/main/kotlin/g0001_0100/s0071_simplify_path/Solution.kt)| Medium | String, Stack | 318 | 64.44
15391547
| 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
15401548
| 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
15411549
| 0068 |[Text Justification](src/main/kotlin/g0001_0100/s0068_text_justification/Solution.kt)| Hard | Array, String, Simulation | 269 | 50.00
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g0001_0100.s0081_search_in_rotated_sorted_array_ii
2+
3+
// #Medium #Array #Binary_Search #Binary_Search_II_Day_12
4+
// #2022_09_25_Time_352_ms_(42.31%)_Space_37.5_MB_(88.46%)
5+
6+
class Solution {
7+
fun search(nums: IntArray, target: Int): Boolean {
8+
return binary(nums, 0, nums.size - 1, target)
9+
}
10+
11+
private fun binary(a: IntArray, i: Int, j: Int, t: Int): Boolean {
12+
if (i > j) {
13+
return false
14+
}
15+
val mid = (i + j) / 2
16+
if (a[mid] == t) {
17+
return true
18+
}
19+
val c1 = binary(a, i, mid - 1, t)
20+
val c2 = binary(a, mid + 1, j, t)
21+
return c1 || c2
22+
}
23+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
81\. Search in Rotated Sorted Array II
2+
3+
Medium
4+
5+
There is an integer array `nums` sorted in non-decreasing order (not necessarily with **distinct** values).
6+
7+
Before being passed to your function, `nums` is **rotated** at an unknown pivot index `k` (`0 <= k < nums.length`) such that the resulting array is `[nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]]` (**0-indexed**). For example, `[0,1,2,4,4,4,5,6,6,7]` might be rotated at pivot index `5` and become `[4,5,6,6,7,0,1,2,4,4]`.
8+
9+
Given the array `nums` **after** the rotation and an integer `target`, return `true` _if_ `target` _is in_ `nums`_, or_ `false` _if it is not in_ `nums`_._
10+
11+
You must decrease the overall operation steps as much as possible.
12+
13+
**Example 1:**
14+
15+
**Input:** nums = [2,5,6,0,0,1,2], target = 0
16+
17+
**Output:** true
18+
19+
**Example 2:**
20+
21+
**Input:** nums = [2,5,6,0,0,1,2], target = 3
22+
23+
**Output:** false
24+
25+
**Constraints:**
26+
27+
* `1 <= nums.length <= 5000`
28+
* <code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code>
29+
* `nums` is guaranteed to be rotated at some pivot.
30+
* <code>-10<sup>4</sup> <= target <= 10<sup>4</sup></code>
31+
32+
**Follow up:** This problem is similar to [Search in Rotated Sorted Array](/problems/search-in-rotated-sorted-array/description/), but `nums` may contain **duplicates**. Would this affect the runtime complexity? How and why?
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package g0001_0100.s0082_remove_duplicates_from_sorted_list_ii
2+
3+
// #Medium #Two_Pointers #Linked_List #Data_Structure_II_Day_11_Linked_List
4+
// #Algorithm_II_Day_3_Two_Pointers #2022_09_25_Time_241_ms_(91.04%)_Space_39.6_MB_(16.42%)
5+
6+
import com_github_leetcode.ListNode
7+
8+
/*
9+
* Example:
10+
* var li = ListNode(5)
11+
* var v = li.`val`
12+
* Definition for singly-linked list.
13+
* class ListNode(var `val`: Int) {
14+
* var next: ListNode? = null
15+
* }
16+
*/
17+
class Solution {
18+
fun deleteDuplicates(head: ListNode?): ListNode? {
19+
if (head == null || head.next == null) {
20+
return head
21+
}
22+
val dummy = ListNode(0)
23+
var prev: ListNode? = dummy
24+
prev!!.next = head
25+
var curr = head.next
26+
while (curr != null) {
27+
var flagFoundDuplicate = false
28+
while (curr != null && prev!!.next!!.`val` == curr.`val`) {
29+
flagFoundDuplicate = true
30+
curr = curr.next
31+
}
32+
if (flagFoundDuplicate) {
33+
prev!!.next = curr
34+
if (curr != null) {
35+
curr = curr.next
36+
}
37+
} else {
38+
prev = prev!!.next
39+
prev!!.next = curr
40+
curr = curr!!.next
41+
}
42+
}
43+
return dummy.next
44+
}
45+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
82\. Remove Duplicates from Sorted List II
2+
3+
Medium
4+
5+
Given the `head` of a sorted linked list, _delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list_. Return _the linked list **sorted** as well_.
6+
7+
**Example 1:**
8+
9+
![](https://assets.leetcode.com/uploads/2021/01/04/linkedlist1.jpg)
10+
11+
**Input:** head = [1,2,3,3,4,4,5]
12+
13+
**Output:** [1,2,5]
14+
15+
**Example 2:**
16+
17+
![](https://assets.leetcode.com/uploads/2021/01/04/linkedlist2.jpg)
18+
19+
**Input:** head = [1,1,1,2,3]
20+
21+
**Output:** [2,3]
22+
23+
**Constraints:**
24+
25+
* The number of nodes in the list is in the range `[0, 300]`.
26+
* `-100 <= Node.val <= 100`
27+
* The list is guaranteed to be **sorted** in ascending order.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package g0001_0100.s0083_remove_duplicates_from_sorted_list
2+
3+
// #Easy #Linked_List #Data_Structure_I_Day_8_Linked_List
4+
// #2022_09_25_Time_274_ms_(77.82%)_Space_37.9_MB_(53.23%)
5+
6+
import com_github_leetcode.ListNode
7+
8+
/*
9+
* Example:
10+
* var li = ListNode(5)
11+
* var v = li.`val`
12+
* Definition for singly-linked list.
13+
* class ListNode(var `val`: Int) {
14+
* var next: ListNode? = null
15+
* }
16+
*/
17+
class Solution {
18+
fun deleteDuplicates(head: ListNode?): ListNode? {
19+
if (head == null) {
20+
return null
21+
}
22+
var current: ListNode = head
23+
var next = current.next
24+
while (null != next) {
25+
if (current.`val` == next.`val`) {
26+
current.next = next.next
27+
} else {
28+
current = next
29+
}
30+
next = current.next
31+
}
32+
return head
33+
}
34+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
83\. Remove Duplicates from Sorted List
2+
3+
Easy
4+
5+
Given the `head` of a sorted linked list, _delete all duplicates such that each element appears only once_. Return _the linked list **sorted** as well_.
6+
7+
**Example 1:**
8+
9+
![](https://assets.leetcode.com/uploads/2021/01/04/list1.jpg)
10+
11+
**Input:** head = [1,1,2]
12+
13+
**Output:** [1,2]
14+
15+
**Example 2:**
16+
17+
![](https://assets.leetcode.com/uploads/2021/01/04/list2.jpg)
18+
19+
**Input:** head = [1,1,2,3,3]
20+
21+
**Output:** [1,2,3]
22+
23+
**Constraints:**
24+
25+
* The number of nodes in the list is in the range `[0, 300]`.
26+
* `-100 <= Node.val <= 100`
27+
* The list is guaranteed to be **sorted** in ascending order.
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package g0001_0100.s0085_maximal_rectangle
2+
3+
// #Hard #Array #Dynamic_Programming #Matrix #Stack #Monotonic_Stack
4+
// #2022_09_25_Time_463_ms_(55.17%)_Space_58.5_MB_(62.07%)
5+
6+
class Solution {
7+
fun maximalRectangle(matrix: Array<CharArray>): Int {
8+
/*
9+
* idea: using [LC84 Largest Rectangle in Histogram]. For each row of the matrix, construct
10+
* the histogram based on the current row and the previous histogram (up to the previous
11+
* row), then compute the largest rectangle area using LC84.
12+
*/
13+
val m = matrix.size
14+
var n = 0
15+
if (m == 0 || matrix[0].size.also { n = it } == 0) {
16+
return 0
17+
}
18+
var i: Int
19+
var j: Int
20+
var res = 0
21+
val heights = IntArray(n)
22+
i = 0
23+
while (i < m) {
24+
j = 0
25+
while (j < n) {
26+
if (matrix[i][j] == '0') {
27+
heights[j] = 0
28+
} else {
29+
heights[j] += 1
30+
}
31+
j++
32+
}
33+
res = Math.max(res, largestRectangleArea(heights))
34+
i++
35+
}
36+
return res
37+
}
38+
39+
private fun largestRectangleArea(heights: IntArray): Int {
40+
/*
41+
* idea: scan and store if a[i-1]<=a[i] (increasing), then as long as a[i]<a[i-1], then we
42+
* can compute the largest rectangle area with base a[j], for j<=i-1, and a[j]>a[i], which
43+
* is a[j]*(i-j). And meanwhile, all these bars (a[j]'s) are already done, and thus are
44+
* throwable (using pop() with a stack).
45+
*
46+
* <p>We can use an array nLeftGeq[] of size n to simulate a stack. nLeftGeq[i] = the number
47+
* of elements to the left of [i] having value greater than or equal to a[i] (including a[i]
48+
* itself). It is also the index difference between [i] and the next index on the top of the
49+
* stack.
50+
*/
51+
val n = heights.size
52+
if (n == 0) {
53+
return 0
54+
}
55+
val nLeftGeq = IntArray(n)
56+
// the number of elements to the left
57+
// of [i] with value >= heights[i]
58+
nLeftGeq[0] = 1
59+
// preIdx=the index of stack.peek(), res=max area so far
60+
var preIdx = 0
61+
var res = 0
62+
for (i in 1 until n) {
63+
nLeftGeq[i] = 1
64+
// notice that preIdx = i - 1 = peek()
65+
while (preIdx >= 0 && heights[i] < heights[preIdx]) {
66+
res = Math.max(res, heights[preIdx] * (nLeftGeq[preIdx] + i - preIdx - 1))
67+
// pop()
68+
nLeftGeq[i] += nLeftGeq[preIdx]
69+
// peek() current top
70+
preIdx = preIdx - nLeftGeq[preIdx]
71+
}
72+
if (preIdx >= 0 && heights[i] == heights[preIdx]) {
73+
// pop()
74+
nLeftGeq[i] += nLeftGeq[preIdx]
75+
}
76+
// otherwise nothing to do
77+
preIdx = i
78+
}
79+
// compute the rest largest rectangle areas with (indices of) bases
80+
// on stack
81+
while (preIdx >= 0 && 0 < heights[preIdx]) {
82+
res = Math.max(res, heights[preIdx] * (nLeftGeq[preIdx] + n - preIdx - 1))
83+
// peek() current top
84+
preIdx = preIdx - nLeftGeq[preIdx]
85+
}
86+
return res
87+
}
88+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
85\. Maximal Rectangle
2+
3+
Hard
4+
5+
Given a `rows x cols` binary `matrix` filled with `0`'s and `1`'s, find the largest rectangle containing only `1`'s and return _its area_.
6+
7+
**Example 1:**
8+
9+
![](https://assets.leetcode.com/uploads/2020/09/14/maximal.jpg)
10+
11+
**Input:** matrix = [["1","0","1","0","0"],["1","0","1","1","1"],["1","1","1","1","1"],["1","0","0","1","0"]]
12+
13+
**Output:** 6
14+
15+
**Explanation:** The maximal rectangle is shown in the above picture.
16+
17+
**Example 2:**
18+
19+
**Input:** matrix = [["0"]]
20+
21+
**Output:** 0
22+
23+
**Example 3:**
24+
25+
**Input:** matrix = [["1"]]
26+
27+
**Output:** 1
28+
29+
**Constraints:**
30+
31+
* `rows == matrix.length`
32+
* `cols == matrix[i].length`
33+
* `1 <= row, cols <= 200`
34+
* `matrix[i][j]` is `'0'` or `'1'`.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package g0001_0100.s0081_search_in_rotated_sorted_array_ii
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 search() {
10+
assertThat(Solution().search(intArrayOf(2, 5, 6, 0, 0, 1, 2), 0), equalTo(true))
11+
}
12+
13+
@Test
14+
fun search2() {
15+
assertThat(Solution().search(intArrayOf(2, 5, 6, 0, 0, 1, 2), 3), equalTo(false))
16+
}
17+
}

0 commit comments

Comments
 (0)