All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g2001_2100.s2017_grid_game.Solution.kt Maven / Gradle / Ivy

There is a newer version: 1.30
Show newest version
package g2001_2100.s2017_grid_game

// #Medium #Array #Matrix #Prefix_Sum #2023_06_23_Time_578_ms_(87.50%)_Space_56.7_MB_(100.00%)

class Solution {
    fun gridGame(grid: Array): Long {
        val n = grid[0].size
        val cum0 = LongArray(n + 1)
        val cum1 = LongArray(n + 1)
        for (i in 0 until n) {
            cum0[i + 1] = cum0[i] + grid[0][i]
            cum1[i + 1] = cum1[i] + grid[1][i]
        }
        var ans = Long.MAX_VALUE
        for (i in 0 until n) {
            ans = Math.min(ans, Math.max(cum0[n] - cum0[i + 1], cum1[i]))
        }
        return ans
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy