Skip to content

Commit ab40ec6

Browse files
authored
Added tasks 401, 402, 403, 404.
1 parent cb42c8c commit ab40ec6

File tree

13 files changed

+424
-0
lines changed

13 files changed

+424
-0
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.7'
245245
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
246246
|-|-|-|-|-|-
247247
| 0104 |[Maximum Depth of Binary Tree](src.save/main/kotlin/g0101_0200/s0104_maximum_depth_of_binary_tree/Solution.kt)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree | 236 | 83.39
248+
| 0404 |[Sum of Left Leaves](src/main/kotlin/g0401_0500/s0404_sum_of_left_leaves/Solution.kt)| Easy | Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree | 173 | 86.05
248249

249250
#### Day 11 Containers and Libraries
250251

@@ -1618,6 +1619,10 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.7'
16181619
| 0438 |[Find All Anagrams in a String](src/main/kotlin/g0401_0500/s0438_find_all_anagrams_in_a_string/Solution.kt)| Medium | Top_100_Liked_Questions, String, Hash_Table, Sliding_Window, Algorithm_II_Day_5_Sliding_Window, Programming_Skills_II_Day_12, Level_1_Day_12_Sliding_Window/Two_Pointer | 561 | 54.68
16191620
| 0437 |[Path Sum III](src/main/kotlin/g0401_0500/s0437_path_sum_iii/Solution.kt)| Medium | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree, Level_2_Day_7_Tree | 403 | 54.12
16201621
| 0416 |[Partition Equal Subset Sum](src/main/kotlin/g0401_0500/s0416_partition_equal_subset_sum/Solution.kt)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Level_2_Day_13_Dynamic_Programming | 509 | 57.56
1622+
| 0404 |[Sum of Left Leaves](src/main/kotlin/g0401_0500/s0404_sum_of_left_leaves/Solution.kt)| Easy | Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Programming_Skills_I_Day_10_Linked_List_and_Tree | 173 | 86.05
1623+
| 0403 |[Frog Jump](src/main/kotlin/g0401_0500/s0403_frog_jump/Solution.kt)| Hard | Array, Dynamic_Programming | 240 | 100.00
1624+
| 0402 |[Remove K Digits](src/main/kotlin/g0401_0500/s0402_remove_k_digits/Solution.kt)| Medium | String, Greedy, Stack, Monotonic_Stack | 375 | 75.00
1625+
| 0401 |[Binary Watch](src/main/kotlin/g0401_0500/s0401_binary_watch/Solution.kt)| Easy | Bit_Manipulation, Backtracking | 266 | 71.43
16211626
| 0400 |[Nth Digit](src.save/main/kotlin/g0301_0400/s0400_nth_digit/Solution.kt)| Medium | Math, Binary_Search | 271 | 50.00
16221627
| 0399 |[Evaluate Division](src.save/main/kotlin/g0301_0400/s0399_evaluate_division/Solution.kt)| Medium | Array, Depth_First_Search, Breadth_First_Search, Graph, Union_Find, Shortest_Path | 183 | 91.49
16231628
| 0398 |[Random Pick Index](src.save/main/kotlin/g0301_0400/s0398_random_pick_index/Solution.kt)| Medium | Hash_Table, Math, Randomized, Reservoir_Sampling | 1091 | 75.00
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package g0401_0500.s0401_binary_watch
2+
3+
// #Easy #Bit_Manipulation #Backtracking #2022_11_30_Time_266_ms_(71.43%)_Space_34.9_MB_(100.00%)
4+
5+
import java.util.ArrayList
6+
7+
class Solution {
8+
fun readBinaryWatch(turnedOn: Int): List<String> {
9+
val times: MutableList<String> = ArrayList()
10+
for (hour in 0..11) {
11+
for (minutes in 0..59) {
12+
readBinaryWatchHelper(turnedOn, times, hour, minutes)
13+
}
14+
}
15+
return times
16+
}
17+
18+
private fun readBinaryWatchHelper(
19+
turnedOn: Int,
20+
selectedTimes: MutableList<String>,
21+
hour: Int,
22+
minutes: Int
23+
) {
24+
if (isValidTime(turnedOn, hour, minutes)) {
25+
selectedTimes.add(getTimeString(hour, minutes))
26+
}
27+
}
28+
29+
private fun getTimeString(hour: Int, minutes: Int): String {
30+
val time = StringBuilder()
31+
time.append(hour)
32+
time.append(':')
33+
if (minutes < 10) {
34+
time.append('0')
35+
}
36+
time.append(minutes)
37+
return time.toString()
38+
}
39+
40+
private fun isValidTime(turnedOn: Int, hour: Int, minutes: Int): Boolean {
41+
var hour = hour
42+
var minutes = minutes
43+
var counter = 0
44+
while (hour != 0) {
45+
if (hour and 1 == 1) {
46+
counter++
47+
}
48+
hour = hour ushr 1
49+
}
50+
if (counter > turnedOn) {
51+
return false
52+
}
53+
while (minutes != 0) {
54+
if (minutes and 1 == 1) {
55+
counter++
56+
}
57+
minutes = minutes ushr 1
58+
}
59+
return counter == turnedOn
60+
}
61+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
401\. Binary Watch
2+
3+
Easy
4+
5+
A binary watch has 4 LEDs on the top to represent the hours (0-11), and 6 LEDs on the bottom to represent the minutes (0-59). Each LED represents a zero or one, with the least significant bit on the right.
6+
7+
* For example, the below binary watch reads `"4:51"`.
8+
9+
![](https://assets.leetcode.com/uploads/2021/04/08/binarywatch.jpg)
10+
11+
Given an integer `turnedOn` which represents the number of LEDs that are currently on (ignoring the PM), return _all possible times the watch could represent_. You may return the answer in **any order**.
12+
13+
The hour must not contain a leading zero.
14+
15+
* For example, `"01:00"` is not valid. It should be `"1:00"`.
16+
17+
The minute must be consist of two digits and may contain a leading zero.
18+
19+
* For example, `"10:2"` is not valid. It should be `"10:02"`.
20+
21+
**Example 1:**
22+
23+
**Input:** turnedOn = 1
24+
25+
**Output:** ["0:01","0:02","0:04","0:08","0:16","0:32","1:00","2:00","4:00","8:00"]
26+
27+
**Example 2:**
28+
29+
**Input:** turnedOn = 9
30+
31+
**Output:** []
32+
33+
**Constraints:**
34+
35+
* `0 <= turnedOn <= 10`
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package g0401_0500.s0402_remove_k_digits
2+
3+
// #Medium #String #Greedy #Stack #Monotonic_Stack
4+
// #2022_12_01_Time_375_ms_(75.00%)_Space_42.8_MB_(66.67%)
5+
6+
class Solution {
7+
fun removeKdigits(num: String, k: Int): String {
8+
var k = k
9+
val list = CharArray(num.length)
10+
val len = num.length - k
11+
var top = 0
12+
for (i in 0 until num.length) {
13+
while (top > 0 && k > 0 && num[i] < list[top - 1]) {
14+
top--
15+
k--
16+
}
17+
list[top++] = num[i]
18+
}
19+
var number = 0
20+
while (number < len && list[number] == '0') {
21+
number++
22+
}
23+
return if (number == len) "0" else String(list, number, len - number)
24+
}
25+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
402\. Remove K Digits
2+
3+
Medium
4+
5+
Given string num representing a non-negative integer `num`, and an integer `k`, return _the smallest possible integer after removing_ `k` _digits from_ `num`.
6+
7+
**Example 1:**
8+
9+
**Input:** num = "1432219", k = 3
10+
11+
**Output:** "1219"
12+
13+
**Explanation:** Remove the three digits 4, 3, and 2 to form the new number 1219 which is the smallest.
14+
15+
**Example 2:**
16+
17+
**Input:** num = "10200", k = 1
18+
19+
**Output:** "200"
20+
21+
**Explanation:** Remove the leading 1 and the number is 200. Note that the output must not contain leading zeroes.
22+
23+
**Example 3:**
24+
25+
**Input:** num = "10", k = 2
26+
27+
**Output:** "0"
28+
29+
**Explanation:** Remove all the digits from the number and it is left with nothing which is 0.
30+
31+
**Constraints:**
32+
33+
* <code>1 <= k <= num.length <= 10<sup>5</sup></code>
34+
* `num` consists of only digits.
35+
* `num` does not have any leading zeros except for the zero itself.
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package g0401_0500.s0403_frog_jump
2+
3+
// #Hard #Array #Dynamic_Programming #2022_11_30_Time_240_ms_(100.00%)_Space_37.1_MB_(100.00%)
4+
5+
import java.util.HashMap
6+
import java.util.HashSet
7+
8+
class Solution {
9+
// global hashmap to store visited index -> set of jump lengths from that index
10+
private val visited: HashMap<Int, HashSet<Int>> = HashMap()
11+
fun canCross(stones: IntArray): Boolean {
12+
// a mathematical check before going in the recursion
13+
for (i in 3 until stones.size) {
14+
if (stones[i] > stones[i - 1] * 2) {
15+
return false
16+
}
17+
}
18+
// map of values -> index to make sure we get the next index quickly
19+
val rocks: HashMap<Int, Int> = HashMap()
20+
for (i in stones.indices) {
21+
rocks.put(stones[i], i)
22+
}
23+
return jump(stones, 0, 1, 0, rocks)
24+
}
25+
26+
private fun jump(
27+
stones: IntArray,
28+
index: Int,
29+
jumpLength: Int,
30+
expectedVal: Int,
31+
rocks: Map<Int, Int>
32+
): Boolean {
33+
// overshoot and going backwards not allowed
34+
if (index >= stones.size || jumpLength <= 0) {
35+
return false
36+
}
37+
// reached the last index
38+
if (index == stones.size - 1) {
39+
return expectedVal == stones[index]
40+
}
41+
// check if this index -> jumpLength pair was seen before, otherwise record it
42+
val rememberJumps: HashSet<Int> = visited.getOrDefault(index, HashSet())
43+
if (stones[index] > expectedVal || rememberJumps.contains(jumpLength)) {
44+
return false
45+
}
46+
rememberJumps.add(jumpLength)
47+
visited.put(index, rememberJumps)
48+
// check for jumpLength-1, jumpLength, jumpLength+1 for a new expected value
49+
return (
50+
jump(
51+
stones,
52+
rocks[stones[index] + jumpLength] ?: stones.size,
53+
jumpLength + 1,
54+
stones[index] + jumpLength,
55+
rocks
56+
) ||
57+
jump(
58+
stones,
59+
rocks[stones[index] + jumpLength] ?: stones.size,
60+
jumpLength,
61+
stones[index] + jumpLength,
62+
rocks
63+
) ||
64+
jump(
65+
stones,
66+
rocks[stones[index] + jumpLength] ?: stones.size,
67+
jumpLength - 1,
68+
stones[index] + jumpLength,
69+
rocks
70+
)
71+
)
72+
}
73+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
403\. Frog Jump
2+
3+
Hard
4+
5+
A frog is crossing a river. The river is divided into some number of units, and at each unit, there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.
6+
7+
Given a list of `stones`' positions (in units) in sorted **ascending order**, determine if the frog can cross the river by landing on the last stone. Initially, the frog is on the first stone and assumes the first jump must be `1` unit.
8+
9+
If the frog's last jump was `k` units, its next jump must be either `k - 1`, `k`, or `k + 1` units. The frog can only jump in the forward direction.
10+
11+
**Example 1:**
12+
13+
**Input:** stones = [0,1,3,5,6,8,12,17]
14+
15+
**Output:** true
16+
17+
**Explanation:** The frog can jump to the last stone by jumping 1 unit to the 2nd stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3 units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th stone.
18+
19+
**Example 2:**
20+
21+
**Input:** stones = [0,1,2,3,4,8,9,11]
22+
23+
**Output:** false
24+
25+
**Explanation:** There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large.
26+
27+
**Constraints:**
28+
29+
* `2 <= stones.length <= 2000`
30+
* <code>0 <= stones[i] <= 2<sup>31</sup> - 1</code>
31+
* `stones[0] == 0`
32+
* `stones` is sorted in a strictly increasing order.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package g0401_0500.s0404_sum_of_left_leaves
2+
3+
// #Easy #Depth_First_Search #Breadth_First_Search #Tree #Binary_Tree
4+
// #Programming_Skills_I_Day_10_Linked_List_and_Tree
5+
// #2022_12_01_Time_173_ms_(86.05%)_Space_35_MB_(25.58%)
6+
7+
import com_github_leetcode.TreeNode
8+
9+
/*
10+
* Example:
11+
* var ti = TreeNode(5)
12+
* var v = ti.`val`
13+
* Definition for a binary tree node.
14+
* class TreeNode(var `val`: Int) {
15+
* var left: TreeNode? = null
16+
* var right: TreeNode? = null
17+
* }
18+
*/
19+
class Solution {
20+
fun sumOfLeftLeaves(root: TreeNode): Int {
21+
fun dfs(root: TreeNode?, left: Boolean): Int {
22+
root ?: return 0
23+
if (root.left == null && root.right == null) {
24+
return if (left) {
25+
root.`val`
26+
} else {
27+
0
28+
}
29+
}
30+
return dfs(root.left, true) + dfs(root.right, false)
31+
}
32+
33+
return dfs(root, false)
34+
}
35+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
404\. Sum of Left Leaves
2+
3+
Easy
4+
5+
Given the `root` of a binary tree, return _the sum of all left leaves._
6+
7+
A **leaf** is a node with no children. A **left leaf** is a leaf that is the left child of another node.
8+
9+
**Example 1:**
10+
11+
![](https://assets.leetcode.com/uploads/2021/04/08/leftsum-tree.jpg)
12+
13+
**Input:** root = [3,9,20,null,null,15,7]
14+
15+
**Output:** 24
16+
17+
**Explanation:** There are two left leaves in the binary tree, with values 9 and 15 respectively.
18+
19+
**Example 2:**
20+
21+
**Input:** root = [1]
22+
23+
**Output:** 0
24+
25+
**Constraints:**
26+
27+
* The number of nodes in the tree is in the range `[1, 1000]`.
28+
* `-1000 <= Node.val <= 1000`
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package g0401_0500.s0401_binary_watch
2+
3+
import org.hamcrest.CoreMatchers.equalTo
4+
import org.hamcrest.MatcherAssert.assertThat
5+
import org.junit.jupiter.api.Test
6+
import java.util.Arrays
7+
8+
internal class SolutionTest {
9+
@Test
10+
fun readBinaryWatch() {
11+
assertThat(
12+
Solution().readBinaryWatch(1),
13+
equalTo(
14+
Arrays.asList(
15+
"0:01", "0:02", "0:04", "0:08", "0:16", "0:32", "1:00", "2:00",
16+
"4:00", "8:00"
17+
)
18+
)
19+
)
20+
}
21+
22+
@Test
23+
fun readBinaryWatch2() {
24+
assertThat(
25+
Solution().readBinaryWatch(9),
26+
equalTo(
27+
Arrays.asList()
28+
)
29+
)
30+
}
31+
}

0 commit comments

Comments
 (0)