File tree Expand file tree Collapse file tree 1 file changed +64
-0
lines changed Expand file tree Collapse file tree 1 file changed +64
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * m x n 행렬이 있을 때 로봇이 상단-좌측 가장자리에서 하단-우측 가장자리로 갈 수 있는 경우의 수 구하기
3
+ * 로봇은 오른쪽 또는 밑으로만 움직일 수 있음
4
+ */
5
+ class Solution {
6
+
7
+ // DFS 시간 초과로 DP로 구현
8
+ // 시간복잡도: O(m * n)
9
+ public int uniquePaths (int m , int n ) {
10
+ int [][] dp = new int [m ][n ];
11
+
12
+ // 첫째 열
13
+ for (int i = 0 ; i < m ; i ++) {
14
+ dp [i ][0 ] = 1 ;
15
+ }
16
+
17
+ // 첫째 행
18
+ for (int j = 0 ; j < n ; j ++) {
19
+ dp [0 ][j ] = 1 ;
20
+ }
21
+
22
+ for (int i = 1 ; i < m ; i ++) {
23
+ for (int j = 1 ; j < n ; j ++) {
24
+ dp [i ][j ] = dp [i - 1 ][j ] + dp [i ][j - 1 ]; // 위, 왼쪽 값
25
+ }
26
+ }
27
+
28
+ return dp [m - 1 ][n - 1 ];
29
+
30
+ }
31
+
32
+ // DFS 시간 초과
33
+ // private int cnt = 0;
34
+ // int[] dx = {1, 0};
35
+ // int[] dy = {0, 1};
36
+
37
+ // public int uniquePaths(int m, int n) {
38
+ // int[][] path = new int[m][n];
39
+ // dfs(0, 0, path);
40
+ // return cnt;
41
+ // }
42
+
43
+ // private void dfs(int x, int y, int[][] path) {
44
+
45
+ // if (x == path.length - 1 && y == path[0].length - 1) {
46
+ // cnt++;
47
+ // return;
48
+ // }
49
+
50
+ // path[x][y] = 1;
51
+
52
+ // for (int i = 0; i < 2; i++) {
53
+ // int nx = x + dx[i];
54
+ // int ny = y + dy[i];
55
+ // if (nx >= 0 && nx < path.length && ny >= 0 && ny < path[0].length && path[nx][ny] != 1) {
56
+ // dfs(nx, ny, path);
57
+ // }
58
+ // }
59
+
60
+ // path[x][y] = 0;
61
+
62
+ // }
63
+ }
64
+
You can’t perform that action at this time.
0 commit comments