File tree Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Original file line number Diff line number Diff line change
1
+ /* *
2
+ * ํ์ด
3
+ * - ์กฐํฉ ๊ณต์์ ์ฌ์ฉํ๋ฉด overflow ๋ฐ ์๊ฐ์ด๊ณผ๋ฅผ ์ผ์ผํฌ ์ ์์ต๋๋ค
4
+ * - ๋ชจ๋ ์ขํ์ ๋ํด uniquePaths๋ฅผ ๊ณ์ฐํ๋ ๋ฐฉ์์ ์ฌ์ฉํฉ๋๋ค
5
+ * - ํน์ ์ขํ์ uniquePaths๋ฅผ ๊ณ์ฐํ๊ธฐ ์ํด์๋ ๋ ํ๋ง ํ์ํ๊ธฐ ๋๋ฌธ์ ๊ธธ์ด m์ ๋ฐฐ์ด ๋ ๊ฐ๋ฅผ ์ด์ฉํฉ๋๋ค
6
+ *
7
+ * Big O
8
+ * - Time complexity: O(MN)
9
+ * - Space compexity: O(N)
10
+ */
11
+
12
+ class Solution {
13
+ public:
14
+ int uniquePaths (int m, int n) {
15
+ vector<int > row1;
16
+ vector<int > row2;
17
+
18
+ for (int i = 0 ; i < n; ++i) row1.push_back (1 );
19
+ row2.push_back (1 );
20
+ for (int i = 1 ; i < n; ++i) row2.push_back (0 );
21
+
22
+ for (int j = 1 ; j < m; ++j) {
23
+ for (int i = 1 ; i < n; ++i) row2[i] = row1[i] + row2[i - 1 ];
24
+ swap (row1, row2);
25
+ row2[0 ] = 1 ;
26
+ for (int i = 1 ; i < n; ++i) row2[i] = 0 ;
27
+ }
28
+
29
+ return row1[n - 1 ];
30
+ }
31
+ };
You canโt perform that action at this time.
0 commit comments