Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8db95be

Browse files
2577: Minimum Time to Visit a Cell In a Grid
1 parent 25f9bfb commit 8db95be

File tree

4 files changed

+117
-0
lines changed

4 files changed

+117
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ My solutions to LeetCode problems in Kotlin.
3434
| [2275](https://leetcode.com/problems/largest-combination-with-bitwise-and-greater-than-zero/) | [Largest Combination With Bitwise AND Greater Than Zero](src/main/kotlin/com/schmoczer/leetcode/_2275/LargestCombination.kt) | Medium |
3535
| [2490](https://leetcode.com/problems/circular-sentence/) | [Circular Sentence](src/main/kotlin/com/schmoczer/leetcode/_2490/CircularSentence.kt) | Easy |
3636
| [2516](https://leetcode.com/problems/take-k-of-each-character-from-left-and-right/) | [Take K of Each Character From Left and Right](src/main/kotlin/com/schmoczer/leetcode/_2516/TakeKOfEachCharacterFromLeftAndRight.kt) | Medium |
37+
| [2577](https://leetcode.com/problems/minimum-time-to-visit-a-cell-in-a-grid/) | [Minimum Time to Visit a Cell In a Grid](src/main/kotlin/com/schmoczer/leetcode/_2577/MinimumTimeToVisitCellInGrid.kt) | Hard |
3738
| [2914](https://leetcode.com/problems/minimum-number-of-changes-to-make-binary-string-beautiful/) | [Minimum Number of Changes to Make Binary String Beautiful](src/main/kotlin/com/schmoczer/leetcode/_2914/MinChanges.kt) | Medium |
3839
| [2924](https://leetcode.com/problems/find-champion-ii/) | [Find Champion II](src/main/kotlin/com/schmoczer/leetcode/_2924/FindChampionII.kt) | Medium |
3940
| [3011](https://leetcode.com/problems/find-if-array-can-be-sorted/) | [Find if Array Can Be Sorted](src/main/kotlin/com/schmoczer/leetcode/_3011/FindIfArrayCanBeSorted.kt) | Medium |
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.schmoczer.leetcode._2577
2+
3+
import java.util.*
4+
5+
class MinimumTimeToVisitCellInGrid {
6+
// Runtime 142ms Beats 100.00%
7+
fun minimumTime(grid: Array<IntArray>): Int {
8+
if (grid[0][1] > 1 && grid[1][0] > 1) {
9+
return -1
10+
}
11+
12+
val lastRow = grid.size - 1
13+
val lastColumn = grid.first().size - 1
14+
val directions = arrayOf(intArrayOf(1, 0), intArrayOf(-1, 0), intArrayOf(0, 1), intArrayOf(0, -1))
15+
val queue = PriorityQueue<IntArray>(compareBy { it[2] })
16+
queue.offer(intArrayOf(0, 0, 0))
17+
while (queue.isNotEmpty()) {
18+
val (currentRow, currentColumn, currentTime) = queue.poll()
19+
if (currentRow == lastRow && currentColumn == lastColumn) {
20+
return currentTime
21+
}
22+
grid[currentRow][currentColumn] = -1
23+
for (i in 0 until 4) {
24+
val nextRow = currentRow + directions[i][0]
25+
val nextColumn = currentColumn + directions[i][1]
26+
if (nextRow in 0..lastRow && nextColumn in 0..lastColumn && grid[nextRow][nextColumn] > -1) {
27+
val difference = grid[nextRow][nextColumn] - currentTime
28+
val nextTime = currentTime + if (difference < 2) {
29+
1
30+
} else if (difference % 2 == 0) {
31+
difference + 1
32+
} else {
33+
difference
34+
}
35+
queue.offer(intArrayOf(nextRow, nextColumn, nextTime))
36+
grid[nextRow][nextColumn] = -1
37+
}
38+
}
39+
}
40+
return -1
41+
}
42+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Minimum Time to Visit a Cell In a Grid
2+
3+
You are given a `m x n` matrix `grid` consisting of non-negative integers where `grid[row][col]` represents the minimum
4+
time required to be able to visit the cell `(row, col)`, which means you can visit the cell `(row, col)` only when the
5+
time you visit it is greater than or equal to `grid[row][col]`.
6+
7+
You are standing in the top-left cell of the matrix in the `0`'th second, and you must move to any adjacent cell in the
8+
four directions: up, down, left, and right. Each move you make takes 1 second.
9+
10+
Return the minimum time required in which you can visit the bottom-right cell of the matrix. If you cannot visit the
11+
bottom-right cell, then return `-1`.
12+
13+
Example 1:
14+
15+
> Input: grid = [[0,1,3,2],[5,1,2,5],[4,3,8,6]]
16+
>
17+
> Output: 7
18+
19+
Example 2:
20+
21+
> Input: grid = [[0,2,4],[3,2,1],[1,0,4]]
22+
>
23+
> Output: -1
24+
25+
Constraints:
26+
27+
- `m == grid.length`
28+
- `n == grid[i].length`
29+
- `2 <= m, n <= 1000`
30+
- `4 <= m * n <= 10^5`
31+
- `0 <= grid[i][j] <= 10^5`
32+
- `grid[0][0] == 0`
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.schmoczer.leetcode._2577
2+
3+
import org.junit.jupiter.api.BeforeEach
4+
import kotlin.test.Test
5+
import kotlin.test.assertEquals
6+
7+
class MinimumTimeToVisitCellInGridTest {
8+
private lateinit var sut: MinimumTimeToVisitCellInGrid
9+
10+
@BeforeEach
11+
fun setUp() {
12+
sut = MinimumTimeToVisitCellInGrid()
13+
}
14+
15+
@Test
16+
fun `a 0,1,3,2 | 5,1,2,5 | 4,3,8,6 grid needs a minimum time of 7`() {
17+
val grid = arrayOf(
18+
intArrayOf(0, 1, 3, 2),
19+
intArrayOf(5, 1, 2, 5),
20+
intArrayOf(4, 3, 8, 6),
21+
)
22+
val expected = 7
23+
24+
val result = sut.minimumTime(grid)
25+
26+
assertEquals(expected, result)
27+
}
28+
29+
@Test
30+
fun `a 0,2,4 | 3,2,1 | 1,0,4 grid has no path`() {
31+
val grid = arrayOf(
32+
intArrayOf(0, 2, 4),
33+
intArrayOf(3, 2, 1),
34+
intArrayOf(1, 0, 4),
35+
)
36+
val expected = -1
37+
38+
val result = sut.minimumTime(grid)
39+
40+
assertEquals(expected, result)
41+
}
42+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /