Skip to content

Commit ef89b27

Browse files
authored
Added tasks 39, 41, 42, 45.
1 parent 4b4b779 commit ef89b27

File tree

13 files changed

+336
-1
lines changed

13 files changed

+336
-1
lines changed

src/main/kotlin/g0001_0100/s0008_string_to_integer_atoi/Solution.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class Solution {
2727
var tem = input[i] - '0'
2828
tem = if (negativeSign) -tem else tem
2929
// avoid invalid number like 038
30-
if (num == 0 && tem == '0'.code) {
30+
if (num == 0 && tem == '0'.toInt()) {
3131
i++
3232
} else if (num == Int.MIN_VALUE / 10 && tem <= -8 || num < Int.MIN_VALUE / 10) {
3333
return Int.MIN_VALUE
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package g0001_0100.s0039_combination_sum
2+
3+
// #Medium #Top_100_Liked_Questions #Array #Backtracking #Algorithm_II_Day_10_Recursion_Backtracking
4+
// #Level_2_Day_20_Brute_Force/Backtracking #Udemy_Backtracking/Recursion
5+
// #2022_08_27_Time_317_ms_(86.85%)_Space_45.1_MB_(63.75%)
6+
7+
class Solution {
8+
fun combinationSum(candidates: IntArray, target: Int): List<List<Int>> {
9+
val result = mutableListOf<List<Int>>()
10+
11+
fun backtrack(start: Int, case: MutableList<Int>, sum: Int) {
12+
if (sum >= target) {
13+
if (sum == target) {
14+
result.add(case.toList())
15+
}
16+
return
17+
}
18+
19+
for (i in start until candidates.size) {
20+
case.add(candidates[i])
21+
backtrack(i, case, sum + candidates[i])
22+
case.removeAt(case.size - 1)
23+
}
24+
}
25+
26+
backtrack(0, mutableListOf(), 0)
27+
return result
28+
}
29+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
39\. Combination Sum
2+
3+
Medium
4+
5+
Given an array of **distinct** integers `candidates` and a target integer `target`, return _a list of all **unique combinations** of_ `candidates` _where the chosen numbers sum to_ `target`_._ You may return the combinations in **any order**.
6+
7+
The **same** number may be chosen from `candidates` an **unlimited number of times**. Two combinations are unique if the frequency of at least one of the chosen numbers is different.
8+
9+
It is **guaranteed** that the number of unique combinations that sum up to `target` is less than `150` combinations for the given input.
10+
11+
**Example 1:**
12+
13+
**Input:** candidates = [2,3,6,7], target = 7
14+
15+
**Output:** [[2,2,3],[7]]
16+
17+
**Explanation:**
18+
2 and 3 are candidates, and 2 + 2 + 3 = 7. Note that 2 can be used multiple times.
19+
20+
7 is a candidate, and 7 = 7.
21+
22+
These are the only two combinations.
23+
24+
**Example 2:**
25+
26+
**Input:** candidates = [2,3,5], target = 8
27+
28+
**Output:** [[2,2,2,2],[2,3,3],[3,5]]
29+
30+
**Example 3:**
31+
32+
**Input:** candidates = [2], target = 1
33+
34+
**Output:** []
35+
36+
**Constraints:**
37+
38+
* `1 <= candidates.length <= 30`
39+
* `1 <= candidates[i] <= 200`
40+
* All elements of `candidates` are **distinct**.
41+
* `1 <= target <= 500`
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package g0001_0100.s0041_first_missing_positive
2+
3+
// #Hard #Top_100_Liked_Questions #Top_Interview_Questions #Array #Hash_Table #Udemy_Arrays
4+
// #2022_08_27_Time_345_ms_(100.00%)_Space_47.3_MB_(100.00%)
5+
6+
class Solution {
7+
fun firstMissingPositive(nums: IntArray): Int {
8+
var noOne = true
9+
for (i in 0..(nums.size - 1)) {
10+
if (noOne && nums[i] == 1) {
11+
noOne = false
12+
} else if (nums[i] <= 0) {
13+
nums[i] = 1
14+
}
15+
}
16+
if (noOne) {
17+
return 1
18+
}
19+
var high = 0
20+
var k: Int
21+
for (x in nums) {
22+
k = kotlin.math.abs(x)
23+
high = kotlin.math.max(high, k)
24+
if (k - 1 < nums.size) {
25+
nums[k - 1] = -1 * kotlin.math.abs(nums[k - 1])
26+
}
27+
}
28+
for (i in nums.indices) {
29+
if (nums[i] > 0) {
30+
return i + 1
31+
}
32+
}
33+
return high + 1
34+
}
35+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
41\. First Missing Positive
2+
3+
Hard
4+
5+
Given an unsorted integer array `nums`, return the smallest missing positive integer.
6+
7+
You must implement an algorithm that runs in `O(n)` time and uses constant extra space.
8+
9+
**Example 1:**
10+
11+
**Input:** nums = [1,2,0]
12+
13+
**Output:** 3
14+
15+
**Explanation:** The numbers in the range [1,2] are all in the array.
16+
17+
**Example 2:**
18+
19+
**Input:** nums = [3,4,-1,1]
20+
21+
**Output:** 2
22+
23+
**Explanation:** 1 is in the array but 2 is missing.
24+
25+
**Example 3:**
26+
27+
**Input:** nums = [7,8,9,11,12]
28+
29+
**Output:** 1
30+
31+
**Explanation:** The smallest positive integer 1 is missing.
32+
33+
**Constraints:**
34+
35+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
36+
* <code>-2<sup>31</sup> <= nums[i] <= 2<sup>31</sup> - 1</code>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package g0001_0100.s0042_trapping_rain_water
2+
3+
// #Hard #Top_100_Liked_Questions #Top_Interview_Questions #Array #Dynamic_Programming #Two_Pointers
4+
// #Stack #Monotonic_Stack #Dynamic_Programming_I_Day_9 #Udemy_Two_Pointers
5+
// #2022_08_27_Time_196_ms_(100.00%)_Space_36.9_MB_(98.52%)
6+
7+
class Solution {
8+
fun trap(height: IntArray): Int {
9+
val size = height.size
10+
var maxLeft = 0
11+
var maxRight = 0
12+
var totalWater = 0
13+
var leftPtr = 0
14+
var rightPtr = size - 1
15+
16+
while (leftPtr < rightPtr) {
17+
if (height[leftPtr] <= height[rightPtr]) {
18+
if (maxLeft > height[leftPtr]) {
19+
totalWater = totalWater + maxLeft - height[leftPtr]
20+
} else {
21+
maxLeft = height[leftPtr]
22+
}
23+
++leftPtr
24+
} else {
25+
if (maxRight > height[rightPtr]) {
26+
totalWater = totalWater + maxRight - height[rightPtr]
27+
} else {
28+
maxRight = height[rightPtr]
29+
}
30+
--rightPtr
31+
}
32+
}
33+
34+
return totalWater
35+
}
36+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
42\. Trapping Rain Water
2+
3+
Hard
4+
5+
Given `n` non-negative integers representing an elevation map where the width of each bar is `1`, compute how much water it can trap after raining.
6+
7+
**Example 1:**
8+
9+
![](https://assets.leetcode.com/uploads/2018/10/22/rainwatertrap.png)
10+
11+
**Input:** height = [0,1,0,2,1,0,1,3,2,1,2,1]
12+
13+
**Output:** 6
14+
15+
**Explanation:** The above elevation map (black section) is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped.
16+
17+
**Example 2:**
18+
19+
**Input:** height = [4,2,0,3,2,5]
20+
21+
**Output:** 9
22+
23+
**Constraints:**
24+
25+
* `n == height.length`
26+
* <code>1 <= n <= 2 * 10<sup>4</sup></code>
27+
* <code>0 <= height[i] <= 10<sup>5</sup></code>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g0001_0100.s0045_jump_game_ii
2+
3+
// #Medium #Top_100_Liked_Questions #Array #Dynamic_Programming #Greedy
4+
// #Algorithm_II_Day_13_Dynamic_Programming #Dynamic_Programming_I_Day_4
5+
// #2022_08_27_Time_231_ms_(98.08%)_Space_36.8_MB_(100.00%)
6+
7+
class Solution {
8+
fun jump(nums: IntArray): Int {
9+
var jumps = 0
10+
var minJump = 0
11+
var maxJump = 0
12+
13+
while (maxJump < nums.lastIndex) {
14+
var nextJump = 0
15+
for (i in minJump..maxJump) nextJump = maxOf(nextJump, i + nums[i])
16+
minJump = maxJump + 1
17+
maxJump = nextJump
18+
jumps++
19+
}
20+
21+
return jumps
22+
}
23+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
45\. Jump Game II
2+
3+
Medium
4+
5+
Given an array of non-negative integers `nums`, you are initially positioned at the first index of the array.
6+
7+
Each element in the array represents your maximum jump length at that position.
8+
9+
Your goal is to reach the last index in the minimum number of jumps.
10+
11+
You can assume that you can always reach the last index.
12+
13+
**Example 1:**
14+
15+
**Input:** nums = [2,3,1,1,4]
16+
17+
**Output:** 2
18+
19+
**Explanation:** The minimum number of jumps to reach the last index is 2. Jump 1 step from index 0 to 1, then 3 steps to the last index.
20+
21+
**Example 2:**
22+
23+
**Input:** nums = [2,3,0,1,4]
24+
25+
**Output:** 2
26+
27+
**Constraints:**
28+
29+
* <code>1 <= nums.length <= 10<sup>4</sup></code>
30+
* `0 <= nums[i] <= 1000`
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package g0001_0100.s0039_combination_sum
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 combinationSum() {
10+
assertThat(Solution().combinationSum(intArrayOf(2, 3, 6, 7), 7), equalTo(arrayOf(intArrayOf(2, 2, 3).toList(), intArrayOf(7).toList()).toList()))
11+
}
12+
13+
@Test
14+
fun combinationSum2() {
15+
assertThat(Solution().combinationSum(intArrayOf(2, 3, 5), 8), equalTo(arrayOf(intArrayOf(2, 2, 2, 2).toList(), intArrayOf(2, 3, 3).toList(), intArrayOf(3, 5).toList()).toList()))
16+
}
17+
18+
@Test
19+
fun combinationSum3() {
20+
assertThat(Solution().combinationSum(intArrayOf(2), 1), equalTo(emptyArray<Int>().toList()))
21+
}
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package g0001_0100.s0041_first_missing_positive
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 firstMissingPositive() {
10+
assertThat(Solution().firstMissingPositive(intArrayOf(1, 2, 0)), equalTo(3))
11+
}
12+
13+
@Test
14+
fun firstMissingPositive2() {
15+
assertThat(Solution().firstMissingPositive(intArrayOf(3, 4, -1, 1)), equalTo(2))
16+
}
17+
18+
@Test
19+
fun firstMissingPositive3() {
20+
assertThat(Solution().firstMissingPositive(intArrayOf(7, 8, 9, 11, 12)), equalTo(1))
21+
}
22+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package g0001_0100.s0042_trapping_rain_water
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 trap() {
10+
assertThat(Solution().trap(intArrayOf(0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1)), equalTo(6))
11+
}
12+
13+
@Test
14+
fun trap2() {
15+
assertThat(Solution().trap(intArrayOf(4, 2, 0, 3, 2, 5)), equalTo(9))
16+
}
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package g0001_0100.s0045_jump_game_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 jump() {
10+
assertThat(Solution().jump(intArrayOf(2, 3, 1, 1, 4)), equalTo(2))
11+
}
12+
13+
@Test
14+
fun jump2() {
15+
assertThat(Solution().jump(intArrayOf(2, 3, 0, 1, 4)), equalTo(2))
16+
}
17+
}

0 commit comments

Comments
 (0)