The trick here is to name the state correctly, then let the implementation follow. For 63. Unique Paths II, the solution in this repository is mainly a dynamic programming solution.
Guide
What?
Before optimizing anything, pin down what information is still useful after each move. Instead of trying to be clever immediately, read the code as a sequence of questions:
- What state are we keeping?
- How do we move from one state to the next?
- When do we know the answer is already determined?
For this file, the main tools are: dynamic programming.
Guide
When?
Use this approach when the hard part is not syntax, but deciding what must stay true after every update. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are uniquePathsWithObstacles, row.
Guide
Why?
The point of the implementation is not to make the code longer. It is to avoid doing the same thinking twice.
- The two-dimensional vector is the memory of the solution: grid state, DP state, or adjacency shape.
- The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.
Guide
How?
Walk through the solution in this order:
- Read the setup variables first.
- Follow the main loop or recursive helper next.
- Watch where invalid states get skipped.
- Check which value survives to the return statement.
The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.
Guide
Complexity
- Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01//Runtime: 4 ms, faster than 75.49% of C++ online submissions for Unique Paths II.
02//Memory Usage: 9.2 MB, less than 86.67% of C++ online submissions for Unique Paths II.
03
04class Solution {
05public:
06 int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
07 int m = obstacleGrid.size(), n = obstacleGrid[0].size();
08 vector<vector<long> > grids(m+1, vector<long>(n+1));
09 //padding to the top and left
10 for(int i = 0; i < m+1; i++){
11 grids[i][0] = 0;
12 }
13 for(int j = 0; j < n+1; j++){
14 grids[0][j] = 0;
15 }
16
17 for(int diag = 0; diag <= (m-1) + (n-1); diag++){
18 for(int i = max(0, diag-(n-1)), j = diag - i; i <= m-1 && j >= 0; i++, j--){
19 if(obstacleGrid[i][j] == 1){
20 grids[i+1][j+1] = 0;
21 }else if(i == 0 && j == 0){
22 //it should be "if(i == 0 && j == 0)" rather than "if(i == 0 || j == 0)" here!
23 //we can only ensure that grids[0][0] is reachable, but grids[0][x] or grids[x][0]
24 grids[i+1][j+1] = 1;
25 }else{
26 grids[i+1][j+1] = grids[i-1+1][j+1] + grids[i+1][j-1+1];
27 }
28 // cout << i << " " << j << " " << obstacleGrid[i][j] << " " << grids[i+1][j+1] << endl;
29 }
30 }
31 // cout << endl << endl;
32
33 // for(int i = 0; i < m+1; Vi++){
34 // for(int j = 0; j < n+1; j++){
35 // cout << grids[i][j] << " ";
36 // }
37 // cout << endl;
38 // }
39 return grids[m-1+1][n-1+1];
40 }
41};
42
43//DP, shorter
44class Solution {
45public:
46 int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
47 int m = obstacleGrid.size(), n = obstacleGrid[0].size();
48
49 if(obstacleGrid[0][0] == 1 || obstacleGrid[m-1][n-1] == 1) return 0;
50
51 vector<vector<long long int>> dp(m+1, vector(n+1, 0LL));
52
53 dp[1][1] = 1;
54
55 for(int i = 1; i <= m; i++){
56 for(int j = 1; j <= n; j++){
57 if(i == 1 && j == 1) continue;
58 dp[i][j] = (obstacleGrid[i-1][j-1] == 1) ? 0 : (dp[i-1][j] + dp[i][j-1]);
59 // cout << dp[i][j] << " ";
60 }
61 // cout << endl;
62 }
63
64 return dp[m][n];
65 }
66};
67
68//DP, O(N) space
69class Solution {
70public:
71 int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
72 int m = obstacleGrid.size(), n = obstacleGrid[0].size();
73
74 if(obstacleGrid[0][0] == 1 || obstacleGrid[m-1][n-1] == 1) return 0;
75
76 vector<long long int> row(n+1, 0LL);
77
78 row[1] = 1;
79
80 for(int i = 1; i <= m; i++){
81 for(int j = 1; j <= n; j++){
82 if(i == 1 && j == 1) continue;
83 row[j] = (obstacleGrid[i-1][j-1] == 1) ? 0 : (row[j] + row[j-1]);
84 }
85 }
86
87 return row[n];
88 }
89};
Cost