Skip to content

Commit 1a10039

Browse files
authored
Added tasks 89, 90, 91, 92.
1 parent d17492e commit 1a10039

File tree

13 files changed

+391
-0
lines changed

13 files changed

+391
-0
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.4'
105105
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
106106
|-|-|-|-|-|-
107107
| 0078 |[Subsets](src/main/kotlin/g0001_0100/s0078_subsets/Solution.kt)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Bit_Manipulation, Backtracking | 353 | 30.14
108+
| 0090 |[Subsets II](src/main/kotlin/g0001_0100/s0090_subsets_ii/Solution.kt)| Medium | Array, Bit_Manipulation, Backtracking | 366 | 58.09
108109

109110
#### Day 10 Recursion Backtracking
110111

@@ -145,6 +146,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.4'
145146

146147
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
147148
|-|-|-|-|-|-
149+
| 0091 |[Decode Ways](src/main/kotlin/g0001_0100/s0091_decode_ways/Solution.kt)| Medium | Top_Interview_Questions, String, Dynamic_Programming | 327 | 17.68
148150
| 0139 |[Word Break](src/main/kotlin/g0101_0200/s0139_word_break/Solution.kt)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Dynamic_Programming, Trie, Memoization | 197 | 87.17
149151

150152
#### Day 16 Dynamic Programming
@@ -415,6 +417,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.4'
415417

416418
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
417419
|-|-|-|-|-|-
420+
| 0091 |[Decode Ways](src/main/kotlin/g0001_0100/s0091_decode_ways/Solution.kt)| Medium | Top_Interview_Questions, String, Dynamic_Programming | 327 | 17.68
418421

419422
#### Day 11
420423

