### 9.5 1. Unique Paths
There is a robot on an m x n grid. The robot is initially located at the top-left corner (i.e., `grid[0][0]). The robot tries to move to the bottom-right corner (i.e., grid[m - 1][n - 1]). The robot can only move either down or right at any point in time.
Given the two integers m and n, return the number of possible unique paths that the robot can take to reach the bottom-right corner.
The test cases are generated so that the answer will be less than or equal to 2 * $10^{9}$.
![[uniquePathExample.png]]
62.不同路径
本题大家掌握动态规划的方法就可以。 数论方法 有点非主流,很难想到。
https://leetcode.cn/problems/unique-paths/description/
https://programmercarl.com/0062.%E4%B8%8D%E5%90%8C%E8%B7%AF%E5%BE%84.html
https://www.bilibili.com/video/BV1ve4y1x7Eu
自己写遇到的问题:
1.二维数组初始化一整行/一整列:for循环遍历
2.二维数组递推公式
`dp[i][j] = dp[i-1][j] + dp[i][j-1];
如何写循环?递推公式遍历顺序
```java
public class uniquePath {
public int uniquePaths(int m, int n) {
//这里初始化二维数组一定要把第二个维度也初始化,否则无法初始化dp。
int[][] dp = new int[m+1][n+1];
if(m == 1 || n == 1) return 1;
for (int i = 1; i <= m; i++) {
dp[i][1] = 1;
}
for (int i = 1; i <= n; i++) {
dp[1][i] = 1;
}
for (int i = 2; i <= m; i++) {
for (int j = 2; j <= n; j++) {
dp[i][j] = dp[i-1][j] + dp[i][j-1];
}
}
return dp[m][n];
}
}
class uniquePathTest {
public static void main(String[] args) {
int m = 3;
int n = 7;
uniquePath example = new uniquePath();
System.out.println(example.uniquePaths(m,n));
}
}
```
### 9.6 63. Unique Paths II
You are given an m x n integer array grid. There is a robot initially located at the top-left corner (i.e., `grid[0][0]). The robot tries to move to the bottom-right corner (i.e., grid[m - 1][n - 1]). The robot can only move either down or right at any point in time.
An obstacle and space are marked as 1 or 0 respectively in grid. A path that the robot takes cannot include any square that is an obstacle.
Return the number of possible unique paths that the robot can take to reach the bottom-right corner.
The testcases are generated so that the answer will be less than or equal to $2 * 10^{9}$.
![[uniquePath2Example.png]]
63. 不同路径 II
https://leetcode.cn/problems/unique-paths-ii/description/
https://programmercarl.com/0063.%E4%B8%8D%E5%90%8C%E8%B7%AF%E5%BE%84II.html#%E7%AE%97%E6%B3%95%E5%85%AC%E5%BC%80%E8%AF%BE
https://www.bilibili.com/video/BV1Ld4y1k7c6
### 9.7 343. Integer Break
Given an integer n, break it into the sum of k positive integers, where k >= 2, and maximize the product of those integers.
Return the maximum product you can get.
343. 整数拆分 (可跳过)
本题思路并不容易想,一刷建议可以跳过。如果学有余力,可以看视频理解一波。
https://leetcode.cn/problems/integer-break/description/
https://programmercarl.com/0343.%E6%95%B4%E6%95%B0%E6%8B%86%E5%88%86.html
https://www.bilibili.com/video/BV1Mg411q7YJ
自己写遇到的问题:
如果最后一排全是障碍物怎么办?
不知道最后几排有多少障碍物。
答:如果最后一个是障碍物,那直接返回0即可。(题目里示意图的星星是终点的意思,不是障碍物!)
```java
public class uniquePath2 {
public int uniquePathsWithObstacles(int[][] obstacleGrid) {
int m = obstacleGrid.length;
int n = obstacleGrid[0].length;
if(obstacleGrid[0][0] == 1 || obstacleGrid[m-1][n-1] == 1 ) return 0;
//if(m == 1 || n == 1) return 1;//这次不可以简单地这样剪枝了,因为这其中可能有障碍,那最后还是0
int[][] dp = new int[m][n];
// for (int i = 0; i < m ; i++) {
// if(obstacleGrid[i][0] == 1){
// dp[i][0] = 0;
// }else{
// dp[i][0] = 1;
// }
// }
//可精简为:
for (int i = 0; i < m && obstacleGrid[i][0] == 0; i++) {
dp[i][0] = 1;
}
//下同
for (int i = 0; i < n && obstacleGrid[0][i] == 0; i++) {
dp[0][i] = 1;
}
for (int i = 1; i < m; i++) {
for (int j = 1; j < n; j++) {
if(obstacleGrid[i][j] == 0){
dp[i][j] = dp[i-1][j] + dp[i][j-1];
}
}
}
return dp[m-1][n-1];
}
}
class uniquePath2Test {
public static void main(String[] args) {
int[][] obstacleGrid = {
{0,0,0},{0,1,0},{0,0,0},{0,0,1}
};
uniquePath2 example = new uniquePath2();
System.out.println(example.uniquePathsWithObstacles(obstacleGrid));
}
}
```
### 9.8 96. Unique Binary Search Trees(未做)
Given an integer n, return the number of structurally unique BST's (binary search trees) which has exactly n nodes of unique values from 1 to n.
https://leetcode.cn/problems/unique-binary-search-trees/description/
96..不同的二叉搜索树 (可跳过)
本题思路并不容易想,一刷建议可以跳过。 如果学有余力,可以看视频理解一波。
https://programmercarl.com/0096.%E4%B8%8D%E5%90%8C%E7%9A%84%E4%BA%8C%E5%8F%89%E6%90%9C%E7%B4%A2%E6%A0%91.html
https://www.bilibili.com/video/BV1eK411o7QA