Skip to content

Commit d17492e

Browse files
authored
Added tasks 86, 87, 88.
1 parent 346f5e8 commit d17492e

File tree

10 files changed

+251
-0
lines changed

10 files changed

+251
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1172,6 +1172,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.4'
11721172
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
11731173
|-|-|-|-|-|-
11741174
| 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
1175+
| 0088 |[Merge Sorted Array](src/main/kotlin/g0001_0100/s0088_merge_sorted_array/Solution.kt)| Easy | Top_Interview_Questions, Array, Sorting, Two_Pointers | 311 | 33.40
11751176

11761177
#### Day 3 Array
11771178

@@ -1529,6 +1530,9 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.4'
15291530
| 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
15301531
| 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
15311532
| 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
1533+
| 0088 |[Merge Sorted Array](src/main/kotlin/g0001_0100/s0088_merge_sorted_array/Solution.kt)| Easy | Top_Interview_Questions, Array, Sorting, Two_Pointers, Data_Structure_I_Day_2_Array | 311 | 33.40
1534+
| 0087 |[Partition List](src/main/kotlin/g0001_0100/s0087_scramble_string/Solution.kt)| Hard | String, Dynamic_Programming | 366 | 85.00
1535+
| 0086 |[Partition List](src/main/kotlin/g0001_0100/s0086_partition_list/Solution.kt)| Medium | Two_Pointers, Linked_List | 331 | 5.88
15321536
| 0085 |[Maximal Rectangle](src/main/kotlin/g0001_0100/s0085_maximal_rectangle/Solution.kt)| Hard | Array, Dynamic_Programming, Matrix, Stack, Monotonic_Stack | 463 | 55.17
15331537
| 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
15341538
| 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
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package g0001_0100.s0086_partition_list
2+
3+
// #Medium #Two_Pointers #Linked_List #2022_09_25_Time_331_ms_(5.88%)_Space_35.6_MB_(35.29%)
4+
5+
import com_github_leetcode.ListNode
6+
7+
/*
8+
* Example:
9+
* var li = ListNode(5)
10+
* var v = li.`val`
11+
* Definition for singly-linked list.
12+
* class ListNode(var `val`: Int) {
13+
* var next: ListNode? = null
14+
* }
15+
*/
16+
class Solution {
17+
fun partition(head: ListNode?, x: Int): ListNode? {
18+
var head = head
19+
var nHead: ListNode? = ListNode(0)
20+
var nTail: ListNode? = ListNode(0)
21+
val ptr = nTail
22+
val temp = nHead
23+
while (head != null) {
24+
val nNext = head.next
25+
if (head.`val` < x) {
26+
nHead!!.next = head
27+
nHead = nHead.next
28+
} else {
29+
nTail!!.next = head
30+
nTail = nTail.next
31+
}
32+
head = nNext
33+
}
34+
nTail!!.next = null
35+
nHead!!.next = ptr!!.next
36+
return temp!!.next
37+
}
38+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
86\. Partition List
2+
3+
Medium
4+
5+
Given the `head` of a linked list and a value `x`, partition it such that all nodes **less than** `x` come before nodes **greater than or equal** to `x`.
6+
7+
You should **preserve** the original relative order of the nodes in each of the two partitions.
8+
9+
**Example 1:**
10+
11+
![](https://assets.leetcode.com/uploads/2021/01/04/partition.jpg)
12+
13+
**Input:** head = [1,4,3,2,5,2], x = 3
14+
15+
**Output:** [1,2,2,4,3,5]
16+
17+
**Example 2:**
18+
19+
**Input:** head = [2,1], x = 2
20+
21+
**Output:** [1,2]
22+
23+
**Constraints:**
24+
25+
* The number of nodes in the list is in the range `[0, 200]`.
26+
* `-100 <= Node.val <= 100`
27+
* `-200 <= x <= 200`
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package g0001_0100.s0087_scramble_string
2+
3+
// #Hard #String #Dynamic_Programming #2022_09_25_Time_366_ms_(85.00%)_Space_36.2_MB_(100.00%)
4+
5+
class Solution {
6+
fun isScramble(s1: String, s2: String): Boolean {
7+
val n = s1.length
8+
val dp = Array(n) { Array(n) { BooleanArray(n + 1) } }
9+
for (len in 1..n) {
10+
for (i in 0..n - len) {
11+
for (j in 0..n - len) {
12+
if (len == 1) {
13+
dp[i][j][len] = s1[i] == s2[j]
14+
} else {
15+
var k = 1
16+
while (k < len && !dp[i][j][len]) {
17+
dp[i][j][len] = (
18+
dp[i][j][k] && dp[i + k][j + k][len - k] ||
19+
dp[i][j + len - k][k] && dp[i + k][j][len - k]
20+
)
21+
k++
22+
}
23+
}
24+
}
25+
}
26+
}
27+
return dp[0][0][n]
28+
}
29+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
86\. Partition List
2+
3+
Medium
4+
5+
Given the `head` of a linked list and a value `x`, partition it such that all nodes **less than** `x` come before nodes **greater than or equal** to `x`.
6+
7+
You should **preserve** the original relative order of the nodes in each of the two partitions.
8+
9+
**Example 1:**
10+
11+
![](https://assets.leetcode.com/uploads/2021/01/04/partition.jpg)
12+
13+
**Input:** head = [1,4,3,2,5,2], x = 3
14+
15+
**Output:** [1,2,2,4,3,5]
16+
17+
**Example 2:**
18+
19+
**Input:** head = [2,1], x = 2
20+
21+
**Output:** [1,2]
22+
23+
**Constraints:**
24+
25+
* The number of nodes in the list is in the range `[0, 200]`.
26+
* `-100 <= Node.val <= 100`
27+
* `-200 <= x <= 200`
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package g0001_0100.s0088_merge_sorted_array
2+
3+
// #Easy #Top_Interview_Questions #Array #Sorting #Two_Pointers #Data_Structure_I_Day_2_Array
4+
// #2022_09_25_Time_311_ms_(33.40%)_Space_35.7_MB_(57.76%)
5+
6+
class Solution {
7+
fun merge(nums1: IntArray, m: Int, nums2: IntArray, n: Int) {
8+
var i = m - 1
9+
var j = nums1.size - 1
10+
var p2 = n - 1
11+
while (p2 >= 0) {
12+
if (i >= 0 && nums1[i] > nums2[p2]) {
13+
nums1[j--] = nums1[i--]
14+
} else {
15+
nums1[j--] = nums2[p2--]
16+
}
17+
}
18+
}
19+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
88\. Merge Sorted Array
2+
3+
Easy
4+
5+
You are given two integer arrays `nums1` and `nums2`, sorted in **non-decreasing order**, and two integers `m` and `n`, representing the number of elements in `nums1` and `nums2` respectively.
6+
7+
**Merge** `nums1` and `nums2` into a single array sorted in **non-decreasing order**.
8+
9+
The final sorted array should not be returned by the function, but instead be _stored inside the array_ `nums1`. To accommodate this, `nums1` has a length of `m + n`, where the first `m` elements denote the elements that should be merged, and the last `n` elements are set to `0` and should be ignored. `nums2` has a length of `n`.
10+
11+
**Example 1:**
12+
13+
**Input:** nums1 = [1,2,3,0,0,0], m = 3, nums2 = [2,5,6], n = 3
14+
15+
**Output:** [1,2,2,3,5,6]
16+
17+
**Explanation:** The arrays we are merging are [1,2,3] and [2,5,6]. The result of the merge is [<ins>1</ins>,<ins>2</ins>,2,<ins>3</ins>,5,6] with the underlined elements coming from nums1.
18+
19+
**Example 2:**
20+
21+
**Input:** nums1 = [1], m = 1, nums2 = [], n = 0
22+
23+
**Output:** [1]
24+
25+
**Explanation:** The arrays we are merging are [1] and []. The result of the merge is [1].
26+
27+
**Example 3:**
28+
29+
**Input:** nums1 = [0], m = 0, nums2 = [1], n = 1
30+
31+
**Output:** [1]
32+
33+
**Explanation:** The arrays we are merging are [] and [1]. The result of the merge is [1]. Note that because m = 0, there are no elements in nums1. The 0 is only there to ensure the merge result can fit in nums1.
34+
35+
**Constraints:**
36+
37+
* `nums1.length == m + n`
38+
* `nums2.length == n`
39+
* `0 <= m, n <= 200`
40+
* `1 <= m + n <= 200`
41+
* <code>-10<sup>9</sup> <= nums1[i], nums2[j] <= 10<sup>9</sup></code>
42+
43+
**Follow up:** Can you come up with an algorithm that runs in `O(m + n)` time?
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package g0001_0100.s0086_partition_list
2+
3+
import com_github_leetcode.LinkedListUtils.createSinglyLinkedList
4+
import org.hamcrest.CoreMatchers.equalTo
5+
import org.hamcrest.MatcherAssert.assertThat
6+
import org.junit.jupiter.api.Test
7+
import java.util.Arrays
8+
9+
internal class SolutionTest {
10+
@Test
11+
fun partition() {
12+
val head = createSinglyLinkedList(Arrays.asList(1, 4, 3, 2, 5, 2))
13+
assertThat(Solution().partition(head, 3).toString(), equalTo("1, 2, 2, 4, 3, 5"))
14+
}
15+
16+
@Test
17+
fun partition2() {
18+
val head = createSinglyLinkedList(Arrays.asList(2, 1))
19+
assertThat(Solution().partition(head, 2).toString(), equalTo("1, 2"))
20+
}
21+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package g0001_0100.s0087_scramble_string
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 isScramble() {
10+
assertThat(Solution().isScramble("great", "rgeat"), equalTo(true))
11+
}
12+
13+
@Test
14+
fun isScramble2() {
15+
assertThat(Solution().isScramble("abcde", "caebd"), equalTo(false))
16+
}
17+
18+
@Test
19+
fun isScramble3() {
20+
assertThat(Solution().isScramble("a", "a"), equalTo(true))
21+
}
22+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package g0001_0100.s0088_merge_sorted_array
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 merge() {
10+
val array = intArrayOf(1, 2, 3, 0, 0, 0)
11+
Solution().merge(array, 3, intArrayOf(2, 5, 6), 3)
12+
assertThat(array, equalTo(intArrayOf(1, 2, 2, 3, 5, 6)))
13+
}
14+
15+
@Test
16+
fun merge2() {
17+
val array = intArrayOf(1)
18+
Solution().merge(array, 1, intArrayOf(), 0)
19+
assertThat(array, equalTo(intArrayOf(1)))
20+
}
21+
}

0 commit comments

Comments
 (0)