forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsungjinwi.cpp
More file actions
31 lines (25 loc) ยท 876 Bytes
/
sungjinwi.cpp
File metadata and controls
31 lines (25 loc) ยท 876 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
/*
ํ์ด :
ํน์ ์นธ์ ์ค๋ ๋ฐฉ๋ฒ์ ์ผ์ชฝ์์ ์ค๊ฑฐ๋ ์์์ ์ค๊ฑฐ๋ ๋ ์ค ํ๋์ด๋ฏ๋ก ๋ ๊ณณ์ unique path ๊ฐ์๋ฅผ ๋ํ๋ฉด ๊ตฌํ ์ ์๋ค
ํ row ๋ง๋ค๊ณ ๋๋ฒ์จฐ ์ค๋ถํฐ ๊ทธ ์ด์ ๊ฐ(์์์ ์ค๋ ๊ฐ์)๊ณผ ๊ทธ ์ index์ ๊ฐ(์ผ์ชฝ์์ ์ค๋ ๊ฐ์)๋ฅผ ๋ํ๋ฉด์ ๋ฐ๋ณตํ๋ค
์ต์ข
์ ์ผ๋ก row์ ๊ฐ์ฅ ์ค๋ฅธ์ชฝ ๊ฐ return
rowํฌ๊ธฐ = M, col ํฌ๊ธฐ = N
TC : O(M * N)
๋ชจ๋ ์นธ์ ๋ํด ์ํ
SC : O(N)
ํ row๋งํผ ๋ฉ๋ชจ๋ฆฌ ํ ๋น
*/
#include <vector>
using namespace std;
class Solution {
public:
int uniquePaths(int m, int n) {
vector<int> row(n, 1);
for (int i = 1; i < m; i++) {
for (int j = 1; j < n; j++) {
row[j] = row[j] + row[j - 1];
}
}
return row[n - 1];
}
};