-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUniquePaths.cpp
More file actions
38 lines (31 loc) · 806 Bytes
/
UniquePaths.cpp
File metadata and controls
38 lines (31 loc) · 806 Bytes
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
// Leetcode https://leetcode.com/explore/interview/card/top-interview-questions-medium/111/dynamic-programming/808/
// https://leetcode.com/explore/interview/card/top-interview-questions-medium/111/dynamic-programming/808/discuss/22958/Math-solution-O(1)-space
#include <iostream>
int uniquePaths(int m, int n)
{
if (m == 1 || n == 1) {
return 1;
}
m--; n--;
if (m < n) {
std::swap(m, n);
}
long res = 1;
int j = 1;
for (int i = m + 1; i <= m + n; ++i, ++j) {
res = (res * i) / j;
}
return static_cast<int>(res);
}
void test(int m, int n)
{
std::cout << "m : " << m << ", n : " << n;
std::cout << ", Unique paths : " << uniquePaths(m , n) << "\n";
}
int main()
{
test(3, 7);
test(7, 3);
test(3, 3);
return 0;
}