forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobzva.cpp
More file actions
31 lines (27 loc) ยท 923 Bytes
/
obzva.cpp
File metadata and controls
31 lines (27 loc) ยท 923 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
/**
* ํ์ด
* - ์กฐํฉ ๊ณต์์ ์ฌ์ฉํ๋ฉด overflow ๋ฐ ์๊ฐ์ด๊ณผ๋ฅผ ์ผ์ผํฌ ์ ์์ต๋๋ค
* - ๋ชจ๋ ์ขํ์ ๋ํด uniquePaths๋ฅผ ๊ณ์ฐํ๋ ๋ฐฉ์์ ์ฌ์ฉํฉ๋๋ค
* - ํน์ ์ขํ์ uniquePaths๋ฅผ ๊ณ์ฐํ๊ธฐ ์ํด์๋ ๋ ํ๋ง ํ์ํ๊ธฐ ๋๋ฌธ์ ๊ธธ์ด m์ ๋ฐฐ์ด ๋ ๊ฐ๋ฅผ ์ด์ฉํฉ๋๋ค
*
* Big O
* - Time complexity: O(MN)
* - Space compexity: O(N)
*/
class Solution {
public:
int uniquePaths(int m, int n) {
vector<int> row1;
vector<int> row2;
for (int i = 0; i < n; ++i) row1.push_back(1);
row2.push_back(1);
for (int i = 1; i < n; ++i) row2.push_back(0);
for (int j = 1; j < m; ++j) {
for (int i = 1; i < n; ++i) row2[i] = row1[i] + row2[i - 1];
swap(row1, row2);
row2[0] = 1;
for (int i = 1; i < n; ++i) row2[i] = 0;
}
return row1[n - 1];
}
};