FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
leetcode-cpp-practices/62. Unique Paths.cpp at master · keineahnung2345/leetcode-cpp-practices · GitHub
keineahnung2345
leetcode-cpp-practices
Repository navigation
Code
Issues
Pull requests
3
(3)
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
leetcode-cpp-practices
/
62. Unique Paths.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
74 lines (65 loc) · 2.34 KB
Breadcrumbs
leetcode-cpp-practices
/
62. Unique Paths.cpp
Copy path
File metadata and controls
74 lines (65 loc) · 2.34 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
//
Runtime: 4 ms, faster than 54.06% of C++ online submissions for Unique Paths.
//
Memory Usage: 8.6 MB, less than 51.56% of C++ online submissions for Unique Paths.
class
Solution
{
public:
int
uniquePaths
(
int
m,
int
n) {
vector<vector<
int
> >
grid
(m, vector<
int
>(n));
//
cout << m << " " << n << endl;
//
range of the length of diagonal: 0 ~ (max possible i) + (max possible j)
for
(
int
diag =
0
; diag <= (n-
1
) + (m-
1
); diag++){
//
i starts from the first position that j >= 0
//
i is increased in the loop and its upper bound is m-1
//
j + i must = diag
//
j is decreased in the loop and its lower bound is 0
//
dynamic programming
for
(
int
i =
max
(
0
, diag-(n-
1
)), j = diag - i; i <= m-
1
&& j >=
0
; i++, j--){
//
cout << i << " " << j << endl;
if
(i ==
0
|| j ==
0
){
grid[i][j] =
1
;
}
else
{
grid[i][j] = grid[i-
1
][j] + grid[i][j-
1
];
}
}
}
return
grid[m-
1
][n-
1
];
}
};
//
Runtime: 0 ms, faster than 100.00% of C++ online submissions for Unique Paths.
//
Memory Usage: 6.5 MB, less than 100.00% of C++ online submissions for Unique Paths.
class
Solution
{
public:
int
uniquePaths
(
int
m,
int
n) {
vector<vector<
int
>>
dp
(m+
1
,
vector
(n+
1
,
0
));
//
base case
dp[
1
][
1
] =
1
;
for
(
int
i =
1
; i <= m; i++){
for
(
int
j =
1
; j <= n; j++){
if
(i ==
1
&& j ==
1
)
continue
;
dp[i][j] = dp[i-
1
][j] + dp[i][j-
1
];
}
}
return
dp[m][n];
}
};
//
DP, O(n) space
//
https://leetcode.com/problems/unique-paths/discuss/22954/C%2B%2B-DP
class
Solution
{
public:
int
uniquePaths
(
int
m,
int
n) {
//
base case: top boundary and left boundary
//
vector<vector<int>> dp(m, vector(n, 1));
vector<
int
>
row
(n,
1
);
for
(
int
i =
1
; i < m; i++){
for
(
int
j =
1
; j < n; j++){
//
dp[i][j] = dp[i-1][j] + dp[i][j-1];
/*
row[j] represnets for the value in last row,
just like dp[i-1][j]
*/
row[j] = row[j] + row[j-
1
];
}
}
//
return dp[m-1][n-1];
return
row[n-
1
];
}
};
Back
|
FazBrowse Home
|
New Git URL