Given a square array of integers A, we want the minimum sum of a falling path through A.

A falling path starts at any element in the first row, and chooses one element from each row.  The next row's choice must be in a column that is different from the previous row's column by at most one.

Example 1:

Input: [[1,2,3],[4,5,6],[7,8,9]]
Output: 12
Explanation:
The possible falling paths are:
  • [1,4,7], [1,4,8], [1,5,7], [1,5,8], [1,5,9]
  • [2,4,7], [2,4,8], [2,5,7], [2,5,8], [2,5,9], [2,6,8], [2,6,9]
  • [3,5,7], [3,5,8], [3,5,9], [3,6,8], [3,6,9]

The falling path with the smallest sum is [1,4,7], so the answer is 12.

Note:

  1. 1 <= A.length == A[0].length <= 100
  2. -100 <= A[i][j] <= 100

大概都知道求矩阵的最小和,这里不过是规则变了(需要是向下且不同行)

那个dp[i][j]可能从正上方,或者是右上,左上 + A[i][j]得来,考虑边界

class Solution {
public:
int minFallingPathSum(vector<vector<int>>& A) {
if(A.size() == ) return ;
int inf = ;
int n = A.size();
vector<vector<int>> dp(n + , vector<int>(n + , inf));
for (int i = ; i <= n; ++i) dp[][i] = A[][i];
for (int i = ; i < n; ++i) {
for (int j = ; j < n; ++j) {
if (j > ) dp[i][j] = min(dp[i][j], dp[i - ][j - ] + A[i][j]);
if (j + < n) dp[i][j] = min(dp[i][j], dp[i - ][j + ] + A[i][j]);
dp[i][j] = min(dp[i][j], dp[i - ][j] + A[i][j]);
}
}
int ret = inf;
for (int x : dp[n - ]) ret = min(ret, x);
return ret;
}
};

108th LeetCode Weekly Contest Minimum Falling Path Sum的更多相关文章

  1. 【LeetCode】931. Minimum Falling Path Sum 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 相似题目 参考资料 日期 题目地址:htt ...

  2. 【leetcode】1289. Minimum Falling Path Sum II

    题目如下: Given a square grid of integers arr, a falling path with non-zero shifts is a choice of exactl ...

  3. 【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 ...

  4. 108th LeetCode Weekly Contest Binary Subarrays With Sum

    In an array A of 0s and 1s, how many non-empty subarrays have sum S? Example 1: Input: A = [1,0,1,0, ...

  5. Leetcode 931. Minimum falling path sum 最小下降路径和(动态规划)

    Leetcode 931. Minimum falling path sum 最小下降路径和(动态规划) 题目描述 已知一个正方形二维数组A,我们想找到一条最小下降路径的和 所谓下降路径是指,从一行到 ...

  6. LeetCode 931. Minimum Falling Path Sum

    原题链接在这里:https://leetcode.com/problems/minimum-falling-path-sum/ 题目: Given a square array of integers ...

  7. Leetcode之动态规划(DP)专题-931. 下降路径最小和(Minimum Falling Path Sum)

    Leetcode之动态规划(DP)专题-931. 下降路径最小和(Minimum Falling Path Sum) 给定一个方形整数数组 A,我们想要得到通过 A 的下降路径的最小和. 下降路径可以 ...

  8. [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 falling p ...

  9. [Swift]LeetCode931. 下降路径最小和 | Minimum Falling Path Sum

    Given a square array of integers A, we want the minimum sum of a falling path through A. A falling p ...

随机推荐

  1. 【微信公众平台开发】微信JS-SDK开发,信公众平台js-sdk

    根据微信开发文档步骤如下: 1.先登录微信公众平台进入“公众号设置”的“功能设置”里填写“JS接口安全域名”. JS接口安全域名设置 mi.com(前面不用带www/http,域名必须备案过)   2 ...

  2. excel中COUNTIF的使用

    =(COUNTIF(D9:AH9,"●")+COUNTIF(D7:AH7,"●"))*0.5

  3. 再谈JQuery插件$.extend(), $.fn和$.fn.extend()

    在我的博客中,曾经写过一篇关于JQuery插件的文章  https://www.cnblogs.com/wphl-27/p/6903170.html 今天看一个项目的代码时,看到使用JQuery插件部 ...

  4. 基于.Net平台常用的组件和框架整理

    转载自:http://www.cnblogs.com/hgmyz/p/5313983.html 基于转载进行补充 RPC框架: RPC:远程过程调用协议,它是一种通过网络从远程计算机程序上请求服务,而 ...

  5. C高级第一次作业

    未来两周学习内容 复习指针的定义和引用 指针的应用场景: 指针作为函数参数(角色互换) 指针作为函数的参数返回多个值 指针.数组和地址间的关系 使用指针进行数组操作 数组名(指针)作为函数参数(冒泡排 ...

  6. (转)mongodb学习(翻译1)

    原文地址:http://www.cnblogs.com/Johnzhang/archive/2013/09/10/3313582.html 学习mongodb,试着翻译写,英语能力有限,希望大家指正, ...

  7. Xamarin.Forms之UserDialogs 重制版本

    在 forms 里面,目前使用比较多的弹出组件是 Acr.UserDialogs ,但是这个组件有些小问题,比如 loading .hide 会同时把 toast 给一起关掉,android 下的 t ...

  8. 微信第三方平台开头篇--MVC代码(第三方获取ticket和公众号授权)

    微信公众号授权给开放平台 公众号授权给第三方平台的技术实现流程比较简单 这个步骤遗漏了开头获取第三方平台自己的accessToken 先说下流程 如何注册开放平台的第三方信息看截图 其他不说了,此文只 ...

  9. 钩子(hook)编程

    一.钩子介绍 1.1钩子的实现机制 钩子英文名叫Hook,是一种截获windows系统中某应用程序或者所有进程的消息的一种技术.下图是windows应用程序传递消息的过程: 如在键盘中按下一键,操作系 ...

  10. React杂篇(1) -- 打包发布注意事项

    打包后资源路径 问题:直接打包会出现一下情况: 方式一: package.json文件下加入homepage字段 { "name": "wap-v2", &qu ...