当前位置:网站首页>Force buckle 64 minimum path sum -- Introduction to dynamic programming
Force buckle 64 minimum path sum -- Introduction to dynamic programming
2022-07-19 13:03:00 【Tension √】
Title Description
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 .
Their thinking
- State definition :grid [ i ][ j ] Defined as a point ( i , j ) Minimum path at ;
- State transition equation : At this time, it is generally discussed in four categories
When both the left and upper edges are matrix boundaries : When i = 0, j = 0 when , In fact, that is The starting point , grid [ i ] [ j ] = grid [ i ] [ j ];
When neither the left nor the upper edge is a matrix boundary : When i and j Not for 0 , grid [ i ] [ j ] = min( grid [ i -1 ] [ j ] , grid [ i ] [ j - 1] ) + grid [ i ] [ j ];
When only the left is the matrix boundary : Only from above , When i = 0, j It's not equal to 0 when , grid [ i ] [ j ] = grid [ i ] [ j - 1] + grid [ i ] [ j ];
When only the upper edge is the matrix boundary : Only from the left , When i Not for 0 , j = 0 when , grid [ i ] [ j ] = grid [ i - 1] [ j ] + grid [ i ] [ j ];
- The final return value returns to the state in the lower right corner of the matrix :return grid[m-1][n-1];
I / O example

Code
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];
}
}边栏推荐
- 面试难题:分布式 Session 实现难点,这篇就够!
- MOF customized materials | ultra thin MOF nanobelts | magnetic Fe3O4 @cd-mof nanocomposites | zif-8/ graphene composite nanoparticles
- Three minutes to understand the primary key, foreign key, non empty, unique and default constraints in mysql, and how to create a table
- How can MySQL delete data tables and associated data tables
- 最懂你的服装设计师是AI?让用户 “凭心意” 生成数字服装#Adidas OZWORLD
- Cadmium sulfide supported mil-125 (TI) | streptavidin (SA) - zirconium porphyrin MOF composite (PCN- [email protected] )|Shell core
- The difference and use between get request and post request
- Acwing4405. Statistical submatrix
- [C language programming 7] BTB model
- Audio common terminal anatomy - never make a mistake again
猜你喜欢

Amino metal organic framework material Fe MOF, fe-mil-88nh2 | Zr based metal organic framework catalyst (pt-uio-66) | Qiyue biology

Review the 2008 financial crisis and become a long-term investor 2020-03-19

【Pygame 学习笔记】6.Cursor 鼠标光标

Which domestic API tool is better? It turned out to be it

CMOS开关学习(一)

Preparation Notes: Matplotlib learning notes a

The latest Jilin construction safety officer simulation question bank and answers in 2022

松下A6伺服驱动器外部绝对值光栅尺全闭环参数设置

Yu Meimei, Ji Gongdu

VMware imports ova/ovf virtual machine files
随机推荐
Go unit test
国产API工具哪家强?原来是它…
XML file parsing
Common bug precautions of audio control
备赛笔记:matplotlib学习笔记a
O & M LITTLE WHITE Growth record - architecture week 6
Unveiling secrets of matrixcube 101 - functions and architecture of matrixcube
Equivalent domain name
Which domestic API tool is better? It turned out to be it
10 minutes to customize the pedestrian analysis system, detection and tracking, behavior recognition, human attributes all in one!
Brief analysis of circuit fault
Is the career direction of test / development programmers over 35 a turning point in the workplace?
Can you view MySQL data table structure in two ways?
深度梳理:机器学习建模调参方法总结
大气非等晕效应
Label ball problem
Google developer community sharing - flutter animation sharing has been released
收益风险比:投资机会最重要指标 2020-03-14
Yunxi focuses on store broadcast solutions to accelerate global layout
Acwing4405. Statistical submatrix