当前位置:网站首页>Sword finger offer II 098 Number of paths / Sword finger offer II 099 Sum of minimum paths
Sword finger offer II 098 Number of paths / Sword finger offer II 099 Sum of minimum paths
2022-06-26 20:53:00 【Biqiliang】
The finger of the sword Offer II 098. Number of paths 【 Medium question 】
Ideas :【 Dynamic programming 】
Second order dp Array :dp[i][j] Indicates walking from the upper left corner to the subscript (i,j) The number of paths required for the location of .
The boundary conditions :dp[0][0]=1
i = 0 when :
The end point is at the upper boundary of the matrix ,dp[i][j] The value of depends only on the left point dp
namely dp[0][j] = dp[0][j-1]
j = 0 when :
The end point is at the left boundary of the matrix ,dp[i][j] The value of depends only on the upper side point dp
namely dp[i][0] = dp[i-1][0]
In fact, if the points of the left boundary and the upper boundary start from the upper left corner , There is only one way to get there ,
namely dp[0][j] = 1,dp[i][0] = 1.
Transfer equation :
dp[i][j] = dp[i-1][j] + dp[i][j-1]
Code :
class Solution {
public int uniquePaths(int m, int n) {
// Dynamic programming
// Definition dp Array , Said go (i,j) Number of paths available for location
int[][] dp = new int[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (i > 0 && j > 0){
// Transfer equation
// Go to the (i,j) The number of paths to a position is determined by the number of paths to its left and upper sides
dp[i][j] = dp[i-1][j] + dp[i][j-1];
}else {
// The boundary conditions
// //1. Starting point of upper left corner
// if (i == 0 && j == 0){
// dp[0][0] = 1;
// }else if (i == 0){//2. Upper boundary
// dp[0][j] = dp[0][j-1];
// }else {//3. Left boundary
// dp[i][0] = dp[i-1][0];
// }
dp[i][j] = 1;
}
}
}
//(m-1,n-1) The location is m×n The lower right corner of the grid ,dp The number of paths of the array is always calculated from the upper left corner , therefore dp[m-1][n-1] That is, the total number of paths required by the topic
return dp[m-1][n-1];
}
}
The finger of the sword Offer II 099. Sum of minimum paths 【 Medium question 】
Ideas :【 Dynamic programming 】
Refer to the above question for solving ideas , take dp[i][j] The value of is defined as going from the upper left corner of the matrix to (i,j) The minimum path sum of the position is .
Code :
class Solution {
public int minPathSum(int[][] grid) {
// Dynamic programming
// Exclude special inputs
if (grid == null || grid.length == 0 || grid[0].length == 0){
return 0;
}
int m = grid.length,n = grid[0].length;
// Define an array dp dp[i][j] Represents walking from the upper left corner of the matrix to (i,j) The minimum path and
int[][] dp = new int[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (i > 0 && j > 0){
// Transfer equation
dp[i][j] = Math.min(dp[i-1][j],dp[i][j-1]) + grid[i][j];
}else {
// The boundary conditions
if (i == 0 && j == 0){
dp[0][0] = grid[0][0];
}else if (i == 0){
dp[0][j] = dp[0][j-1] + grid[0][j];
}else {
dp[i][0] = dp[i-1][0] + grid[i][0];
}
}
}
}
//dp[m-1][n-1] That is, the sum of the minimum paths from the upper left corner to the lower right corner of the matrix
return dp[m-1][n-1];
}
}
边栏推荐
- Developer survey: rust/postgresql is the most popular, and PHP salary is low
- C: Reverse linked list
- How to install mysql8.0 database under Windows system? (Graphic tutorial)
- 剑指 Offer II 098. 路径的数目 / 剑指 Offer II 099. 最小路径之和
- 两个文件 合并为第三个文件 。
- c语言99乘法表
- 【贝叶斯分类3】半朴素贝叶斯分类器
- Stop being a giant baby
- 基于SSH框架的学生信息管理系统
- 0 basic C language (3)
猜你喜欢

Detailed explanation of shutter textfield

mongoDB的三种基础备份方法

Tiktok practice ~ sharing module ~ short video download (save to photo album)

Tiktok practice ~ sharing module ~ copy short video link

分布式ID生成系统

Two methods of QT to realize timer

Guomingyu: Apple's AR / MR head mounted display is the most complicated product in its history and will be released in January 2023

QT两种方法实现定时器

Bonne Recommandation: développer des outils de sécurité pour les terminaux mobiles

Tiktok practice ~ search page ~ scan QR code
随机推荐
Sword finger offer II 091 Paint the house
Browser event loop
[Bayesian classification 3] semi naive Bayesian classifier
leetcode刷题:字符串06(实现 strStr())
超分之VRT
Development of NFT for digital collection platform
Tiktok practice ~ search page ~ video details
C primer plus学习笔记 —— 3、字符的IO(输入/输出)
BOM and DOM operations
孙老师版本JDBC(2022年6月12日21:34:25)
mysql存储过程
慕课8、服务容错-Sentinel
Tiktok practice ~ search page ~ scan QR code
When are global variables initialized before entering the main function?
Detailed explanation of shutter textfield
MySQL中存储过程的详细详解
开户可以在网上开么?能安全吗?
leetcode刷题:字符串05(剑指 Offer 58 - II. 左旋转字符串)
剑指 Offer II 098. 路径的数目 / 剑指 Offer II 099. 最小路径之和
GameFi 活跃用户、交易量、融资额、新项目持续性下滑,Axie、StepN 能摆脱死亡螺旋吗?链游路在何方?