当前位置:网站首页>Topic37——64. Minimum path sum
Topic37——64. Minimum path sum
2022-06-27 12:16:00 【_ Cabbage_】
subject : Given a... That contains a nonnegative integer m x n grid grid , Please find a path from the top left corner to the bottom right corner , Make the sum of the numbers on the path the smallest .
explain : You can only move down or right one step at a time .
Example 1:

Input :grid = [[1,3,1],[1,5,1],[4,2,1]]
Output :7
explain : Because the path 1→3→1→1→1 The sum of is the smallest .
Example 2:
Input :grid = [[1,2,3],[4,5,6]]
Output :12
Tips :
m == grid.length
n == grid[i].length
1 <= m, n <= 200
0 <= grid[i][j] <= 100
Double cycle :
class Solution {
public int minPathSum(int[][] grid) {
for(int i = 0; i < grid.length; i++) {
for(int j = 0; j < grid[0].length; j++) {
int temp = Integer.MAX_VALUE;
if(j-1 >= 0)
temp = Math.min(temp, grid[i][j-1]);
if(i-1 >= 0)
temp = Math.min(temp, grid[i-1][j]);
if(i-1 < 0 && j-1 < 0)
temp = 0;
grid[i][j] = grid[i][j] + temp;
}
}
return grid[grid.length-1][grid[0].length-1];
}
}
Official solution ( Improvement of the above method ):
class Solution {
public int minPathSum(int[][] grid) {
int[][] dp = new int[grid.length][grid[0].length];
dp[0][0] = grid[0][0];
for(int i = 1; i < grid.length; i++) {
dp[i][0] = dp[i-1][0] + grid[i][0];
}
for(int j = 1; j < grid[0].length; j++) {
dp[0][j] = dp[0][j-1] + grid[0][j];
}
for(int i = 1; i < grid.length; i++) {
for(int j = 1; j < grid[0].length; j++) {
dp[i][j] = grid[i][j] + Math.min(dp[i-1][j], dp[i][j-1]);
}
}
return dp[grid.length-1][grid[0].length-1];
}
}
边栏推荐
- Tidb 6.0: making Tso more efficient tidb Book rush
- 巅峰小店APP仿站开发玩法模式讲解源码分享
- Nifi from introduction to practice (nanny level tutorial) - identity authentication
- 优博讯出席OpenHarmony技术日,全新打造下一代安全支付终端
- [high frequency interview questions] difficulty 1.5/5, LCS template questions
- Secyun won the "2022 AI analysis · it operation and maintenance vendor panorama report" as the representative vendor of intelligent operation and maintenance aiops Market
- In 2021, the global professional liability insurance revenue was about USD 44740million, and it is expected to reach USD 55980million in 2028. From 2022 to 2028, the CAGR was 3.5%
- Mathematical knowledge -- ideas and examples of game theory (bash game, Nim game, wizov game)
- 【TcaplusDB知识库】TcaplusDB-tcapsvrmgr工具介绍(二)
- pull request
猜你喜欢

How to participate in openharmony code contribution

怎么找相同台词的影视片段?这8个电影搜索神器,一句台词找到对应片段

How to adjust an integer that is entered in Excel but always displays decimals?

Private dry goods sharing: how to implement platform in Enterprise Architecture

微服务拆分

Neo4j:入门基础(一)之安装与使用

Jwas: a Bayesian based GWAS and GS software developed by Julia

Detailed explanation of interprocess communication

一个有趣的网络掩码的实验

Topic37——64. 最小路径和
随机推荐
R语言使用epiDisplay包的poisgof函数对泊松回归(Poisson Regression)执行拟合优度检验、检验是否存在过度离散问题(overdispersion)
Tidb 6.0: making Tso more efficient tidb Book rush
Comment modifier Node Fichiers dans les modules
TiDB 6.0:让 TSO 更高效丨TiDB Book Rush
R语言使用glm函数构建泊松对数线性回归模型处理三维列联表数据构建饱和模型、使用step函数基于AIC指标实现逐步回归筛选最佳模型、解读分析模型
The DBSCAN function of FPC package in R language performs density clustering analysis on data, and the plot function visualizes the clustering graph
"24 of the 29 students in the class successfully went to graduate school" rushed to the hot search! Where are the remaining five?
秒云荣获《2022爱分析 · IT运维厂商全景报告》智能运维AIOps市场代表厂商
The GLM function of R language is used to build a binary logistic regression model (the family parameter is binomial), and the AIC function is used to compare the AIC values of the two models (simple
如何修改 node_modules 裏的文件
R语言dplyr包arrange函数排序dataframe数据、通过多个数据列排序dataframe数据、指定第一个字段降序排序,第二字段不指定(默认升序排序)
This privatized deployed enterprise knowledge base makes telecommuting a zero distance
在 Golang 中构建 CRUD 应用程序
The R language uses the DOTPLOT function of epidisplay package to visualize the frequency of data points in different intervals in the form of point graph, specifies the grouping parameters with the b
动态规划【三】(区间dp)石子合并
优博讯出席OpenHarmony技术日,全新打造下一代安全支付终端
干货!零售业智能化管理会遇到哪些问题?看懂这篇文章就够了
今晚战码先锋润和赛道第2期直播丨如何参与OpenHarmony代码贡献
Daily leetcode force deduction (21~25)
i. Construction of mx6ull C language environment