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
66 changes: 66 additions & 0 deletions README.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class StockSpanner {
if (stocks.size == 1) {
return index - stocks.peek()
}
while (stocks.size > 1 && map[stocks.peek()]!! <= price) {
while (stocks.size > 1 && map.getValue(stocks.peek()) <= price) {
stocks.pop()
}
return index - stocks.peek()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class Solution {
queue.add(1)
visited[0] = true
var step = 0
while (!queue.isEmpty()) {
while (queue.isNotEmpty()) {
val queueSize = queue.size
for (i in 0 until queueSize) {
val previousLabel = queue.poll()
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/g0901_1000/s0913_cat_and_mouse/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class Solution {
q.offer(intArrayOf(0, i, CAT, MOUSE_WIN))
q.offer(intArrayOf(i, i, CAT, CAT_WIN))
}
while (!q.isEmpty()) {
while (q.isNotEmpty()) {
val state = q.poll()
val mouse = state[0]
val cat = state[1]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,12 @@ class Solution {
val map: HashMap<Int, Int> = HashMap()
for (j in deck) {
if (map.containsKey(j)) {
map[j] = map[j]!! + 1
map[j] = map.getValue(j) + 1
} else {
map[j] = 1
}
}
var x = map[deck[0]]!!

var x = map.getValue(deck[0])
for (entry in map.entries.iterator()) {
x = gcd(x, entry.value)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class CBTInserter(root: TreeNode?) {
private fun addToQueue() {
val hlq: Queue<TreeNode> = LinkedList()
hlq.add(head)
while (!hlq.isEmpty()) {
while (hlq.isNotEmpty()) {
var size = hlq.size
while (size-- > 0) {
val poll: TreeNode = hlq.poll()
Expand Down Expand Up @@ -97,7 +97,7 @@ class CBTInserter(root: TreeNode?) {
}

private fun deleteFullNode() {
while (!q.isEmpty()) {
while (q.isNotEmpty()) {
val peek: TreeNode = q.peek()
if (peek.left != null && peek.right != null) {
q.poll()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class Solution {
val stack: Deque<Char> = LinkedList()
for (c in s.toCharArray()) {
if (c == ')') {
if (!stack.isEmpty() && stack.peek() == '(') {
if (stack.isNotEmpty() && stack.peek() == '(') {
stack.pop()
} else {
stack.push(c)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class Solution {
visited!!.add(initial[i])
}
}
while (!q.isEmpty()) {
while (q.isNotEmpty()) {
val curr = q.poll()
if (curr != initial[ind]) {
count++
Expand All @@ -87,7 +87,7 @@ class Solution {
adj.putIfAbsent(i, ArrayList())
for (j in 0 until n) {
if (graph[i][j] == 1) {
adj[i]!!.add(j)
adj.getValue(i).add(j)
}
}
}
Expand All @@ -96,7 +96,7 @@ class Solution {
var node = initial[0]
for (i in initial.indices) {
visited = HashSet()
val children = adj[initial[i]]!!
val children = adj.getValue(initial[i])
adj.remove(initial[i])
bfs(i, initial)
if (count < min) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/g0901_1000/s0934_shortest_bridge/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class Solution {
i++
}
var level = -1
while (!q.isEmpty()) {
while (q.isNotEmpty()) {
var size: Int = q.size
level++
while (size-- > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class Solution {
val map: MutableMap<Int, MutableSet<Int>> = HashMap()
for (p in points) {
map.putIfAbsent(p[0], HashSet())
map[p[0]]!!.add(p[1])
map.getValue(p[0]).add(p[1])
}
Arrays.sort(
points
Expand All @@ -65,7 +65,7 @@ class Solution {
if (area >= min || area == 0) {
continue
}
if (map[p1[0]]!!.contains(p2[1]) && map[p2[0]]!!.contains(p1[1])) {
if (map.getValue(p1[0]).contains(p2[1]) && map.getValue(p2[0]).contains(p1[1])) {
min = area
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,15 @@ class Solution {
if (pushed[i] == popped[j]) {
i++
j++
} else if (!stack.isEmpty() && stack.peek() == popped[j]) {
} else if (stack.isNotEmpty() && stack.peek() == popped[j]) {
stack.pop()
j++
} else {
stack.push(pushed[i++])
}
}
while (j < len) {
if (!stack.isEmpty() && stack.peek() != popped[j++]) {
if (stack.isNotEmpty() && stack.peek() != popped[j++]) {
return false
} else {
stack.pop()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class Solution {
queue.add(root.left)
queue.add(root.right)
var seenNull = false
while (!queue.isEmpty()) {
while (queue.isNotEmpty()) {
val node: TreeNode? = queue.poll()
if (node == null) {
seenNull = true
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
[![](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)

## 960\. Delete Columns to Make Sorted III

Hard

You are given an array of `n` strings `strs`, all of the same length.

We may choose any deletion indices, and we delete all the characters in those indices for each string.

For example, if we have `strs = ["abcdef","uvwxyz"]` and deletion indices `{0, 2, 3}`, then the final array after deletions is `["bef", "vyz"]`.

Suppose we chose a set of deletion indices `answer` such that after deletions, the final array has **every string (row) in lexicographic** order. (i.e., `(strs[0][0] <= strs[0][1] <= ... <= strs[0][strs[0].length - 1])`, and `(strs[1][0] <= strs[1][1] <= ... <= strs[1][strs[1].length - 1])`, and so on). Return _the minimum possible value of_ `answer.length`.

**Example 1:**

**Input:** strs = ["babca","bbazb"]

**Output:** 3

**Explanation:** After deleting columns 0, 1, and 4, the final array is strs = ["bc", "az"]. Both these rows are individually in lexicographic order (ie. strs[0][0] <= strs[0][1] and strs[1][0] <= strs[1][1]). Note that strs[0] > strs[1] - the array strs is not necessarily in lexicographic order.

**Example 2:**

**Input:** strs = ["edcba"]

**Output:** 4

**Explanation:** If we delete less than 4 columns, the only row will not be lexicographically sorted.

**Example 3:**

**Input:** strs = ["ghi","def","abc"]

**Output:** 0

**Explanation:** All rows are already lexicographically sorted.

**Constraints:**

* `n == strs.length`
* `1 <= n <= 100`
* `1 <= strs[i].length <= 100`
* `strs[i]` consists of lowercase English letters.

## Solution

```kotlin
class Solution {
fun minDeletionSize(strs: Array<String>): Int {
val n = strs[0].length
val dp = Array(n + 1) { IntArray(2) }
for (i in 1..n) {
dp[i][0] = 1 + dp[i - 1][0].coerceAtMost(dp[i - 1][1])
var min = i - 1
var j: Int = i - 1
while (j > 0) {
var lexico = true
for (str in strs) {
if (str[i - 1] < str[j - 1]) {
lexico = false
break
}
}
if (lexico) {
min = min.coerceAtMost(dp[j][1] + i - j - 1)
}
j--
}
dp[i][1] = min
}
return dp[n][0].coerceAtMost(dp[n][1])
}
}
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
[![](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)

## 961\. N-Repeated Element in Size 2N Array

Easy

You are given an integer array `nums` with the following properties:

* `nums.length == 2 * n`.
* `nums` contains `n + 1` **unique** elements.
* Exactly one element of `nums` is repeated `n` times.

Return _the element that is repeated_ `n` _times_.

**Example 1:**

**Input:** nums = [1,2,3,3]

**Output:** 3

**Example 2:**

**Input:** nums = [2,1,2,5,3,2]

**Output:** 2

**Example 3:**

**Input:** nums = [5,1,5,2,5,3,5,4]

**Output:** 5

**Constraints:**

* `2 <= n <= 5000`
* `nums.length == 2 * n`
* <code>0 <= nums[i] <= 10<sup>4</sup></code>
* `nums` contains `n + 1` **unique** elements and one of them is repeated exactly `n` times.

## Solution

```kotlin
class Solution {
fun repeatedNTimes(nums: IntArray): Int {
val hs: HashSet<Int> = HashSet()
for (x in nums) {
if (!hs.contains(x)) {
hs.add(x)
} else {
return x
}
}
return 1
}
}
```
56 changes: 56 additions & 0 deletions src/main/kotlin/g0901_1000/s0962_maximum_width_ramp/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
[![](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)

## 962\. Maximum Width Ramp

Medium

A **ramp** in an integer array `nums` is a pair `(i, j)` for which `i < j` and `nums[i] <= nums[j]`. The **width** of such a ramp is `j - i`.

Given an integer array `nums`, return _the maximum width of a **ramp** in_ `nums`. If there is no **ramp** in `nums`, return `0`.

**Example 1:**

**Input:** nums = [6,0,8,2,1,5]

**Output:** 4

**Explanation:** The maximum width ramp is achieved at (i, j) = (1, 5): nums[1] = 0 and nums[5] = 5.

**Example 2:**

**Input:** nums = [9,8,1,0,1,9,4,0,4,1]

**Output:** 7

**Explanation:** The maximum width ramp is achieved at (i, j) = (2, 9): nums[2] = 1 and nums[9] = 1.

**Constraints:**

* <code>2 <= nums.length <= 5 * 10<sup>4</sup></code>
* <code>0 <= nums[i] <= 5 * 10<sup>4</sup></code>

## Solution

```kotlin
class Solution {
fun maxWidthRamp(nums: IntArray): Int {
val m = nums.size
val dp = IntArray(m)
var minInd = 0
var ramp = 0
for (i in 0 until m) {
var prInd = minInd
while (prInd > 0 && nums[i] >= nums[dp[prInd]]) {
prInd = dp[prInd]
}
dp[i] = prInd
if (nums[i] >= nums[prInd]) {
ramp = ramp.coerceAtLeast(i - prInd)
}
minInd = if (nums[i] < nums[minInd]) i else minInd
}
return ramp
}
}
```
Loading