当前位置:网站首页>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];
}
}
边栏推荐
- Tiktok practice ~ sharing module ~ copy short video link
- Tiktok practice ~ sharing module ~ generate short video QR code
- 回首望月
- Bonne Recommandation: développer des outils de sécurité pour les terminaux mobiles
- [serialization] how to master the core technology of opengauss database? Secret 5: master database security (6)
- 【贝叶斯分类2】朴素贝叶斯分类器
- Mongodb implements creating and deleting databases, creating and deleting tables (sets), and adding, deleting, modifying, and querying data
- Unity——Mathf. Similarities and differences between atan and atan2
- 云计算技术的发展与芯片处理器的关系
- 30. 串联所有单词的子串
猜你喜欢

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

mongoDB的三种基础备份方法

郭明錤:苹果 AR / MR 头显是其有史以来设计最复杂的产品,将于 2023 年 1 月发布

Muke 8. Service fault tolerance Sentinel

Review of watermelon book (VII): Bayesian classifier (manual push + code demo)

MySQL - database creation and management

Daily basic use of alicloud personal image warehouse

windows系統下怎麼安裝mysql8.0數據庫?(圖文教程)

QT两种方法实现定时器

Gee: calculate the maximum and minimum values of pixels in the image area
随机推荐
[Shandong University] information sharing for the first and second examinations of postgraduate entrance examination
C language file cursor fseek
【贝叶斯分类3】半朴素贝叶斯分类器
vue中缓存组件keep-alive
Sentinelresource annotation details
Garbage collection mechanism of browser
Dynamic parameter association using postman
Looking back at the moon
Stringutils judge whether the string is empty
浏览器事件循环
MongoDB实现创建删除数据库、创建删除表(集合)、数据增删改查
GameFi 活跃用户、交易量、融资额、新项目持续性下滑,Axie、StepN 能摆脱死亡螺旋吗?链游路在何方?
回首望月
网上办理中金财富开户安全吗?
飞天+CIPU体为元宇宙带来更大想象空间
Mr. Sun's version of JDBC (21:34:25, June 12, 2022)
The source code that everyone can understand (I) the overall architecture of ahooks
C primer plus learning notes - 3. Character IO (input / output)
C exercise. Class list plus records, display records and clear records
Some cold knowledge about QT database development