Skip to content

Commit

Permalink
Create 2017-grid-game.kt
Browse files Browse the repository at this point in the history
  • Loading branch information
a93a authored May 22, 2023
1 parent 8e28396 commit 7fe92db
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions kotlin/2017-grid-game.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
class Solution {
fun gridGame(grid: Array<IntArray>): Long {
val n = grid[0].size
val topPrefix = LongArray(n)
val botPrefix = LongArray(n)

for (i in 0 until n) {
topPrefix[i] = grid[0][i].toLong()
botPrefix[i] = grid[1][i].toLong()
if (i > 0 ) {
topPrefix[i] += topPrefix[i - 1]
botPrefix[i] += botPrefix[i - 1]
}
}

var res = Long.MAX_VALUE
for (i in 0 until n) {
val rob2top = topPrefix[n - 1] - topPrefix[i]
val rob2bot = if (i > 0) botPrefix[i - 1] else 0L

val rob2take = maxOf(
rob2top,
rob2bot
)

res = minOf(
res,
rob2take
)
}

return res
}
}

0 comments on commit 7fe92db

Please sign in to comment.