下降的路径就是从第一行任一个元素出发,只能直着或斜着走到下一行,直到到达最后一行,返回形成的路径中最小的路径和。

思路:

DP
从下往上推,
在最后一行选一个元素,那么它是从哪里过来的?是从上一行的j-1, j, j+1 3个位置过来的。
设dp[ i ] [ j ]是从第一行任一元素到[ i][ j ]所在元素的所有路径的最小路径和。
那么dp[ i ][ j ] = min(dp[ i - 1][j - 1], dp[i - 1] [ j ], dp[i-1][j+1]) + matrix[ i ][ j ],

然后取dp[n-1]这一行的最小值即可。

    public int minFallingPathSum(int[][] matrix) {int n = matrix.length;int res = 10000000;int[][] dp = new int[n][n];for(int j = 0; j < n; j++) dp[0][j] = matrix[0][j];for(int i = 1; i < n; i++) {for(int j = 0; j < n; j++) {if(j == 0) {dp[i][j] = Math.min(dp[i-1][j], dp[i-1][j+1]) + matrix[i][j];} else if(j == n-1) {dp[i][j] = Math.min(dp[i-1][j-1], dp[i-1][j]) + matrix[i][j];} else {dp[i][j] = Math.min(Math.min(dp[i-1][j-1], dp[i-1][j]), dp[i-1][j+1]) + matrix[i][j];}}}for(int j = 0; j < n; j++) {res = Math.min(res, dp[n-1][j]);}return res;}

上面的方法好理解,但不是最快的,最快的参考

class Solution {int ans = Integer.MAX_VALUE;public int minFallingPathSum(int[][] matrix) {helper(matrix, matrix.length-2);return ans;}void helper(int[][] matrix, int r) {if(r<0) {for(int i=0; i<matrix.length; i++) {ans = Math.min(ans, matrix[0][i]);}return;}for(int i=0; i<matrix.length; i++) {int minNextVal = matrix[r+1][i];if(i>0) {minNextVal = Math.min(minNextVal, matrix[r+1][i-1]);}if(i<matrix.length-1) {minNextVal = Math.min(minNextVal, matrix[r+1][i+1]);}matrix[r][i] += minNextVal;}helper(matrix, r-1);}
}

leetcode 931. Minimum Falling Path Sum(最小的下降路径和)相关推荐

  1. 【leetcode】931. Minimum Falling Path Sum

    题目如下: Given a square array of integers A, we want the minimum sum of a falling path through A. A fal ...

  2. Leetcode | Binary Tree Maximum Path Sum

    Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. ...

  3. [Leetcode] Binary Tree Maximum Path Sum

    这是LeetCode上的一道题目,需要求二叉树中两点路径的最大和.原题是 https://oj.leetcode.com/problems/binary-tree-maximum-path-sum/ ...

  4. leetcode 112. Path Sum, 113. Path Sum II | 112,113. 路径总和 I, II(Java)

    题目 https://leetcode.com/problems/path-sum/ https://leetcode.com/problems/path-sum-ii/ 题解 简单的遍历二叉树,不解 ...

  5. LeetCode 712. Minimum ASCII Delete Sum for Two Strings

    712.Minimum ASCII Delete Sum for Two Strings(两个字符串的最小ASCII删除和) 题目: 给定两个字符串s1, s2,找到使两个字符串相等所需删除字符的AS ...

  6. Leetcode: Binary Tree Maximum Path Sum

    Given a binary tree, find the maximum path sum.The path may start and end at any node in the tree.Fo ...

  7. LeetCode:Minimum Path Sum(网格最大路径和)

    Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...

  8. leetcode 712. Minimum ASCII Delete Sum for Two Strings | 712. 两个字符串的最小ASCII删除和(暴力递归->傻缓存->DP)

    题目 https://leetcode.com/problems/minimum-ascii-delete-sum-for-two-strings/ 题解 经典的 暴力递归 -> 傻缓存 -&g ...

  9. leetcode 209. Minimum Size Subarray Sum | 209. 长度最小的子数组(Java)

    题目 https://leetcode.com/problems/minimum-size-subarray-sum/ 题解 双指针解法,左指针和右指针在合适的时候向右走,并维护一个sum 版本1 思 ...

最新文章

  1. Pixhawk代码分析-姿态解算篇B
  2. The following module was built either with optimizations enabled or without debug information - winz
  3. java 注解 target_详解JDK 5 Annotation 注解之@Target的用法
  4. linux 内核文件结构,linux-011内核文件结构图
  5. 神奇的applycall
  6. 分享一个入门级实操项目,思路非常棒!
  7. 简单链表实现增删改查(内部类+递归)
  8. 分享吴恩达机器学习视频和300页word学习笔记,以及深度学习五门课视频及700页word笔记
  9. [python3] zipfile压缩目录下所有的文档都被压缩,并解决压缩路径过深的问题
  10. 通信LOGO隐秘史(续集:运营商篇)
  11. 数据地图搜索功能模块项目总结【springBoot+Elasticsearch】
  12. 微醺时代的白酒新锐:政策“理想”与现实“骨感”的夹缝进击
  13. 寻找协调器FindCoordinatorRequest请求流程
  14. 计算机管理的事件id,事件ID6009,6006检测电脑开关机时间
  15. 解决无法删除文件夹的情况:文件夹正在使用,操作无法完成,因为其中的文件,或文件夹已在另一个程序中打开...
  16. Category底层原理实现
  17. Js中apply和call
  18. matlab 数组偶数,matlab中取数组的偶数列赋值给新数组
  19. 重庆交通大学计算机考研资料汇总
  20. 外发简历为什么服务器响应失败,为什么求职者投了简历,但我的电子邮箱一直收不到简历邮件呢?...

热门文章

  1. 不归零法编码、曼彻斯特编码和差分曼彻斯特编码
  2. Android ToolBar设定为ifRoom的menu不显示图标的问题
  3. 【人工智能】人工神经网络
  4. 数字字符与中文字符的替换
  5. Altium Designer中无法正常显示汉字的解决方法
  6. 批量生成HTML文件,通过 学生名单(学号、姓名) 与 博客名单(姓名、网址)
  7. 死锁产生的4个必要条件?
  8. 摊牌了,请各位做好1年内随时失业的准备
  9. FastChat(小羊驼模型)部署体验
  10. php 一二三 排序,php 数组排序函数