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

g1201_1300.s1289_minimum_falling_path_sum_ii.Solution.kt Maven / Gradle / Ivy

There is a newer version: 1.30
Show newest version
package g1201_1300.s1289_minimum_falling_path_sum_ii

// #Hard #Array #Dynamic_Programming #Matrix
// #2023_06_08_Time_234_ms_(100.00%)_Space_38.4_MB_(100.00%)

class Solution {
    fun minFallingPathSum(grid: Array): Int {
        val n = grid[0].size
        var prev = IntArray(n)
        var curr = IntArray(n)
        var prevMinOne = 0
        var prevMinTwo = 0
        for (ints in grid) {
            var currMinOne = Int.MAX_VALUE
            var currMinTwo = Int.MAX_VALUE
            for (j in 0 until n) {
                val prevMin = if (prev[j] == prevMinOne) prevMinTwo else prevMinOne
                curr[j] = ints[j] + prevMin
                if (curr[j] < currMinOne) {
                    currMinTwo = currMinOne
                    currMinOne = curr[j]
                } else if (curr[j] < currMinTwo) {
                    currMinTwo = curr[j]
                }
            }
            prevMinOne = currMinOne
            prevMinTwo = currMinTwo
            // reuse curr array, avoid new int[] in every row
            val temp = prev
            prev = curr
            curr = temp
        }
        return prevMinOne
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy