当前位置:网站首页>力扣64-最小路径和——动态规划入门题型
力扣64-最小路径和——动态规划入门题型
2022-07-17 17:15:00 【张怼怼√】
题目描述
给定一个包含非负整数的 m x n 网格 grid ,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小。
说明:每次只能向下或者向右移动一步。
解题思路
- 状态定义:grid [ i ][ j ] 被定义为点( i , j )处的最小路径;
- 状态转移方程:此时一般要分四类情况讨论
当左边和上边都是矩阵边界时: 即当 i = 0, j = 0时,其实就是起点, grid [ i ] [ j ] = grid [ i ] [ j ];
当左边和上边都不是矩阵边界时: 即当 i 和 j 都不为 0 , grid [ i ] [ j ] = min( grid [ i -1 ] [ j ] , grid [ i ] [ j - 1] ) + grid [ i ] [ j ];
当只有左边是矩阵边界时: 只能从上面来,即当 i = 0, j 不等于 0 时, grid [ i ] [ j ] = grid [ i ] [ j - 1] + grid [ i ] [ j ];
当只有上边是矩阵边界时: 只能从左面来,即当 i 不为 0 , j = 0 时, grid [ i ] [ j ] = grid [ i - 1] [ j ] + grid [ i ] [ j ];
- 最终返回值返回矩阵右下角的状态即可:return grid[m-1][n-1];
输入输出示例

代码
class Solution {
public int minPathSum(int[][] grid) {
int m = grid.length;
int n = grid[0].length;
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
if(i == 0 && j ==0) grid[i][j] = grid[i][j];
else if(i == 0){
grid[i][j] = grid[i][j-1] + grid[i][j];
}else if(j == 0){
grid[i][j] = grid[i-1][j] + grid[i][j];
}else {
grid[i][j] = Math.min(grid[i-1][j],grid[i][j-1]) + grid[i][j];
}
}
}
return grid[m-1][n-1];
}
}边栏推荐
- 最小交換次數
- In depth sorting: summary of machine learning modeling and parameter adjustment methods
- Which domestic API tool is better? It turned out to be it
- Learning record: call TFTLCD
- Minimum exchange times
- LeetCode_ Prefix and_ Medium_ 523. Continuous subarrays and
- Lanthanide metal organic skeleton( [email protected] )|Rhodamine 6G modified MOF material | catalase @zif composite | MOF
- 2022 safety officer-c certificate title and answer
- Knowledge sorting of MySQL
- Acwing4405. Statistical submatrix
猜你喜欢

Yunxi and Tencent cloud have reached a strategic cooperation to accelerate the expansion of the global live broadcast market

国产API工具哪家强?原来是它…

GO 单元测试

Structure memory alignment, bit segment, union

OpenCV:06形态学

面试难题:分布式 Session 实现难点,这篇就够!

Logical operator 1 (Gretel software - Jiuye training)

10 minutes to customize the pedestrian analysis system, detection and tracking, behavior recognition, human attributes all in one!

Azkaban installation documentation

About the "bottom reading" mentality, it makes you exhausted 2020-03-15
随机推荐
O & M LITTLE WHITE Growth record - architecture week 6
Mysql database tables add fields, delete fields, modify the arrangement of fields and other operations, but not soon
回顾2008年金融危机,做长期主义投资者 2020-03-19
备赛笔记:matplotlib学习笔记a
基于STM32设计的云端健康管理系统(采用阿里云物联网平台)
PostgreSQL function usage record
MatrixCube揭秘 101——MatrixCube的功能与架构
Stable super odds, 9 years old mixin | 2022 Jincang innovative product launch was successfully held
Att & CK actual combat series - red team actual combat (-)
Do you still need to release the database connection manually with typeorm
運維小白成長記—架構第6周
整理了一份通用的内存管理驱动代码
RingBuffer
Three minutes to understand the primary key, foreign key, non empty, unique and default constraints in mysql, and how to create a table
Uio-66 | fe3o4/cu3 (BTC) 2 metal organic framework (MOF) nanocomposites supported on silver nanoparticles | nagdf4:yb, er upconversion nanoparticles @zif-8
R语言--Cox模型校准曲线原理(一)数据来源
最小交换次数
Preparation Notes: Matplotlib learning notes a
Nitrogen heterocyclic molecule modified uio-66-nh2 | polyethyleneimine modified uio-66-nh2| [email protected] @Zif67 nanomaterial
松下A6伺服驱动器外部绝对值光栅尺全闭环参数设置