1289. Minimum Falling Path Sum II

复制代码
package LeetCode_1289

/**
 * 1289. Minimum Falling Path Sum II
 * https://leetcode.com/problems/minimum-falling-path-sum-ii/
 * Given a square grid of integers arr,
 * a falling path with non-zero shifts is a choice of exactly one element from each row of arr,
 * such that no two elements chosen in adjacent rows are in the same column.
Return the minimum sum of a falling path with non-zero shifts.

Example 1:
Input: arr =
[[1,2,3],
[4,5,6],
[7,8,9]]
Output: 13
Explanation:
The possible falling paths are:
[1,5,9], [1,5,7], [1,6,7], [1,6,8],
[2,4,8], [2,4,9], [2,6,7], [2,6,8],
[3,4,8], [3,4,9], [3,5,7], [3,5,9]
The falling path with the smallest sum is [1,5,7], so the answer is 13.

Constraints:
1. 1 <= arr.length == arr[i].length <= 200
2. -99 <= arr[i][j] <= 99
 * */
class Solution {
    /*
   * solution : DP, Bottom-Up, for example array is:
   * [
       [1,2,3],
       [4,5,6],
       [7,8,9]
       ]
      fill new value from second row,the value of current i,j is: current value + (value by: smallest value in prevRow and currentCol!=prevCol)
       [1,2,3],
       [6,6,7],//<-this row after fill by: 4+min(2,3), 5+min(1,3), 6+min(1,2)
       [13,12,13],//<-this row after fill by: 7+min(6,7), 8+min(4,6), 9+min(4,5)
       result is minimum in last row

       Time complexity:O(m*n*logn), Space complexity:O(1)
   * */
    fun minFallingPathSum(arr: Array<IntArray>): Int {
        if (arr == null || arr.isEmpty()) {
            return 0
        }
        val m = arr.size
        val n = arr[0].size
        for (i in 1 until m) {
            //find out two smallest number at prev level
            //first: dp[i][j], second:cols
            val temp = Array<Pair<Int, Int>>(m) { Pair(0, 0) }
            for (k in 0 until n) {
                temp[k] = Pair(arr[i - 1][k], k)
            }
            temp.sortWith(Comparator { a, b -> a.first - b.first })
            for (j in 0 until n) {
                if (j != temp[0].second) {
                    //if current cols not equals prev cols and the value in prev cols is the smallest
                    arr[i][j] += temp[0].first
                } else {
                    arr[i][j] += temp[1].first
                }
            }
        }
        var result = Int.MAX_VALUE
        for (cols in 0 until n) {
            result = Math.min(result, arr[n - 1][cols])
        }
        return result
    }
}
复制代码

 

posted @   johnny_zhao  阅读(72)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示