Skip to content

Commit 7fe92db

Browse files
authored
Create 2017-grid-game.kt
1 parent 8e28396 commit 7fe92db

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

kotlin/2017-grid-game.kt

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class Solution {
2+
fun gridGame(grid: Array<IntArray>): Long {
3+
val n = grid[0].size
4+
val topPrefix = LongArray(n)
5+
val botPrefix = LongArray(n)
6+
7+
for (i in 0 until n) {
8+
topPrefix[i] = grid[0][i].toLong()
9+
botPrefix[i] = grid[1][i].toLong()
10+
if (i > 0 ) {
11+
topPrefix[i] += topPrefix[i - 1]
12+
botPrefix[i] += botPrefix[i - 1]
13+
}
14+
}
15+
16+
var res = Long.MAX_VALUE
17+
for (i in 0 until n) {
18+
val rob2top = topPrefix[n - 1] - topPrefix[i]
19+
val rob2bot = if (i > 0) botPrefix[i - 1] else 0L
20+
21+
val rob2take = maxOf(
22+
rob2top,
23+
rob2bot
24+
)
25+
26+
res = minOf(
27+
res,
28+
rob2take
29+
)
30+
}
31+
32+
return res
33+
}
34+
}

0 commit comments

Comments
 (0)