Skip to content

Commit 2a87964

Browse files
authored
Added tasks 944, 945, 946, 947
1 parent c138ef9 commit 2a87964

File tree

13 files changed

+439
-0
lines changed

13 files changed

+439
-0
lines changed

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.12'
355355
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
356356
|-|-|-|-|-|-
357357
| 0547 |[Number of Provinces](src/main/kotlin/g0501_0600/s0547_number_of_provinces/Solution.kt)| Medium | Depth_First_Search, Breadth_First_Search, Graph, Union_Find | 229 | 79.73
358+
| 0947 |[Most Stones Removed with Same Row or Column](src/main/kotlin/g0901_1000/s0947_most_stones_removed_with_same_row_or_column/Solution.kt)| Medium | Depth_First_Search, Graph, Union_Find | 200 | 100.00
358359

359360
#### Day 20 Brute Force/Backtracking
360361

@@ -1733,6 +1734,10 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.12'
17331734
|------|----------------|-------------|-------------|----------|---------
17341735
| 1143 |[Longest Common Subsequence](src/main/kotlin/g1101_1200/s1143_longest_common_subsequence/Solution.kt)| Medium | Top_100_Liked_Questions, String, Dynamic_Programming, Algorithm_II_Day_17_Dynamic_Programming, Dynamic_Programming_I_Day_19, Udemy_Dynamic_Programming | 307 | 38.36
17351736
| 0994 |[Rotting Oranges](src/main/kotlin/g0901_1000/s0994_rotting_oranges/Solution.kt)| Medium | Array, Breadth_First_Search, Matrix, Algorithm_I_Day_9_Breadth_First_Search_Depth_First_Search, Level_2_Day_10_Graph/BFS/DFS | 308 | 57.93
1737+
| 0947 |[Most Stones Removed with Same Row or Column](src/main/kotlin/g0901_1000/s0947_most_stones_removed_with_same_row_or_column/Solution.kt)| Medium | Depth_First_Search, Graph, Union_Find, Level_2_Day_19_Union_Find | 200 | 100.00
1738+
| 0946 |[Validate Stack Sequences](src/main/kotlin/g0901_1000/s0946_validate_stack_sequences/Solution.kt)| Medium | Array, Stack, Simulation | 180 | 74.91
1739+
| 0945 |[Minimum Increment to Make Array Unique](src/main/kotlin/g0901_1000/s0945_minimum_increment_to_make_array_unique/Solution.kt)| Medium | Array, Sorting, Greedy, Counting | 427 | 100.00
1740+
| 0944 |[Delete Columns to Make Sorted](src/main/kotlin/g0901_1000/s0944_delete_columns_to_make_sorted/Solution.kt)| Easy | Array, String | 221 | 75.00
17361741
| 0943 |[Find the Shortest Superstring](src/main/kotlin/g0901_1000/s0943_find_the_shortest_superstring/Solution.kt)| Hard | Array, String, Dynamic_Programming, Bit_Manipulation, Bitmask | 1290 | 100.00
17371742
| 0942 |[DI String Match](src/main/kotlin/g0901_1000/s0942_di_string_match/Solution.kt)| Easy | Array, String, Math, Greedy, Two_Pointers | 202 | 80.00
17381743
| 0941 |[Valid Mountain Array](src/main/kotlin/g0901_1000/s0941_valid_mountain_array/Solution.kt)| Easy | Array | 251 | 51.94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package g0901_1000.s0944_delete_columns_to_make_sorted
2+
3+
// #Easy #Array #String #2023_04_30_Time_221_ms_(75.00%)_Space_48.7_MB_(6.25%)
4+
5+
class Solution {
6+
fun minDeletionSize(strs: Array<String>): Int {
7+
var deleted = 0
8+
for (i in 0 until strs[0].length) {
9+
var last = ' '
10+
for (str in strs) {
11+
if (str[i] < last) {
12+
deleted++
13+
break
14+
}
15+
last = str[i]
16+
}
17+
}
18+
return deleted
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
944\. Delete Columns to Make Sorted
2+
3+
Easy
4+
5+
You are given an array of `n` strings `strs`, all of the same length.
6+
7+
The strings can be arranged such that there is one on each line, making a grid.
8+
9+
* For example, `strs = ["abc", "bce", "cae"]` can be arranged as follows:
10+
11+
abc bce cae
12+
13+
You want to **delete** the columns that are **not sorted lexicographically**. In the above example (**0-indexed**), columns 0 (`'a'`, `'b'`, `'c'`) and 2 (`'c'`, `'e'`, `'e'`) are sorted, while column 1 (`'b'`, `'c'`, `'a'`) is not, so you would delete column 1.
14+
15+
Return _the number of columns that you will delete_.
16+
17+
**Example 1:**
18+
19+
**Input:** strs = ["cba","daf","ghi"]
20+
21+
**Output:** 1
22+
23+
**Explanation:** The grid looks as follows:
24+
25+
cba
26+
27+
daf
28+
29+
ghi
30+
31+
Columns 0 and 2 are sorted, but column 1 is not, so you only need to delete 1 column.
32+
33+
**Example 2:**
34+
35+
**Input:** strs = ["a","b"]
36+
37+
**Output:** 0
38+
39+
**Explanation:** The grid looks as follows:
40+
41+
a
42+
43+
b
44+
45+
Column 0 is the only column and is sorted, so you will not delete any columns.
46+
47+
**Example 3:**
48+
49+
**Input:** strs = ["zyx","wvu","tsr"]
50+
51+
**Output:** 3
52+
53+
**Explanation:** The grid looks as follows:
54+
55+
zyx
56+
57+
wvu
58+
59+
tsr
60+
61+
All 3 columns are not sorted, so you will delete all 3.
62+
63+
**Constraints:**
64+
65+
* `n == strs.length`
66+
* `1 <= n <= 100`
67+
* `1 <= strs[i].length <= 1000`
68+
* `strs[i]` consists of lowercase English letters.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package g0901_1000.s0945_minimum_increment_to_make_array_unique
2+
3+
// #Medium #Array #Sorting #Greedy #Counting
4+
// #2023_04_30_Time_427_ms_(100.00%)_Space_101.5_MB_(7.14%)
5+
6+
class Solution {
7+
fun minIncrementForUnique(nums: IntArray): Int {
8+
var max = 0
9+
for (num in nums) {
10+
max = Math.max(max, num)
11+
}
12+
val counts = IntArray(nums.size + max)
13+
for (num in nums) {
14+
counts[num]++
15+
}
16+
var minMoves = 0
17+
for (i in counts.indices) {
18+
if (counts[i] <= 1) {
19+
continue
20+
}
21+
val remaining = counts[i] - 1
22+
minMoves += remaining
23+
counts[i + 1] = counts[i + 1] + remaining
24+
}
25+
return minMoves
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
945\. Minimum Increment to Make Array Unique
2+
3+
Medium
4+
5+
You are given an integer array `nums`. In one move, you can pick an index `i` where `0 <= i < nums.length` and increment `nums[i]` by `1`.
6+
7+
Return _the minimum number of moves to make every value in_ `nums` _**unique**_.
8+
9+
The test cases are generated so that the answer fits in a 32-bit integer.
10+
11+
**Example 1:**
12+
13+
**Input:** nums = [1,2,2]
14+
15+
**Output:** 1
16+
17+
**Explanation:** After 1 move, the array could be [1, 2, 3].
18+
19+
**Example 2:**
20+
21+
**Input:** nums = [3,2,1,2,1,7]
22+
23+
**Output:** 6
24+
25+
**Explanation:** After 6 moves, the array could be [3, 4, 1, 2, 5, 7]. It can be shown with 5 or less moves that it is impossible for the array to have all unique values.
26+
27+
**Constraints:**
28+
29+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
30+
* <code>0 <= nums[i] <= 10<sup>5</sup></code>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package g0901_1000.s0946_validate_stack_sequences
2+
3+
// #Medium #Array #Stack #Simulation #2023_04_30_Time_180_ms_(74.91%)_Space_40.7_MB_(6.18%)
4+
5+
import java.util.Deque
6+
import java.util.LinkedList
7+
8+
class Solution {
9+
fun validateStackSequences(pushed: IntArray, popped: IntArray): Boolean {
10+
val stack: Deque<Int> = LinkedList()
11+
var i = 0
12+
var j = 0
13+
val len = pushed.size
14+
while (i < len) {
15+
if (pushed[i] == popped[j]) {
16+
i++
17+
j++
18+
} else if (!stack.isEmpty() && stack.peek() == popped[j]) {
19+
stack.pop()
20+
j++
21+
} else {
22+
stack.push(pushed[i++])
23+
}
24+
}
25+
while (j < len) {
26+
if (!stack.isEmpty() && stack.peek() != popped[j++]) {
27+
return false
28+
} else {
29+
stack.pop()
30+
}
31+
}
32+
return true
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
946\. Validate Stack Sequences
2+
3+
Medium
4+
5+
Given two integer arrays `pushed` and `popped` each with distinct values, return `true` _if this could have been the result of a sequence of push and pop operations on an initially empty stack, or_ `false` _otherwise._
6+
7+
**Example 1:**
8+
9+
**Input:** pushed = [1,2,3,4,5], popped = [4,5,3,2,1]
10+
11+
**Output:** true
12+
13+
**Explanation:** We might do the following sequence:
14+
15+
push(1), push(2), push(3), push(4),
16+
17+
pop() -> 4,
18+
19+
push(5),
20+
21+
pop() -> 5,
22+
23+
pop() -> 3,
24+
25+
pop() -> 2,
26+
27+
pop() -> 1
28+
29+
**Example 2:**
30+
31+
**Input:** pushed = [1,2,3,4,5], popped = [4,3,5,1,2]
32+
33+
**Output:** false
34+
35+
**Explanation:** 1 cannot be popped before 2.
36+
37+
**Constraints:**
38+
39+
* `1 <= pushed.length <= 1000`
40+
* `0 <= pushed[i] <= 1000`
41+
* All the elements of `pushed` are **unique**.
42+
* `popped.length == pushed.length`
43+
* `popped` is a permutation of `pushed`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package g0901_1000.s0947_most_stones_removed_with_same_row_or_column
2+
3+
// #Medium #Depth_First_Search #Graph #Union_Find #Level_2_Day_19_Union_Find
4+
// #2023_04_30_Time_200_ms_(100.00%)_Space_56_MB_(5.88%)
5+
6+
class Solution {
7+
private val roots = IntArray(20002)
8+
fun removeStones(stones: Array<IntArray>): Int {
9+
for (stone in stones) {
10+
init(stone[0] + 1, roots)
11+
init(stone[1] + 10000, roots)
12+
union(stone[0] + 1, stone[1] + 10000)
13+
}
14+
val set: HashSet<Int> = HashSet()
15+
for (n in roots) {
16+
if (n == 0) {
17+
continue
18+
}
19+
set.add(find(n))
20+
}
21+
return stones.size - set.size
22+
}
23+
24+
private fun init(i: Int, roots: IntArray) {
25+
if (roots[i] != 0) {
26+
return
27+
}
28+
roots[i] = i
29+
}
30+
31+
private fun union(i: Int, j: Int) {
32+
val ri = find(i)
33+
val rj = find(j)
34+
if (ri == rj) {
35+
return
36+
}
37+
roots[ri] = rj
38+
}
39+
40+
private fun find(i: Int): Int {
41+
var cur = i
42+
while (cur != roots[cur]) {
43+
cur = roots[roots[cur]]
44+
}
45+
return cur
46+
}
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
947\. Most Stones Removed with Same Row or Column
2+
3+
Medium
4+
5+
On a 2D plane, we place `n` stones at some integer coordinate points. Each coordinate point may have at most one stone.
6+
7+
A stone can be removed if it shares either **the same row or the same column** as another stone that has not been removed.
8+
9+
Given an array `stones` of length `n` where <code>stones[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents the location of the <code>i<sup>th</sup></code> stone, return _the largest possible number of stones that can be removed_.
10+
11+
**Example 1:**
12+
13+
**Input:** stones = [[0,0],[0,1],[1,0],[1,2],[2,1],[2,2]]
14+
15+
**Output:** 5
16+
17+
**Explanation:** One way to remove 5 stones is as follows:
18+
19+
1. Remove stone [2,2] because it shares the same row as [2,1].
20+
2. Remove stone [2,1] because it shares the same column as [0,1].
21+
3. Remove stone [1,2] because it shares the same row as [1,0].
22+
4. Remove stone [1,0] because it shares the same column as [0,0].
23+
5. Remove stone [0,1] because it shares the same row as [0,0].
24+
25+
Stone [0,0] cannot be removed since it does not share a row/column with another stone still on the plane.
26+
27+
**Example 2:**
28+
29+
**Input:** stones = [[0,0],[0,2],[1,1],[2,0],[2,2]]
30+
31+
**Output:** 3
32+
33+
**Explanation:** One way to make 3 moves is as follows:
34+
35+
1. Remove stone [2,2] because it shares the same row as [2,0].
36+
37+
2. Remove stone [2,0] because it shares the same column as [0,0].
38+
39+
3. Remove stone [0,2] because it shares the same row as [0,0].
40+
41+
Stones [0,0] and [1,1] cannot be removed since they do not share a row/column with another stone still on the plane.
42+
43+
**Example 3:**
44+
45+
**Input:** stones = [[0,0]]
46+
47+
**Output:** 0
48+
49+
**Explanation:** [0,0] is the only stone on the plane, so you cannot remove it.
50+
51+
**Constraints:**
52+
53+
* `1 <= stones.length <= 1000`
54+
* <code>0 <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>4</sup></code>
55+
* No two stones are at the same coordinate point.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package g0901_1000.s0944_delete_columns_to_make_sorted
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 minDeletionSize() {
10+
assertThat(Solution().minDeletionSize(arrayOf("cba", "daf", "ghi")), equalTo(1))
11+
}
12+
13+
@Test
14+
fun minDeletionSize2() {
15+
assertThat(Solution().minDeletionSize(arrayOf("a", "b")), equalTo(0))
16+
}
17+
18+
@Test
19+
fun minDeletionSize3() {
20+
assertThat(Solution().minDeletionSize(arrayOf("zyx", "wvu", "tsr")), equalTo(3))
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package g0901_1000.s0945_minimum_increment_to_make_array_unique
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 minIncrementForUnique() {
10+
assertThat(Solution().minIncrementForUnique(intArrayOf(1, 2, 2)), equalTo(1))
11+
}
12+
13+
@Test
14+
fun minIncrementForUnique2() {
15+
assertThat(Solution().minIncrementForUnique(intArrayOf(3, 2, 1, 2, 1, 7)), equalTo(6))
16+
}
17+
}

0 commit comments

Comments
 (0)