当前位置:网站首页>Topic37——64. 最小路径和
Topic37——64. 最小路径和
2022-06-27 11:41:00 【_卷心菜_】
题目:给定一个包含非负整数的 m x n 网格 grid ,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小。
说明:每次只能向下或者向右移动一步。
示例 1:

输入:grid = [[1,3,1],[1,5,1],[4,2,1]]
输出:7
解释:因为路径 1→3→1→1→1 的总和最小。
示例 2:
输入:grid = [[1,2,3],[4,5,6]]
输出:12
提示:
m == grid.length
n == grid[i].length
1 <= m, n <= 200
0 <= grid[i][j] <= 100
双重循环:
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];
}
}
官方解法(对上述方法的改进):
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];
}
}
边栏推荐
- [tcapulusdb knowledge base] Introduction to tcapulusdb general documents
- 面试突击60:什么情况会导致 MySQL 索引失效?
- R语言使用epiDisplay包的followup.plot函数可视化多个ID(病例)监测指标的纵向随访图、使用stress.labels参数在可视化图像中为强调线添加标签信息
- i. Construction of mx6ull C language environment
- R语言glm函数构建二分类logistic回归模型(family参数为binomial)、使用AIC函数比较两个模型的AIC值的差异(简单模型和复杂模型)
- Detailed explanation of interprocess communication
- R语言dplyr包arrange函数排序dataframe数据、通过多个数据列排序dataframe数据、指定第一个字段降序排序,第二字段不指定(默认升序排序)
- Nvme2.0 protocol - new features
- Summary of qstype class usage (III)
- Salesforce 容器化 ISV 场景下的软件供应链安全落地实践
猜你喜欢
![Dynamic programming [4] (counting class DP) example: integer partition](/img/06/4b3863bbb85582348c6f4ea7c5511e.png)
Dynamic programming [4] (counting class DP) example: integer partition

Matlab exercises - create 50 rows and 50 columns of all zero matrix, all 1 matrix, identity matrix, diagonal matrix, and output the 135 element of the matrix.

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

Unity shader learning (II) the first shader

从零开始搭建物联网系统

Youboxun attended the openharmony technology day to create a new generation of secure payment terminals

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

0 basic understanding of how e-commerce systems connect with payment channels

StarCraft's Bug King ia retired for 2 years to engage in AI, and lamented that it was inferior

最短编辑距离(线性dp写法)
随机推荐
Jwas: a Bayesian based GWAS and GS software developed by Julia
In depth analysis of error solutions and problems in dynamic loading of unity shadow and outline components
C# wpf 实现撤销重做功能
Informatics Olympiad all in one 1332: [example 2-1] weekend dance
千万不要错过,新媒体运营15个宝藏公众号分享
"24 of the 29 students in the class successfully went to graduate school" rushed to the hot search! Where are the remaining five?
0基础了解电商系统如何对接支付渠道
数学知识——博弈论(巴什博奕、尼姆博奕、威佐夫博奕)思路及例题
Redis distributed lock 15 ask, what have you mastered?
Daily leetcode force deduction (21~25)
内存四区(栈,堆,全局,代码区)
AUTOCAD——三种修剪方式
Unity Shader学习(一)认识unity shader基本结构
[tcapulusdb knowledge base] tcapulusdb system user group introduction
I.MX6ULL启动方式
【On nacos】快速上手 Nacos
怎么找相同台词的影视片段?这8个电影搜索神器,一句台词找到对应片段
In 2021, the global carbon graphite brush revenue is about US $2366million, and it is expected to reach US $2701.8 million in 2028
pull request
在 Golang 中构建 CRUD 应用程序