Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions README.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ Your code will **only** be given the `head` of the original linked list.
## Solution

```kotlin
import com_github_leetcode.Node
import com_github_leetcode.random.Node

class Solution {
fun copyRandomList(head: Node?): Node? {
Expand Down
101 changes: 101 additions & 0 deletions src/main/kotlin/g0401_0500/s0401_binary_watch/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
[![](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)

## 401\. Binary Watch

Easy

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.

* For example, the below binary watch reads `"4:51"`.

![](https://assets.leetcode.com/uploads/2021/04/08/binarywatch.jpg)

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**.

The hour must not contain a leading zero.

* For example, `"01:00"` is not valid. It should be `"1:00"`.

The minute must be consist of two digits and may contain a leading zero.

* For example, `"10:2"` is not valid. It should be `"10:02"`.

**Example 1:**

**Input:** turnedOn = 1

**Output:** ["0:01","0:02","0:04","0:08","0:16","0:32","1:00","2:00","4:00","8:00"]

**Example 2:**

**Input:** turnedOn = 9

**Output:** []

**Constraints:**

* `0 <= turnedOn <= 10`

## Solution

```kotlin
import java.util.ArrayList

@Suppress("NAME_SHADOWING")
class Solution {
fun readBinaryWatch(turnedOn: Int): List<String> {
val times: MutableList<String> = ArrayList()
for (hour in 0..11) {
for (minutes in 0..59) {
readBinaryWatchHelper(turnedOn, times, hour, minutes)
}
}
return times
}

private fun readBinaryWatchHelper(
turnedOn: Int,
selectedTimes: MutableList<String>,
hour: Int,
minutes: Int
) {
if (isValidTime(turnedOn, hour, minutes)) {
selectedTimes.add(getTimeString(hour, minutes))
}
}

private fun getTimeString(hour: Int, minutes: Int): String {
val time = StringBuilder()
time.append(hour)
time.append(':')
if (minutes < 10) {
time.append('0')
}
time.append(minutes)
return time.toString()
}

private fun isValidTime(turnedOn: Int, hour: Int, minutes: Int): Boolean {
var hour = hour
var minutes = minutes
var counter = 0
while (hour != 0) {
if (hour and 1 == 1) {
counter++
}
hour = hour ushr 1
}
if (counter > turnedOn) {
return false
}
while (minutes != 0) {
if (minutes and 1 == 1) {
counter++
}
minutes = minutes ushr 1
}
return counter == turnedOn
}
}
```
64 changes: 64 additions & 0 deletions src/main/kotlin/g0401_0500/s0402_remove_k_digits/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
[![](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)

## 402\. Remove K Digits

Medium

Given string num representing a non-negative integer `num`, and an integer `k`, return _the smallest possible integer after removing_ `k` _digits from_ `num`.

**Example 1:**

**Input:** num = "1432219", k = 3

**Output:** "1219"

**Explanation:** Remove the three digits 4, 3, and 2 to form the new number 1219 which is the smallest.

**Example 2:**

**Input:** num = "10200", k = 1

**Output:** "200"

**Explanation:** Remove the leading 1 and the number is 200. Note that the output must not contain leading zeroes.

**Example 3:**

**Input:** num = "10", k = 2

**Output:** "0"

**Explanation:** Remove all the digits from the number and it is left with nothing which is 0.

**Constraints:**

* <code>1 <= k <= num.length <= 10<sup>5</sup></code>
* `num` consists of only digits.
* `num` does not have any leading zeros except for the zero itself.

## Solution

```kotlin
@Suppress("NAME_SHADOWING")
class Solution {
fun removeKdigits(num: String, k: Int): String {
var k = k
val list = CharArray(num.length)
val len = num.length - k
var top = 0
for (i in 0 until num.length) {
while (top > 0 && k > 0 && num[i] < list[top - 1]) {
top--
k--
}
list[top++] = num[i]
}
var number = 0
while (number < len && list[number] == '0') {
number++
}
return if (number == len) "0" else String(list, number, len - number)
}
}
```
109 changes: 109 additions & 0 deletions src/main/kotlin/g0401_0500/s0403_frog_jump/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
[![](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)

## 403\. Frog Jump

Hard

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.

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.

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.

**Example 1:**

**Input:** stones = [0,1,3,5,6,8,12,17]

**Output:** true

**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.

**Example 2:**

**Input:** stones = [0,1,2,3,4,8,9,11]

**Output:** false

**Explanation:** There is no way to jump to the last stone as the gap between the 5th and 6th stone is too large.

**Constraints:**

* `2 <= stones.length <= 2000`
* <code>0 <= stones[i] <= 2<sup>31</sup> - 1</code>
* `stones[0] == 0`
* `stones` is sorted in a strictly increasing order.

## Solution

```kotlin
import java.util.HashMap
import java.util.HashSet

