forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGeegong.java
More file actions
52 lines (46 loc) ยท 1.96 KB
/
Geegong.java
File metadata and controls
52 lines (46 loc) ยท 1.96 KB
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
public class Geegong {
/**
* 1. vectors ๋ฅผ ์ ํ๊ณ ์ด์งํธ๋ฆฌ๋ฅผ dfs ์ฒ๋ผ ๋ฐฉํฅ์ ์ฃผ์ด๊ฐ๋ฉฐ ํ์ด๊ฐ๋ค.
* ๊ทธ๋ฐ๋ฐ time limit exceeded ใ
ใ
* => ๋ ๋ฐฉํฅ์ผ๋ก ๊ณ์ ๋ป์ด๊ฐ๊ธฐ ๋๋ฌธ์ time complexity ๊ฐ (m+n)^2 ๊น์ง ๋๋ค ํก
*
* 2. dp ํ์ด ๋ฐฉ๋ฒ, 1์ฐจ์ ๋ฐฐ์ด๋ก ์๊ฐํ์ ๋ dp๋ฐฐ์ด์ ์ธ๋ฑ์ค๋ ์ธ๋ฑ์ค๋งํผ์ row, column ๊น์ง ๋๋ฌํ๋ ๋ฐฉ๋ฒ์ ์๋ฅผ ๊ณ์ํด์
* ๋์ ํด๊ฐ๋ ๋ฐฉ๋ฒ์ผ๋ก ์งํ
*
* @param m
* @param n
* @return
*/
public static int[][] vectors = {{0,1}, {1,0}};
public int uniquePaths(int m, int n) {
// case 1. tle ใ
ใ
// return dfs( 0, 0, m, n);
// ๋ญ๊ฐ m*n ๋งํผ์ ๋ฐฐ์ด์ด์ด์ผ ํ ๊ฒ ๊ฐ์ง๋ง ์ฌ์ค ๊ฐ row๋ง๋ค์ column๋ค์ ๋ฐฉ๋ฌธํ ๋์ ๋ฐฉ๋ฌธ ๊ฐ๋ฅ ๊ฐฏ์๋ฅผ ๋์ ํด๊ฐ๊ธฐ๋๋ฌธ์ n ๋งํผ๋ง ์์ด๋ ๋๋ค.
int[] dp = new int[n];
dp[0] = 1; // ์์์ ๋ถํฐ path ๊ฐ๋ฅ ๊ฐฏ์ 1
for(int rowIdx = 0; rowIdx < m; rowIdx++) {
// 1๋ถํฐ ์ธ๋ ์ด์ ๋... ์ด์ฐจํผ ์ค๋ฅธ์ชฝ์ผ๋ก ์์ง์ด๊ธฐ ๋๋ฌธ์?
for(int colIdx = 1; colIdx < n; colIdx++) {
// dp[colIdx - 1] : ์ง์ column๊ธฐ์ค 0์์๋ถํฐ ๋๋ฌํ ์ ์๋ ๋ฐฉ๋ฒ์ ์
// dp[colIdx] : rowIdx - 1์ด ์์๋ 0์์๋ถํฐ [rowIdx - 1][colIdx] ๊น์ง ๋๋ฌํ ์ ์๋ ๋ฐฉ๋ฒ์ ์
dp[colIdx] = dp[colIdx - 1] + dp[colIdx];
}
}
return dp[n - 1];
}
// public int dfs(int startRow, int startBottom, int m, int n) {
// if (startRow == m - 1 && startBottom == n - 1) {
// return 1;
// } else if (startRow >= m || startBottom >= n) {
// return 0;
// }
//
// int result = 0;
//
// for (int[] vector : vectors) {
// result += dfs(vector[1] + startRow, vector[0] + startBottom, m, n);
// }
//
// return result;
// }
}