当前位置:网站首页>【动态规划】路径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;
}
}
边栏推荐
- Exploring the road of steam education innovation in the Internet Era
- 技术人进阶画业务大图,手把手教学来了
- neo4j安装、运行以及项目的构建和功能实现
- 機器人編程的培訓學科類原理
- Installing mongodb database in Windows Environment
- StrictMode分析Registion-StrictMode原理(4)
- Solve idea:class' xxx 'not found in module' xxx‘
- 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"?
- StrictMode卡顿与泄漏检测-StrictMode原理(2)
- 基础知识之三——标准单元库
猜你喜欢

Digital IC design process summary

数字IC设计流程总结

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

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

Poor students can also play raspberry pie
![[问题已处理]-nvidia-smi命令获取不到自身容器的GPU进程和外部的GPU进程号](/img/51/e48e222c14f4a4e9f2be91a677033f.png)
[问题已处理]-nvidia-smi命令获取不到自身容器的GPU进程和外部的GPU进程号

Installing mongodb database in Windows Environment

dc_labs--lab1的学习与总结

Xjy-220/43ac220v static signal relay

Exploration and practice of "flow batch integration" in JD
随机推荐
Windows环境下安装MongoDB数据库
OCR的一些项目
pull_ to_ refresh
基础知识之一——STA基础概述
Open3D 点云颜色渲染
XJY-220/43AC220V静态信号继电器
gin_gorm
Looksrare team's "cash out" caused disturbance
Double position relay dls-5/2 dc220v
fluttertoast
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"?
自定义注解实现校验
Interpreting the scientific and technological literacy contained in maker Education
(学习力+思考力) x 行动力,技术人成长的飞轮效应总结
StrictMode分析Registion-StrictMode原理(4)
Unknown database连接数据库错误
流批一体在京东的探索与实践
Kongyiji's first question: how much do you know about service communication?
Impact relay zc-23/dc220v
System.CommandLine版CSRebot