@@ -1530,6 +1533,10 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.4'
15301533
| 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
15311534
| 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
15321535
| 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
1536+
| 0092 |[Reverse Linked List II](src/main/kotlin/g0001_0100/s0092_reverse_linked_list_ii/Solution.kt)| Medium | Linked_List | 191 | 82.35
1537+
| 0091 |[Decode Ways](src/main/kotlin/g0001_0100/s0091_decode_ways/Solution.kt)| Medium | Top_Interview_Questions, String, Dynamic_Programming, Algorithm_II_Day_15_Dynamic_Programming, Dynamic_Programming_I_Day_10 | 327 | 17.68
1538+
| 0090 |[Subsets II](src/main/kotlin/g0001_0100/s0090_subsets_ii/Solution.kt)| Medium | Array, Bit_Manipulation, Backtracking, Algorithm_II_Day_9_Recursion_Backtracking | 366 | 58.09
1539+
| 0089 |[Gray Code](src/main/kotlin/g0001_0100/s0089_gray_code/Solution.kt)| Medium | Math, Bit_Manipulation, Backtracking | 528 | 62.86
15331540
| 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
15341541
| 0087 |[Partition List](src/main/kotlin/g0001_0100/s0087_scramble_string/Solution.kt)| Hard | String, Dynamic_Programming | 366 | 85.00
15351542
| 0086 |[Partition List](src/main/kotlin/g0001_0100/s0086_partition_list/Solution.kt)| Medium | Two_Pointers, Linked_List | 331 | 5.88
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package g0001_0100.s0089_gray_code
2+
3+
// #Medium #Math #Bit_Manipulation #Backtracking
4+
// #2022_09_26_Time_528_ms_(62.86%)_Space_61.8_MB_(88.57%)
5+
6+
import java.util.Arrays
7+
8+
class Solution {
9+
fun grayCode(n: Int): List<Int> {
10+
var n = n
11+
var n1 = arrayOf<Int?>(0)
12+
var shift = 1
13+
while (n > 0) {
14+
val temp = arrayOfNulls<Int>(n1.size * 2)
15+
var pos = 0
16+
for (integer in n1) {
17+
temp[pos++] = integer
18+
}
19+
for (i in 0..n1.size - 1) {
20+
temp[pos++] = n1[n1.size - i - 1]!! or shift
21+
}
22+
n1 = temp
23+
shift = shift shl 1
24+
n--
25+
}
26+
return Arrays.asList(*n1) as List<Int>
27+
}
28+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
89\. Gray Code
2+
3+
Medium
4+
5+
An **n-bit gray code sequence** is a sequence of <code>2<sup>n</sup></code> integers where:
6+
7+
* Every integer is in the **inclusive** range <code>[0, 2<sup>n</sup> - 1]</code>,
8+
* The first integer is `0`,
9+
* An integer appears **no more than once** in the sequence,
10+
* The binary representation of every pair of **adjacent** integers differs by **exactly one bit**, and
11+
* The binary representation of the **first** and **last** integers differs by **exactly one bit**.
12+
13+
Given an integer `n`, return _any valid **n-bit gray code sequence**_.
14+
15+
**Example 1:**
16+
17+
**Input:** n = 2
18+
19+
**Output:** [0,1,3,2]
20+
21+
**Explanation:** The binary representation of [0,1,3,2] is [00,01,11,10]. - 0<ins>0</ins> and 0<ins>1</ins> differ by one bit - <ins>0</ins>1 and <ins>1</ins>1 differ by one bit - 1<ins>1</ins> and 1<ins>0</ins> differ by one bit - <ins>1</ins>0 and <ins>0</ins>0 differ by one bit [0,2,3,1] is also a valid gray code sequence, whose binary representation is [00,10,11,01]. - <ins>0</ins>0 and <ins>1</ins>0 differ by one bit - 1<ins>0</ins> and 1<ins>1</ins> differ by one bit - <ins>1</ins>1 and <ins>0</ins>1 differ by one bit - 0<ins>1</ins> and 0<ins>0</ins> differ by one bit
22+
23+
**Example 2:**
24+
25+
**Input:** n = 1
26+
27+
**Output:** [0,1]
28+
29+
**Constraints:**
30+
31+
* `1 <= n <= 16`
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package g0001_0100.s0090_subsets_ii
2+
3+
// #Medium #Array #Bit_Manipulation #Backtracking #Algorithm_II_Day_9_Recursion_Backtracking
4+
// #2022_09_26_Time_366_ms_(58.09%)_Space_38.9_MB_(88.97%
5+
6+
import java.util.Arrays
7+
8+
class Solution {
9+
var allComb: MutableList<List<Int>> = ArrayList()
10+
var comb: MutableList<Int> = ArrayList()
11+
lateinit var nums: IntArray
12+
fun subsetsWithDup(nums: IntArray): List<List<Int>> {
13+
Arrays.sort(nums)
14+
this.nums = nums
15+
dfs(0)
16+
allComb.add(ArrayList())
17+
return allComb
18+
}
19+
20+
private fun dfs(start: Int) {
21+
if (start > nums.size) {
22+
return
23+
}
24+
for (i in start until nums.size) {
25+
if (i > start && nums[i] == nums[i - 1]) {
26+
continue
27+
}
28+
comb.add(nums[i])
29+
allComb.add(ArrayList(comb))
30+
dfs(i + 1)
31+
comb.removeAt(comb.size - 1)
32+
}
33+
}
34+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
90\. Subsets II
2+
3+
Medium
4+
5+
Given an integer array `nums` that may contain duplicates, return _all possible subsets (the power set)_.
6+
7+
The solution set **must not** contain duplicate subsets. Return the solution in **any order**.
8+
9+
**Example 1:**
10+
11+
**Input:** nums = [1,2,2]
12+
13+
**Output:** [[],[1],[1,2],[1,2,2],[2],[2,2]]
14+
15+
**Example 2:**
16+
17+
**Input:** nums = [0]
18+
19+
**Output:** [[],[0]]
20+
21+
**Constraints:**
22+
23+
* `1 <= nums.length <= 10`
24+
* `-10 <= nums[i] <= 10`
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package g0001_0100.s0091_decode_ways
2+
3+
// #Medium #Top_Interview_Questions #String #Dynamic_Programming
4+
// #Algorithm_II_Day_15_Dynamic_Programming #Dynamic_Programming_I_Day_10
5+
// #2022_09_26_Time_327_ms_(17.68%)_Space_34.7_MB_(82.93%)
6+
7+
class Solution {
8+
fun numDecodings(s: String): Int {
9+
if (s[0] == '0') {
10+
return 0
11+
}
12+
val n = s.length
13+
val f = IntArray(n + 1)
14+
// Auxiliary
15+
f[0] = 1
16+
f[1] = 1
17+
for (i in 2..n) {
18+
// Calculate the independent number
19+
if (s[i - 1] != '0') {
20+
// As long as the current character is not 0, it means that the previous decoding
21+
// number can be inherited
22+
f[i] = f[i - 1]
23+
}
24+
// Calculate the number of combinations
25+
val twodigits = (s[i - 2] - '0') * 10 + (s[i - 1] - '0')
26+
if (twodigits >= 10 && twodigits <= 26) {
27+
f[i] += f[i - 2]
28+
}
29+
}
30+
return f[n]
31+
}
32+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
91\. Decode Ways
2+
3+
Medium
4+
5+
A message containing letters from `A-Z` can be **encoded** into numbers using the following mapping:
6+
7+
'A' -> "1" 'B' -> "2" ... 'Z' -> "26"
8+
9+
To **decode** an encoded message, all the digits must be grouped then mapped back into letters using the reverse of the mapping above (there may be multiple ways). For example, `"11106"` can be mapped into:
10+
11+
* `"AAJF"` with the grouping `(1 1 10 6)`
12+
* `"KJF"` with the grouping `(11 10 6)`
13+
14+
Note that the grouping `(1 11 06)` is invalid because `"06"` cannot be mapped into `'F'` since `"6"` is different from `"06"`.
15+
16+
Given a string `s` containing only digits, return _the **number** of ways to **decode** it_.
17+
18+
The test cases are generated so that the answer fits in a **32-bit** integer.
19+
20+
**Example 1:**
21+
22+
**Input:** s = "12"
23+
24+
**Output:** 2
25+
26+
**Explanation:** "12" could be decoded as "AB" (1 2) or "L" (12).
27+
28+
**Example 2:**
29+
30+
**Input:** s = "226"
31+
32+
**Output:** 3
33+
34+
**Explanation:** "226" could be decoded as "BZ" (2 26), "VF" (22 6), or "BBF" (2 2 6).
35+
36+
**Example 3:**
37+
38+
**Input:** s = "06"
39+
40+
**Output:** 0
41+
42+
**Explanation:** "06" cannot be mapped to "F" because of the leading zero ("6" is different from "06").
43+
44+
**Constraints:**
45+
46+
* `1 <= s.length <= 100`
47+
* `s` contains only digits and may contain leading zero(s).
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package g0001_0100.s0092_reverse_linked_list_ii
2+
3+
// #Medium #Linked_List #2022_09_26_Time_191_ms_(82.35%)_Space_34.4_MB_(29.41%)
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 reverseBetween(head: ListNode?, left: Int, right: Int): ListNode? {
18+
var head = head
19+
var right = right
20+
if (left == right) {
21+
return head
22+
}
23+
var prev: ListNode? = null
24+
var temp = head
25+
val start: ListNode?
26+
var k = left
27+
while (temp != null && k > 1) {
28+
prev = temp
29+
temp = temp.next
30+
k--
31+
}
32+
if (left > 1 && prev != null) {
33+
prev.next = null
34+
}
35+
var prev1: ListNode? = null
36+
start = temp
37+
while (temp != null && right - left >= 0) {
38+
prev1 = temp
39+
temp = temp.next
40+
right--
41+
}
42+
if (prev1 != null) {
43+
prev1.next = null
44+
}
45+
if (left > 1 && prev != null) {
46+
prev.next = reverse(start)
47+
} else {
48+
head = reverse(start)
49+
prev = head
50+
}
51+
while (prev!!.next != null) {
52+
prev = prev.next
53+
}
54+
prev.next = temp
55+
return head
56+
}
57+
58+
fun reverse(head: ListNode?): ListNode? {
59+
var p: ListNode?
60+
var q: ListNode?
61+
var r: ListNode? = null
62+
p = head
63+
while (p != null) {
64+
q = p.next
65+
p.next = r
66+
r = p
67+
p = q
68+
}
69+
return r
70+
}
71+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
92\. Reverse Linked List II
2+
3+
Medium
4+
5+
Given the `head` of a singly linked list and two integers `left` and `right` where `left <= right`, reverse the nodes of the list from position `left` to position `right`, and return _the reversed list_.
6+
7+
**Example 1:**
8+
9+
![](https://assets.leetcode.com/uploads/2021/02/19/rev2ex2.jpg)
10+
11+
**Input:** head = [1,2,3,4,5], left = 2, right = 4
12+
13+
**Output:** [1,4,3,2,5]
14+
15+
**Example 2:**
16+
17+
**Input:** head = [5], left = 1, right = 1
18+
19+
**Output:** [5]
20+
21+
**Constraints:**
22+
23+
* The number of nodes in the list is `n`.
24+
* `1 <= n <= 500`
25+
* `-500 <= Node.val <= 500`
26+
* `1 <= left <= right <= n`
27+
28+
**Follow up:** Could you do it in one pass?
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package g0001_0100.s0089_gray_code
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 grayCode() {
10+
assertThat(Solution().grayCode(2), equalTo(intArrayOf(0, 1, 3, 2).asList()))
11+
}
12+
13+
@Test
14+
fun grayCode2() {
15+
assertThat(Solution().grayCode(1), equalTo(intArrayOf(0, 1).asList()))
16+
}
17+
}

0 commit comments

Comments
 (0)