Skip to content

Commit cb9461b

Browse files
committed
- Add solutions
1 parent ca4ceca commit cb9461b

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution {
2+
fun isReachableAtTime(sx: Int, sy: Int, fx: Int, fy: Int, t: Int): Boolean {
3+
if (sx == fx && sy == fy && t == 1) return false
4+
val dstX = Math.abs(fx - sx)
5+
val dstY = Math.abs(fy - sy)
6+
val diag = minOf(dstY, dstX) //diagonal movement
7+
val line = maxOf(dstY, dstX) - diag //straight line movement
8+
val shortest = diag + line
9+
return t >= shortest
10+
}
11+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
fun minimumMoves(grid: Array<IntArray>): Int {
3+
// Base Case
4+
var empties = 0
5+
repeat(3) { r -> repeat(3) { c ->
6+
if (grid[r][c] == 0) empties++
7+
} }
8+
if (empties == 0) return 0
9+
10+
var res = Int.MAX_VALUE
11+
repeat(3) { emptyR -> repeat(3) { emptyC ->
12+
if (grid[emptyR][emptyC] == 0) {
13+
repeat(3) { spareR -> repeat(3) { spareC ->
14+
val distance = abs(spareR - emptyR) + abs(spareC - emptyC)
15+
if (grid[spareR][spareC]-- > 1) {
16+
grid[emptyR][emptyC]++
17+
res = min(res, distance + minimumMoves(grid)) //backtrack
18+
grid[spareR][spareC]++
19+
grid[emptyR][emptyC]--
20+
}
21+
} }
22+
}
23+
} }
24+
return res
25+
}
26+
}

0 commit comments

Comments
 (0)