class Solution {
// global hashmap to store visited index -> set of jump lengths from that index
private val visited: HashMap<Int, HashSet<Int>> = HashMap()
fun canCross(stones: IntArray): Boolean {
// a mathematical check before going in the recursion
for (i in 3 until stones.size) {
if (stones[i] > stones[i - 1] * 2) {
return false
}
}
// map of values -> index to make sure we get the next index quickly
val rocks: HashMap<Int, Int> = HashMap()
for (i in stones.indices) {
rocks.put(stones[i], i)
}
return jump(stones, 0, 1, 0, rocks)
}

private fun jump(
stones: IntArray,
index: Int,
jumpLength: Int,
expectedVal: Int,
rocks: Map<Int, Int>
): Boolean {
// overshoot and going backwards not allowed
if (index >= stones.size || jumpLength <= 0) {
return false
}
// reached the last index
if (index == stones.size - 1) {
return expectedVal == stones[index]
}
// check if this index -> jumpLength pair was seen before, otherwise record it
val rememberJumps: HashSet<Int> = visited.getOrDefault(index, HashSet())
if (stones[index] > expectedVal || rememberJumps.contains(jumpLength)) {
return false
}
rememberJumps.add(jumpLength)
visited.put(index, rememberJumps)
// check for jumpLength-1, jumpLength, jumpLength+1 for a new expected value
return (
jump(
stones,
rocks[stones[index] + jumpLength] ?: stones.size,
jumpLength + 1,
stones[index] + jumpLength,
rocks
) ||
jump(
stones,
rocks[stones[index] + jumpLength] ?: stones.size,
jumpLength,
stones[index] + jumpLength,
rocks
) ||
jump(
stones,
rocks[stones[index] + jumpLength] ?: stones.size,
jumpLength - 1,
stones[index] + jumpLength,
rocks
)
)
}
}
```
65 changes: 65 additions & 0 deletions src/main/kotlin/g0401_0500/s0404_sum_of_left_leaves/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
[![](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)

## 404\. Sum of Left Leaves

Easy

Given the `root` of a binary tree, return _the sum of all left leaves._

A **leaf** is a node with no children. A **left leaf** is a leaf that is the left child of another node.

**Example 1:**

![](https://assets.leetcode.com/uploads/2021/04/08/leftsum-tree.jpg)

**Input:** root = [3,9,20,null,null,15,7]

**Output:** 24

**Explanation:** There are two left leaves in the binary tree, with values 9 and 15 respectively.

**Example 2:**

**Input:** root = [1]

**Output:** 0

**Constraints:**

* The number of nodes in the tree is in the range `[1, 1000]`.
* `-1000 <= Node.val <= 1000`

## Solution

```kotlin
import com_github_leetcode.TreeNode

/*
* Example:
* var ti = TreeNode(5)
* var v = ti.`val`
* Definition for a binary tree node.
* class TreeNode(var `val`: Int) {
* var left: TreeNode? = null
* var right: TreeNode? = null
* }
*/
class Solution {
fun sumOfLeftLeaves(root: TreeNode): Int {
fun dfs(root: TreeNode?, left: Boolean): Int {
root ?: return 0
if (root.left == null && root.right == null) {
return if (left) {
root.`val`
} else {
0
}
}
return dfs(root.left, true) + dfs(root.right, false)
}

return dfs(root, false)
}
}
```
Loading