Skip to content

Commit d47ea77

Browse files
authored
Added tasks 1033, 1034, 1035, 1036
1 parent 4c68002 commit d47ea77

File tree

13 files changed

+413
-0
lines changed

13 files changed

+413
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1750,6 +1750,10 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.12'
17501750
| # | Title | Difficulty | Tag | Time, ms | Time, %
17511751
|------|----------------|-------------|-------------|----------|---------
17521752
| 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
1753+
| 1036 |[Escape a Large Maze](src/main/kotlin/g1001_1100/s1036_escape_a_large_maze/Solution.kt)| Hard | Array, Hash_Table, Depth_First_Search, Breadth_First_Search | 387 | 100.00
1754+
| 1035 |[Uncrossed Lines](src/main/kotlin/g1001_1100/s1035_uncrossed_lines/Solution.kt)| Medium | Array, Dynamic_Programming | 162 | 93.33
1755+
| 1034 |[Coloring A Border](src/main/kotlin/g1001_1100/s1034_coloring_a_border/Solution.kt)| Medium | Array, Depth_First_Search, Breadth_First_Search, Matrix | 332 | 100.00
1756+
| 1033 |[Moving Stones Until Consecutive](src/main/kotlin/g1001_1100/s1033_moving_stones_until_consecutive/Solution.kt)| Medium | Math, Brainteaser | 139 | 100.00
17531757
| 1032 |[Stream of Characters](src/main/kotlin/g1001_1100/s1032_stream_of_characters/StreamChecker.kt)| Hard | Array, String, Design, Trie, Data_Stream | 733 | 100.00
17541758
| 1031 |[Maximum Sum of Two Non-Overlapping Subarrays](src/main/kotlin/g1001_1100/s1031_maximum_sum_of_two_non_overlapping_subarrays/Solution.kt)| Medium | Array, Dynamic_Programming, Sliding_Window | 172 | 100.00
17551759
| 1030 |[Matrix Cells in Distance Order](src/main/kotlin/g1001_1100/s1030_matrix_cells_in_distance_order/Solution.kt)| Easy | Array, Math, Sorting, Matrix, Geometry | 426 | 100.00
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package g1001_1100.s1033_moving_stones_until_consecutive
2+
3+
// #Medium #Math #Brainteaser #2023_05_25_Time_139_ms_(100.00%)_Space_35.3_MB_(100.00%)
4+
5+
class Solution {
6+
private fun minMoves(x: Int, y: Int, z: Int): Int {
7+
if (x + 1 == y && y + 1 == z) {
8+
return 0
9+
}
10+
return if (y - x <= 2 || z - y <= 2) {
11+
1
12+
} else 2
13+
}
14+
15+
private fun maxMoves(x: Int, z: Int): Int {
16+
return z - x - 2
17+
}
18+
19+
fun numMovesStones(a: Int, b: Int, c: Int): IntArray {
20+
val t = intArrayOf(a, b, c)
21+
t.sort()
22+
val min = minMoves(t[0], t[1], t[2])
23+
val max = maxMoves(t[0], t[2])
24+
return intArrayOf(min, max)
25+
}
26+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
1033\. Moving Stones Until Consecutive
2+
3+
Medium
4+
5+
There are three stones in different positions on the X-axis. You are given three integers `a`, `b`, and `c`, the positions of the stones.
6+
7+
In one move, you pick up a stone at an endpoint (i.e., either the lowest or highest position stone), and move it to an unoccupied position between those endpoints. Formally, let's say the stones are currently at positions `x`, `y`, and `z` with `x < y < z`. You pick up the stone at either position `x` or position `z`, and move that stone to an integer position `k`, with `x < k < z` and `k != y`.
8+
9+
The game ends when you cannot make any more moves (i.e., the stones are in three consecutive positions).
10+
11+
Return _an integer array_ `answer` _of length_ `2` _where_:
12+
13+
* `answer[0]` _is the minimum number of moves you can play, and_
14+
* `answer[1]` _is the maximum number of moves you can play_.
15+
16+
**Example 1:**
17+
18+
**Input:** a = 1, b = 2, c = 5
19+
20+
**Output:** [1,2]
21+
22+
**Explanation:** Move the stone from 5 to 3, or move the stone from 5 to 4 to 3.
23+
24+
**Example 2:**
25+
26+
**Input:** a = 4, b = 3, c = 2
27+
28+
**Output:** [0,0]
29+
30+
**Explanation:** We cannot make any moves.
31+
32+
**Example 3:**
33+
34+
**Input:** a = 3, b = 5, c = 1
35+
36+
**Output:** [1,2]
37+
38+
**Explanation:** Move the stone from 1 to 4; or move the stone from 1 to 2 to 4.
39+
40+
**Constraints:**
41+
42+
* `1 <= a, b, c <= 100`
43+
* `a`, `b`, and `c` have different values.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package g1001_1100.s1034_coloring_a_border
2+
3+
// #Medium #Array #Depth_First_Search #Breadth_First_Search #Matrix
4+
// #2023_05_25_Time_332_ms_(100.00%)_Space_63.2_MB_(100.00%)
5+
6+
import kotlin.math.abs
7+
8+
class Solution {
9+
fun colorBorder(grid: Array<IntArray>, row: Int, col: Int, color: Int): Array<IntArray> {
10+
getComp(grid, row, col, color, grid[row][col])
11+
for (i in grid.indices) {
12+
for (j in grid[0].indices) {
13+
if (grid[i][j] < 0) {
14+
grid[i][j] = color
15+
}
16+
}
17+
}
18+
return grid
19+
}
20+
21+
private fun getComp(grid: Array<IntArray>, r: Int, c: Int, color: Int, stColor: Int): Int {
22+
if (r < 0 || c < 0 || r >= grid.size || c >= grid[0].size || abs(grid[r][c]) != stColor) {
23+
return 0
24+
}
25+
if (grid[r][c] == -stColor) {
26+
return 1
27+
}
28+
grid[r][c] = -grid[r][c]
29+
var count = 0
30+
count += getComp(grid, r - 1, c, color, stColor)
31+
count += getComp(grid, r + 1, c, color, stColor)
32+
count += getComp(grid, r, c - 1, color, stColor)
33+
count += getComp(grid, r, c + 1, color, stColor)
34+
if (count == 4) {
35+
grid[r][c] = -grid[r][c]
36+
}
37+
return 1
38+
}
39+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
1034\. Coloring A Border
2+
3+
Medium
4+
5+
You are given an `m x n` integer matrix `grid`, and three integers `row`, `col`, and `color`. Each value in the grid represents the color of the grid square at that location.
6+
7+
Two squares belong to the same **connected component** if they have the same color and are next to each other in any of the 4 directions.
8+
9+
The **border of a connected component** is all the squares in the connected component that are either **4-directionally** adjacent to a square not in the component, or on the boundary of the grid (the first or last row or column).
10+
11+
You should color the **border** of the **connected component** that contains the square `grid[row][col]` with `color`.
12+
13+
Return _the final grid_.
14+
15+
**Example 1:**
16+
17+
**Input:** grid = [[1,1],[1,2]], row = 0, col = 0, color = 3
18+
19+
**Output:** [[3,3],[3,2]]
20+
21+
**Example 2:**
22+
23+
**Input:** grid = [[1,2,2],[2,3,2]], row = 0, col = 1, color = 3
24+
25+
**Output:** [[1,3,3],[2,3,3]]
26+
27+
**Example 3:**
28+
29+
**Input:** grid = [[1,1,1],[1,1,1],[1,1,1]], row = 1, col = 1, color = 2
30+
31+
**Output:** [[2,2,2],[2,1,2],[2,2,2]]
32+
33+
**Constraints:**
34+
35+
* `m == grid.length`
36+
* `n == grid[i].length`
37+
* `1 <= m, n <= 50`
38+
* `1 <= grid[i][j], color <= 1000`
39+
* `0 <= row < m`
40+
* `0 <= col < n`
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package g1001_1100.s1035_uncrossed_lines
2+
3+
// #Medium #Array #Dynamic_Programming #2023_05_25_Time_162_ms_(93.33%)_Space_38.5_MB_(34.22%)
4+
5+
class Solution {
6+
fun maxUncrossedLines(nums1: IntArray, nums2: IntArray): Int {
7+
var dp = IntArray(nums2.size + 1)
8+
for (i in 1..nums1.size) {
9+
val dpRow = IntArray(nums2.size + 1)
10+
for (j in 1..nums2.size) {
11+
if (nums1[i - 1] == nums2[j - 1]) {
12+
dpRow[j] = dp[j - 1] + 1
13+
} else {
14+
dpRow[j] = dp[j].coerceAtLeast(dpRow[j - 1])
15+
}
16+
}
17+
dp = dpRow
18+
}
19+
return dp[nums2.size]
20+
}
21+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
1035\. Uncrossed Lines
2+
3+
Medium
4+
5+
You are given two integer arrays `nums1` and `nums2`. We write the integers of `nums1` and `nums2` (in the order they are given) on two separate horizontal lines.
6+
7+
We may draw connecting lines: a straight line connecting two numbers `nums1[i]` and `nums2[j]` such that:
8+
9+
* `nums1[i] == nums2[j]`, and
10+
* the line we draw does not intersect any other connecting (non-horizontal) line.
11+
12+
Note that a connecting line cannot intersect even at the endpoints (i.e., each number can only belong to one connecting line).
13+
14+
Return _the maximum number of connecting lines we can draw in this way_.
15+
16+
**Example 1:**
17+
18+
![](https://assets.leetcode.com/uploads/2019/04/26/142.png)
19+
20+
**Input:** nums1 = [1,4,2], nums2 = [1,2,4]
21+
22+
**Output:** 2
23+
24+
**Explanation:** We can draw 2 uncrossed lines as in the diagram. We cannot draw 3 uncrossed lines, because the line from nums1[1] = 4 to nums2[2] = 4 will intersect the line from nums1[2]=2 to nums2[1]=2.
25+
26+
**Example 2:**
27+
28+
**Input:** nums1 = [2,5,1,2,5], nums2 = [10,5,2,1,5,2]
29+
30+
**Output:** 3
31+
32+
**Example 3:**
33+
34+
**Input:** nums1 = [1,3,7,1,7,5], nums2 = [1,9,2,5,1]
35+
36+
**Output:** 2
37+
38+
**Constraints:**
39+
40+
* `1 <= nums1.length, nums2.length <= 500`
41+
* `1 <= nums1[i], nums2[j] <= 2000`
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package g1001_1100.s1036_escape_a_large_maze
2+
3+
// #Hard #Array #Hash_Table #Depth_First_Search #Breadth_First_Search
4+
// #2023_05_25_Time_387_ms_(100.00%)_Space_94.1_MB_(100.00%)
5+
6+
class Solution {
7+
fun isEscapePossible(blocked: Array<IntArray>, source: IntArray, target: IntArray): Boolean {
8+
if (blocked.isEmpty()) {
9+
return true
10+
}
11+
val blocks: MutableSet<Int> = HashSet()
12+
for (b in blocked) {
13+
if (target[0] * 1000000 + target[1] != b[0] * 1000000 + b[1]) {
14+
blocks.add(b[0] * 1000000 + b[1])
15+
}
16+
}
17+
return (
18+
dfs(blocks, source, source[0], source[1], HashSet(), target) &&
19+
dfs(blocks, target, target[0], target[1], HashSet(), source)
20+
)
21+
}
22+
23+
private fun dfs(
24+
blocks: Set<Int>,
25+
start: IntArray,
26+
i: Int,
27+
j: Int,
28+
visited: MutableSet<Int>,
29+
target: IntArray
30+
): Boolean {
31+
if (i < 0 || j < 0 || i > 999999 || j > 999999 || blocks.contains(i * 1000000 + j) ||
32+
visited.contains(i * 1000000 + j)
33+
) {
34+
return false
35+
}
36+
if (i == target[0] && j == target[1]) {
37+
return true
38+
}
39+
visited.add(i * 1000000 + j)
40+
return if (visited.size > blocks.size * (blocks.size + 1)) {
41+
true
42+
} else dfs(blocks, start, i + 1, j, visited, target) ||
43+
dfs(blocks, start, i - 1, j, visited, target) ||
44+
dfs(blocks, start, i, j + 1, visited, target) ||
45+
dfs(blocks, start, i, j - 1, visited, target)
46+
}
47+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
1036\. Escape a Large Maze
2+
3+
Hard
4+
5+
There is a 1 million by 1 million grid on an XY-plane, and the coordinates of each grid square are `(x, y)`.
6+
7+
We start at the <code>source = [s<sub>x</sub>, s<sub>y</sub>]</code> square and want to reach the <code>target = [t<sub>x</sub>, t<sub>y</sub>]</code> square. There is also an array of `blocked` squares, where each <code>blocked[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents a blocked square with coordinates <code>(x<sub>i</sub>, y<sub>i</sub>)</code>.
8+
9+
Each move, we can walk one square north, east, south, or west if the square is **not** in the array of `blocked` squares. We are also not allowed to walk outside of the grid.
10+
11+
Return `true` _if and only if it is possible to reach the_ `target` _square from the_ `source` _square through a sequence of valid moves_.
12+
13+
**Example 1:**
14+
15+
**Input:** blocked = [[0,1],[1,0]], source = [0,0], target = [0,2]
16+
17+
**Output:** false
18+
19+
**Explanation:** The target square is inaccessible starting from the source square because we cannot move.
20+
21+
We cannot move north or east because those squares are blocked.
22+
23+
We cannot move south or west because we cannot go outside of the grid.
24+
25+
**Example 2:**
26+
27+
**Input:** blocked = [], source = [0,0], target = [999999,999999]
28+
29+
**Output:** true
30+
31+
**Explanation:** Because there are no blocked cells, it is possible to reach the target square.
32+
33+
**Constraints:**
34+
35+
* `0 <= blocked.length <= 200`
36+
* `blocked[i].length == 2`
37+
* <code>0 <= x<sub>i</sub>, y<sub>i</sub> < 10<sup>6</sup></code>
38+
* `source.length == target.length == 2`
39+
* <code>0 <= s<sub>x</sub>, s<sub>y</sub>, t<sub>x</sub>, t<sub>y</sub> < 10<sup>6</sup></code>
40+
* `source != target`
41+
* It is guaranteed that `source` and `target` are not blocked.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package g1001_1100.s1033_moving_stones_until_consecutive
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 numMovesStones() {
10+
assertThat(Solution().numMovesStones(1, 2, 5), equalTo(intArrayOf(1, 2)))
11+
}
12+
13+
@Test
14+
fun numMovesStones2() {
15+
assertThat(Solution().numMovesStones(4, 3, 2), equalTo(intArrayOf(0, 0)))
16+
}
17+
18+
@Test
19+
fun numMovesStones3() {
20+
assertThat(Solution().numMovesStones(3, 5, 1), equalTo(intArrayOf(1, 2)))
21+
}
22+
}

0 commit comments

Comments
 (0)