当前位置:网站首页>【动态规划】路径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;
}
}
边栏推荐
- ESP8266 RC522
- The liquor and tourism sector recovers, and Yaduo continues to dream of listing. How far is it from "the first share of the new accommodation economy"?
- 生意和投资的思考
- 做生意更加务实
- None of the following candidates is applicable because of a receiver type mismatch
- C语言一点点(未来可会增加)
- Pytorch programming knowledge (2)
- js中把数字转换成汉字输出
- WIN11中MathType编辑中“打开数学输入面板”是灰色不可编辑
- 染色法判断二分图
猜你喜欢

Q play soft large toast to bring more comfortable sleep
![[go] go implements row column conversion of sets](/img/d9/6272e55b2d9c6b6fbdf2537773bb83.png)
[go] go implements row column conversion of sets

解决IDEA:Class ‘XXX‘ not found in module ‘XXX‘

技术人进阶画业务大图,手把手教学来了

日志 logrus第三方库的使用

Xjy-220/43ac220v static signal relay

Unhandled Exception: MissingPluginException(No implementation found for method launch on channel)

【学习笔记】倍增 + 二分

K210工地安全帽

Analyze the maker education path integrating the essence of discipline
随机推荐
【学习笔记】构造
Interpreting the scientific and technological literacy contained in maker Education
Xjy-220/43ac220v static signal relay
Kongyiji's first question: how much do you know about service communication?
JS to convert numbers into Chinese characters for output
Note d'étude du DC: zéro dans le chapitre officiel - - Aperçu et introduction du processus de base
图的连通性基础
StrictMode分析Registion-StrictMode原理(4)
一些本质的区别
Dls-42/6-4 dc110v double position relay
Windows环境下安装MongoDB数据库
TypeError: can‘t convert cuda:0 device type tensor to numpy. Use Tensor.cpu() to copy the tensor to
Inspire students' diversified thinking with steam Education
StrictMode卡顿与泄漏检测-StrictMode原理(2)
flutter报错 -- The argument type ‘Function‘ can‘t be assigned to the parameter type ‘void Function()?‘
Typora的使用
Analyzing the wisdom principle in maker education practice
自定义注解实现校验
XJY-220/43AC220V静态信号继电器
友盟(软件异常实时监听的好帮手:Crash)接入教程(有点基础的小白最易学的教程)