当前位置:网站首页>【动态规划】路径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;
}
}
边栏推荐
- 二十多年来第一次!CVPR最佳学生论文授予中国高校学生!
- OCR的一些项目
- 解决IDEA:Class ‘XXX‘ not found in module ‘XXX‘
- Visual studio 2019 Download
- Training discipline principle of robot programming
- About vctk datasets
- Basic knowledge of software and hardware -- diary (1)
- JS方法大全的一个小文档
- Installing mongodb database in Windows Environment
- Looksrare team's "cash out" caused disturbance
猜你喜欢

Green, green the reed. dew and frost gleam.

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

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

编译安装oh-my-zsh

流批一体在京东的探索与实践

Solve idea:class' xxx 'not found in module' xxx‘

Win11安装redis 数据库以及redis desktop manager的下载

機器人編程的培訓學科類原理

解读创客教育所蕴含的科技素养

【网络丢包,网络延迟?这款神器帮你搞定所有!】
随机推荐
双位置继电器DLS-5/2 DC220V
K210门禁毕设
Green, green the reed. dew and frost gleam.
Orb-slam2 source code learning (II) map initialization
孔乙己第一问之服务通信知多少?
数字IC设计流程总结
None of the following candidates is applicable because of a receiver type mismatch
做生意更加务实
User defined annotation implementation verification
图的连通性基础
gin 配置文件
[问题已处理]-nvidia-smi命令获取不到自身容器的GPU进程和外部的GPU进程号
WIN11中MathType编辑中“打开数学输入面板”是灰色不可编辑
一些本质的区别
pytorch编程知识(2)
DC学习笔记正式篇之零——综述与基本流程介绍
Technical personnel advanced to draw a big picture of business, hand-in-hand teaching is coming
pull_ to_ refresh
解析融合学科本质的创客教育路径
XJY-220/43AC220V静态信号继电器