forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTessa1217.java
More file actions
64 lines (49 loc) ยท 1.58 KB
/
Tessa1217.java
File metadata and controls
64 lines (49 loc) ยท 1.58 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
52
53
54
55
56
57
58
59
60
61
62
63
/**
* m x n ํ๋ ฌ์ด ์์ ๋ ๋ก๋ด์ด ์๋จ-์ข์ธก ๊ฐ์ฅ์๋ฆฌ์์ ํ๋จ-์ฐ์ธก ๊ฐ์ฅ์๋ฆฌ๋ก ๊ฐ ์ ์๋ ๊ฒฝ์ฐ์ ์ ๊ตฌํ๊ธฐ
* ๋ก๋ด์ ์ค๋ฅธ์ชฝ ๋๋ ๋ฐ์ผ๋ก๋ง ์์ง์ผ ์ ์์
*/
class Solution {
// DFS ์๊ฐ ์ด๊ณผ๋ก DP๋ก ๊ตฌํ
// ์๊ฐ๋ณต์ก๋: O(m * n)
public int uniquePaths(int m, int n) {
int[][] dp = new int[m][n];
// ์ฒซ์งธ ์ด
for (int i = 0; i < m; i++) {
dp[i][0] = 1;
}
// ์ฒซ์งธ ํ
for (int j = 0; j < n; j++) {
dp[0][j] = 1;
}
for (int i = 1; i < m; i++) {
for (int j = 1; j < n; j++) {
dp[i][j] = dp[i - 1][j] + dp[i][j - 1]; // ์, ์ผ์ชฝ ๊ฐ
}
}
return dp[m - 1][n - 1];
}
// DFS ์๊ฐ ์ด๊ณผ
// private int cnt = 0;
// int[] dx = {1, 0};
// int[] dy = {0, 1};
// public int uniquePaths(int m, int n) {
// int[][] path = new int[m][n];
// dfs(0, 0, path);
// return cnt;
// }
// private void dfs(int x, int y, int[][] path) {
// if (x == path.length - 1 && y == path[0].length - 1) {
// cnt++;
// return;
// }
// path[x][y] = 1;
// for (int i = 0; i < 2; i++) {
// int nx = x + dx[i];
// int ny = y + dy[i];
// if (nx >= 0 && nx < path.length && ny >= 0 && ny < path[0].length && path[nx][ny] != 1) {
// dfs(nx, ny, path);
// }
// }
// path[x][y] = 0;
// }
}