当前位置:网站首页>Leetcode question brushing series -- 931. Minimum sum of descent path
Leetcode question brushing series -- 931. Minimum sum of descent path
2022-07-26 00:02:00 【On the River continent, Mu Shui】
To give you one n x n Of square An array of integers matrix , Please find out and return through matrix The descent path Of Minimum and .
descent path You can start with any element in the first line , And select an element from each row . The element selected in the next row is at most one column apart from the element selected in the current row ( That is, the first element directly below or diagonally left or right ). say concretely , Location (row, col) The next element of should be (row + 1, col - 1)、(row + 1, col) perhaps (row + 1, col + 1) .
Example 1:
Input :matrix = [[2,1,3],[6,5,4],[7,8,9]]
Output :13
explain : As shown in the figure , And the smallest two descent paths
Example 2:
Input :matrix = [[-19,57],[-40,-5]]
Output :-59
explain : As shown in the figure , Is the minimum descent path
source : Power button (LeetCode)
link :https://leetcode.cn/problems/minimum-falling-path-sum
Ideas :
Define a two-dimensional array dp[][] ,dp[i][j] From the first line to the current element matrix[i][j] Descent path and minimum value .
set up matrix The number of rows is rowLen, The number of columns is colmLen
1. initialization
dp[0][j] = matrix[0][j]
2. State shift
1) j>0 && j< colmLen - 1
dp[i][j] = min(dp[i-1][j-1], dp[i-1][j],dp[i-1][j+1]) + matrix[i][j]
2) j == 0
dp[i][j] = min(dp[i-1][j],dp[i-1][j+1]) + matrix[i][j]
3) j == colmLen - 1
dp[i][j] = min(dp[i-1][j],dp[i-1][j-1]) + matrix[i][j]
3. Traverse i == rowLen The elements of , Find the minimum , That's what the title asks for java Code :
class Solution {
public int minFallingPathSum(int[][] matrix) {
int rowLen = matrix.length;
int colmLen = matrix[0].length;
int[][] dp = new int[rowLen][colmLen];
// 1. initialization
for(int j=0;j<colmLen;j++) {
dp[0][j] = matrix[0][j];
}
// 2. State shift
for(int i=1;i<rowLen;i++) {
for(int j=0;j<colmLen;j++) {
if(j==0) {
dp[i][j] = Math.min(dp[i-1][j],dp[i-1][j+1]) + matrix[i][j];
} else if(j==colmLen-1) {
dp[i][j] = Math.min(dp[i-1][j],dp[i-1][j-1]) + matrix[i][j];
} else {
dp[i][j] = Math.min(dp[i-1][j],dp[i-1][j-1]);
dp[i][j] = Math.min(dp[i][j],dp[i-1][j+1]) + matrix[i][j];
}
}
}
// 3. Traverse to find the minimum
int result = Integer.MAX_VALUE;
for(int j=0;j<colmLen;j++) {
result = Math.min(result,dp[rowLen-1][j]);
}
return result;
}
}边栏推荐
- 痞子衡嵌入式:MCUXpresso IDE下将源码制作成Lib库方法及其与IAR,MDK差异
- Quick sorting of top ten sorting
- 【ManageEngine】ServiceDesk Plus荣获2022安全样板工程数据安全奖
- Bubble sort idea and Implementation
- 安全文档归档软件
- Exercise (3) create a list set (both ArrayList and LinkedList)
- [ManageEngine] servicedesk plus won the 2022 safety model engineering data safety award
- Prometheus 运维工具 Promtool (二) Query 功能
- 二叉树——112. 路径总和
- Programming password guessing game
猜你喜欢
随机推荐
Program environment and pretreatment
ArcGIS cuts TIF images (grid data) according to the vector range, merges shp files in batches, cuts vectors in the region according to the vector range, outputs the geographic coordinate system, conve
Exercise (2) create a set to store the elements "1", "$", "2", "$", "3", "$", "4"“
Macro task, micro task and event cycle mechanism
Shardingsphere data slicing
二叉树——257. 二叉树的所有路径
【一库】mapbox-gl!一款开箱即用的地图引擎
1223. Dice simulation range DP
【ManageEngine】ServiceDesk Plus荣获2022安全样板工程数据安全奖
Solve the problem of rapid index bar extrusion
Scroll case: return to top with animation
Article 75: writing skills of academic papers
Cherish time and improve efficiency
String functions and memory operation functions
模块二作业
行为型模式之迭代器模式
QT smart pointer error prone point
What is parity? How to use C language?
【英雄哥七月集训】第 24天: 线性树
Vscode format JSON file







![[learning notes] solid works operation record](/img/f7/0535b473755643ce32996f00fa5554.png)

