当前位置:网站首页>[dynamic planning] path dp:931 Minimum Falling Path Sum
[dynamic planning] path dp:931 Minimum Falling Path Sum
2022-07-01 01:30:00 【Twilight_ years】
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).
Go down from the first line , You can walk three places at a time , Go to the last line , Find the minimum path sum
Pay attention to initialization :
dp[i][j]=INF
dp Subscript to be 1 Express g[0],dp[0][j]=0( Avoid boundary discussions )
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;
}
}
边栏推荐
- Zero of DC learning notes -- overview and basic process introduction
- StrictMode分析Activity泄漏-StrictMode原理(3)
- Technical personnel advanced to draw a big picture of business, hand-in-hand teaching is coming
- Pytorch programming knowledge (2)
- ESP8266 RC522
- 解读创客教育所蕴含的科技素养
- Visual studio 2019 shortcut notes
- mysql插入\更新前+判断条件
- visual studio 2019 快捷键备忘
- 分割链表[先取next再斩断链表防止断链]
猜你喜欢

3dsmax插件开发遍历节点对象和Object获取及INode变换矩阵说明

Why not two or four TCP handshakes

"Open math input panel" in MathType editing in win11 is gray and cannot be edited

友盟(软件异常实时监听的好帮手:Crash)接入教程(有点基础的小白最易学的教程)

Training discipline principle of robot programming

Docker 部署 MySQL 8

用recyclerReview展示Banner,很简单

Kongyiji's first question: how much do you know about service communication?

Service grid ASM year end summary: how do end users use the service grid?

Docker deployment MySQL 8
随机推荐
1175. Prime Arrangements
gin 配置文件
Strictmode analysis activity leakage -strictmode principle (3)
Poor students can also play raspberry pie
Complete software development process
dc_ Study and summary of labs--lab1
Applet Custom Grid
StrictMode带来的思考-StrictMode原理(5)
图的连通性基础
未来的 Web3会带来什么?
45岁程序员告诉你:程序员为什么要跳槽,太真实...
Parity linked list [two general directions of linked list operation]
Windows环境下安装MongoDB数据库
微生物检测,土壤微生物的作用有哪些?
Openmv and k210 of the f question of the 2021 video game call the openmv API for line patrol, which is completely open source.
Open3d point cloud color rendering
Interpreting the scientific and technological literacy contained in maker Education
Green, green the reed. dew and frost gleam.
gin_gorm
"Open math input panel" in MathType editing in win11 is gray and cannot be edited