Skip to content

Commit df4bcca

Browse files
authored
Added tasks 401-453.
1 parent ff8c291 commit df4bcca

File tree

42 files changed

+3096
-1
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+3096
-1
lines changed

README.md

Lines changed: 62 additions & 0 deletions
Large diffs are not rendered by default.

src/main/kotlin/g0101_0200/s0138_copy_list_with_random_pointer/readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ Your code will **only** be given the `head` of the original linked list.
5353
## Solution
5454

5555
```kotlin
56-
import com_github_leetcode.Node
56+
import com_github_leetcode.random.Node
5757

5858
class Solution {
5959
fun copyRandomList(head: Node?): Node? {
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
3+
4+
## 401\. Binary Watch
5+
6+
Easy
7+
8+
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.
9+
10+
* For example, the below binary watch reads `"4:51"`.
11+
12+
![](https://assets.leetcode.com/uploads/2021/04/08/binarywatch.jpg)
13+
14+
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**.
15+
16+
The hour must not contain a leading zero.
17+
18+
* For example, `"01:00"` is not valid. It should be `"1:00"`.
19+
20+
The minute must be consist of two digits and may contain a leading zero.
21+
22+
* For example, `"10:2"` is not valid. It should be `"10:02"`.
23+
24+
**Example 1:**
25+
26+
**Input:** turnedOn = 1
27+
28+
**Output:** ["0:01","0:02","0:04","0:08","0:16","0:32","1:00","2:00","4:00","8:00"]
29+
30+
**Example 2:**
31+
32+
**Input:** turnedOn = 9
33+
34+
**Output:** []
35+
36+
**Constraints:**
37+
38+
* `0 <= turnedOn <= 10`
39+
40+
## Solution
41+
42+
```kotlin
43+
import java.util.ArrayList
44+
45+
@Suppress("NAME_SHADOWING")
46+
class Solution {
47+
fun readBinaryWatch(turnedOn: Int): List<String> {
48+
val times: MutableList<String> = ArrayList()
49+
for (hour in 0..11) {
50+
for (minutes in 0..59) {
51+
readBinaryWatchHelper(turnedOn, times, hour, minutes)
52+
}
53+
}
54+
return times
55+
}
56+
57+
private fun readBinaryWatchHelper(
58+
turnedOn: Int,
59+
selectedTimes: MutableList<String>,
60+
hour: Int,
61+
minutes: Int
62+
) {
63+
if (isValidTime(turnedOn, hour, minutes)) {
64+
selectedTimes.add(getTimeString(hour, minutes))
65+
}
66+
}
67+
68+
private fun getTimeString(hour: Int, minutes: Int): String {
69+
val time = StringBuilder()
70+
time.append(hour)
71+
time.append(':')
72+
if (minutes < 10) {
73+
time.append('0')
74+
}
75+
time.append(minutes)
76+
return time.toString()
77+
}
78+
79+
private fun isValidTime(turnedOn: Int, hour: Int, minutes: Int): Boolean {
80+
var hour = hour
81+
var minutes = minutes
82+
var counter = 0
83+
while (hour != 0) {
84+
if (hour and 1 == 1) {
85+
counter++
86+
}
87+
hour = hour ushr 1
88+
}
89+
if (counter > turnedOn) {
90+
return false
91+
}
92+
while (minutes != 0) {
93+
if (minutes and 1 == 1) {
94+
counter++
95+
}
96+
minutes = minutes ushr 1
97+
}
98+
return counter == turnedOn
99+
}
100+
}
101+
```
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
3+
4+
## 402\. Remove K Digits
5+
6+
Medium
7+
8+
Given string num representing a non-negative integer `num`, and an integer `k`, return _the smallest possible integer after removing_ `k` _digits from_ `num`.
9+
10+
**Example 1:**
11+
12+
**Input:** num = "1432219", k = 3
13+
14+
**Output:** "1219"
15+
16+
**Explanation:** Remove the three digits 4, 3, and 2 to form the new number 1219 which is the smallest.
17+
18+
**Example 2:**
19+
20+
**Input:** num = "10200", k = 1
21+
22+
**Output:** "200"
23+
24+
**Explanation:** Remove the leading 1 and the number is 200. Note that the output must not contain leading zeroes.
25+
26+
**Example 3:**
27+
28+
**Input:** num = "10", k = 2
29+
30+
**Output:** "0"
31+
32+
**Explanation:** Remove all the digits from the number and it is left with nothing which is 0.
33+
34+
**Constraints:**
35+
36+
* <code>1 <= k <= num.length <= 10<sup>5</sup></code>
37+
* `num` consists of only digits.
38+
* `num` does not have any leading zeros except for the zero itself.
39+
40+
## Solution
41+
42+
```kotlin
43+
@Suppress("NAME_SHADOWING")
44+
class Solution {
45+
fun removeKdigits(num: String, k: Int): String {
46+
var k = k
47+
val list = CharArray(num.length)
48+
val len = num.length - k
49+
var top = 0
50+
for (i in 0 until num.length) {
51+
while (top > 0 && k > 0 && num[i] < list[top - 1]) {
52+
top--
53+
k--
54+
}
55+
list[top++] = num[i]
56+
}
57+
var number = 0
58+
while (number < len && list[number] == '0') {
59+
number++
60+
}
61+
return if (number == len) "0" else String(list, number, len - number)
62+
}
63+
}
64+
```
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
3+
4+
## 403\. Frog Jump
5+
6+
Hard
7+
8+
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.
9+
10+
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.
11+
12+
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.
13+
14+
**Example 1:**
15+
16+
**Input:** stones = [0,1,3,5,6,8,12,17]
17+
18+
**Output:** true
19+
20+
**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.
21+
22+
**Example 2:**
23+
24+
**Input:** stones = [0,1,2,3,4,8,9,11]
25+
26+
**Output:** false
27+
28+
**Explanation:** There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large.
29+
30+
**Constraints:**
31+
32+
* `2 <= stones.length <= 2000`
33+
* <code>0 <= stones[i] <= 2<sup>31</sup> - 1</code>
34+
* `stones[0] == 0`
35+
* `stones` is sorted in a strictly increasing order.
36+
37+
## Solution
38+
39+
```kotlin
40+
import java.util.HashMap
41+
import java.util.HashSet
42+
43+
class Solution {
44+
// global hashmap to store visited index -> set of jump lengths from that index
45+
private val visited: HashMap<Int, HashSet<Int>> = HashMap()
46+
fun canCross(stones: IntArray): Boolean {
47+
// a mathematical check before going in the recursion
48+
for (i in 3 until stones.size) {
49+
if (stones[i] > stones[i - 1] * 2) {
50+
return false
51+
}
52+
}
53+
// map of values -> index to make sure we get the next index quickly
54+
val rocks: HashMap<Int, Int> = HashMap()
55+
for (i in stones.indices) {
56+
rocks.put(stones[i], i)
57+
}
58+
return jump(stones, 0, 1, 0, rocks)
59+
}
60+
61+
private fun jump(
62+
stones: IntArray,
63+
index: Int,
64+
jumpLength: Int,
65+
expectedVal: Int,
66+
rocks: Map<Int, Int>
67+
): Boolean {
68+
// overshoot and going backwards not allowed
69+
if (index >= stones.size || jumpLength <= 0) {
70+
return false
71+
}
72+
// reached the last index
73+
if (index == stones.size - 1) {
74+
return expectedVal == stones[index]
75+
}
76+
// check if this index -> jumpLength pair was seen before, otherwise record it
77+
val rememberJumps: HashSet<Int> = visited.getOrDefault(index, HashSet())
78+
if (stones[index] > expectedVal || rememberJumps.contains(jumpLength)) {
79+
return false
80+
}
81+
rememberJumps.add(jumpLength)
82+
visited.put(index, rememberJumps)
83+
// check for jumpLength-1, jumpLength, jumpLength+1 for a new expected value
84+
return (
85+
jump(
86+
stones,
87+
rocks[stones[index] + jumpLength] ?: stones.size,
88+
jumpLength + 1,
89+
stones[index] + jumpLength,
90+
rocks
91+
) ||
92+
jump(
93+
stones,
94+
rocks[stones[index] + jumpLength] ?: stones.size,
95+
jumpLength,
96+
stones[index] + jumpLength,
97+
rocks
98+
) ||
99+
jump(
100+
stones,
101+
rocks[stones[index] + jumpLength] ?: stones.size,
102+
jumpLength - 1,
103+
stones[index] + jumpLength,
104+
rocks
105+
)
106+
)
107+
}
108+
}
109+
```
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
3+
4+
## 404\. Sum of Left Leaves
5+
6+
Easy
7+
8+
Given the `root` of a binary tree, return _the sum of all left leaves._
9+
10+
A **leaf** is a node with no children. A **left leaf** is a leaf that is the left child of another node.
11+
12+
**Example 1:**
13+
14+
![](https://assets.leetcode.com/uploads/2021/04/08/leftsum-tree.jpg)
15+
16+
**Input:** root = [3,9,20,null,null,15,7]
17+
18+
**Output:** 24
19+
20+
**Explanation:** There are two left leaves in the binary tree, with values 9 and 15 respectively.
21+
22+
**Example 2:**
23+
24+
**Input:** root = [1]
25+
26+
**Output:** 0
27+
28+
**Constraints:**
29+
30+
* The number of nodes in the tree is in the range `[1, 1000]`.
31+
* `-1000 <= Node.val <= 1000`
32+
33+
## Solution
34+
35+
```kotlin
36+
import com_github_leetcode.TreeNode
37+
38+
/*
39+
* Example:
40+
* var ti = TreeNode(5)
41+
* var v = ti.`val`
42+
* Definition for a binary tree node.
43+
* class TreeNode(var `val`: Int) {
44+
* var left: TreeNode? = null
45+
* var right: TreeNode? = null
46+
* }
47+
*/
48+
class Solution {
49+
fun sumOfLeftLeaves(root: TreeNode): Int {
50+
fun dfs(root: TreeNode?, left: Boolean): Int {
51+
root ?: return 0
52+
if (root.left == null && root.right == null) {
53+
return if (left) {
54+
root.`val`
55+
} else {
56+
0
57+
}
58+
}
59+
return dfs(root.left, true) + dfs(root.right, false)
60+
}
61+
62+
return dfs(root, false)
63+
}
64+
}
65+
```

0 commit comments

Comments
 (0)