Skip to content

Commit 69ed2d5

Browse files
committed
unique paths
1 parent 9ef808d commit 69ed2d5

1 file changed

Lines changed: 48 additions & 0 deletions

File tree

unique-paths/se6816.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
class Solution {
2+
public int[] moveX = {0, 1};
3+
public int[] moveY = {1, 0};
4+
int N;
5+
int M;
6+
public int uniquePaths(int m, int n) {
7+
int[][] dp = new int[m][n];
8+
boolean[][] visited = new boolean[m][n];
9+
int curX = 0;
10+
int curY = 0;
11+
N = n;
12+
M = m;
13+
dp[curX][curY] = 1;
14+
visited[curX][curY] = true;
15+
Queue<int[]> que = new ArrayDeque<>();
16+
que.offer(new int[]{0,0});
17+
18+
while(!que.isEmpty()) {
19+
int[] data = que.poll();
20+
21+
for(int i = 0; i < 2; i++) {
22+
int tempX = data[0] + moveX[i];
23+
int tempY = data[1] + moveY[i];
24+
25+
if(isOutOfIndex(tempX, tempY)) {
26+
continue;
27+
}
28+
29+
dp[tempX][tempY] += dp[data[0]][data[1]];
30+
31+
32+
if(visited[tempX][tempY]) {
33+
continue;
34+
}
35+
visited[tempX][tempY] = true;
36+
que.add(new int[]{tempX, tempY});
37+
38+
}
39+
}
40+
41+
return dp[M-1][N-1];
42+
43+
}
44+
45+
public boolean isOutOfIndex(int x, int y) {
46+
return x < 0 || x >= M || y < 0 || y >=N;
47+
}
48+
}

0 commit comments

Comments
 (0)