This problem looks busy at first, but the accepted solution is built around one steady invariant. For 62. Unique Paths, 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?
Reach for this shape when a direct simulation would work logically but waste time revisiting the same information. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are uniquePaths, row.
Guide
Why?
The code is doing bookkeeping so your brain does not have to keep the entire search space open at once.
- 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:
- Start from the smallest reliable state.
- Expand one legal move at a time.
- Cache, count, or merge information as soon as it becomes settled.
- Let the final stored value answer the original question.
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 54.06% of C++ online submissions for Unique Paths.
02//Memory Usage: 8.6 MB, less than 51.56% of C++ online submissions for Unique Paths.
03
04class Solution {
05public:
06 int uniquePaths(int m, int n) {
07 vector<vector<int> > grid(m, vector<int>(n));
08 // cout << m << " " << n << endl;
09 //range of the length of diagonal: 0 ~ (max possible i) + (max possible j)
10 for(int diag = 0; diag <= (n-1) + (m-1); diag++){
11 //i starts from the first position that j >= 0
12 //i is increased in the loop and its upper bound is m-1
13 //j + i must = diag
14 //j is decreased in the loop and its lower bound is 0
15 //dynamic programming
16 for(int i = max(0, diag-(n-1)), j = diag - i; i <= m-1 && j >= 0; i++, j--){
17 // cout << i << " " << j << endl;
18 if(i == 0 || j == 0){
19 grid[i][j] = 1;
20 }else{
21 grid[i][j] = grid[i-1][j] + grid[i][j-1];
22 }
23 }
24 }
25
26 return grid[m-1][n-1];
27 }
28};
29
30//Runtime: 0 ms, faster than 100.00% of C++ online submissions for Unique Paths.
31//Memory Usage: 6.5 MB, less than 100.00% of C++ online submissions for Unique Paths.
32class Solution {
33public:
34 int uniquePaths(int m, int n) {
35 vector<vector<int>> dp(m+1, vector(n+1, 0));
36
37 //base case
38 dp[1][1] = 1;
39
40 for(int i = 1; i <= m; i++){
41 for(int j = 1; j <= n; j++){
42 if(i == 1 && j == 1) continue;
43 dp[i][j] = dp[i-1][j] + dp[i][j-1];
44 }
45 }
46
47 return dp[m][n];
48 }
49};
50
51//DP, O(n) space
52//https://leetcode.com/problems/unique-paths/discuss/22954/C%2B%2B-DP
53class Solution {
54public:
55 int uniquePaths(int m, int n) {
56 //base case: top boundary and left boundary
57 // vector<vector<int>> dp(m, vector(n, 1));
58 vector<int> row(n, 1);
59
60 for(int i = 1; i < m; i++){
61 for(int j = 1; j < n; j++){
62 // dp[i][j] = dp[i-1][j] + dp[i][j-1];
63 /*
64 row[j] represnets for the value in last row,
65 just like dp[i-1][j]
66 */
67 row[j] = row[j] + row[j-1];
68 }
69 }
70
71 // return dp[m-1][n-1];
72 return row[n-1];
73 }
74};
Cost