当前位置:网站首页>【动态规划】路径dp:931. Minimum Falling Path Sum
【动态规划】路径dp:931. Minimum Falling Path Sum
2022-07-01 00:41:00 【暮色_年华】
Given an n x n array of integers matrix, return the minimum sum of any falling path through matrix.
A falling path starts at any element in the first row and chooses the element in the next row that is either directly below or diagonally left/right. Specifically, the next element from position (row, col) will be (row + 1, col - 1), (row + 1, col), or (row + 1, col + 1).
从第一行向下走,每次可以走三个位置,走到最后一行,求最小路径和
注意初始化即可:
dp[i][j]=INF
dp下标为1表示g[0],dp[0][j]=0(避免了边界讨论)
class Solution {
public int minFallingPathSum(int[][] g) {
int n=g.length;
int [][] dp=new int[n+2][n+2];
for(int i=0;i<=n+1;i++)
Arrays.fill(dp[i],0x3f3f3f3f);
for(int i=0;i<=n+1;i++){
dp[0][i]=0;
}
int res=0x3f3f3f3f;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
dp[i][j]=Math.min(dp[i][j],Math.min(dp[i-1][j],Math.min(dp[i-1][j-1],dp[i-1][j+1]))+g[i-1][j-1]);
if(i==n){
res=Math.min(res,dp[i][j]);
}
}
}
return res;
}
}
边栏推荐
猜你喜欢

Openmv and k210 of the f question of the 2021 video game call the openmv API for line patrol, which is completely open source.

基础知识之二——STA相关的基本定义

flutter报错 -- The argument type ‘Function‘ can‘t be assigned to the parameter type ‘void Function()?‘

45岁程序员告诉你:程序员为什么要跳槽,太真实...

Introduction and principle analysis of cluster and LVS

使用 C# 创造 ASCII 艺术
![分割链表[先取next再斩断链表防止断链]](/img/eb/708ab20c13df75f4dbd2d6461d3602.png)
分割链表[先取next再斩断链表防止断链]

Docker deployment MySQL 8

Pre training / transfer learning of models

Technical personnel advanced to draw a big picture of business, hand-in-hand teaching is coming
随机推荐
ASCII、Unicode、GBK、UTF-8之间的关系
StrictMode卡顿与泄漏检测-StrictMode原理(2)
【go】go 实现行专列 将集合进行转列
Impact relay zc-23/dc220v
neo4j安装、运行以及项目的构建和功能实现
45岁程序员告诉你:程序员为什么要跳槽,太真实...
友盟(软件异常实时监听的好帮手:Crash)接入教程(有点基础的小白最易学的教程)
孔乙己第一问之服务通信知多少?
[learning notes] structure
人穷志不短,穷学生也能玩转树莓派
Docker 部署 MySQL 8
MFC TCP通信服务端客户端Demo备忘vs2019
二季度最后一天
冲击继电器ZC-23/DC220V
Dx-11q signal relay
Koa koa combine routes sub route management
小程序自定义宫格
Call the classic architecture and build the model based on the classic
Analyze the maker education path integrating the essence of discipline
Interpreting the scientific and technological literacy contained in maker Education