Skip to content

Commit 7b45fa5

Browse files
committed
feat : leetCode DP uniquePaths
1 parent d4d3975 commit 7b45fa5

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

src/main/java/sgyj/leetcode/Readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,4 @@
3333
- [746. Min Cost Climbing Stairs](https://leetcode.com/problems/min-cost-climbing-stairs/description/)
3434
- [128. Longest Consecutive Sequence](https://leetcode.com/problems/longest-consecutive-sequence/)
3535
- [322. Coin Change](https://leetcode.com/problems/coin-change/description/)
36+
- [62. Unique Paths](https://leetcode.com/problems/unique-paths/description/)
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package sgyj.leetcode.yeji.section6;
2+
3+
// 62. Unique Paths
4+
/*
5+
There is a robot on an m x n grid. The robot is initially located at the top-left corner (i.e., grid[0][0]).
6+
The robot tries to move to the bottom-right corner (i.e., grid[m - 1][n - 1]). The robot can only move either down or right at any point in time.
7+
Given the two integers m and n, return the number of possible unique paths that the robot can take to reach the bottom-right corner.
8+
The test cases are generated so that the answer will be less than or equal to 2 * 109.
9+
*/
10+
11+
public class Solution62 {
12+
private static int[][] visited;
13+
private static int answer = 0;
14+
public static int uniquePaths(int m, int n) {
15+
16+
visited = new int[m][n];
17+
visited[0][0] = 1;
18+
19+
for(int i=0; i<m; i++){
20+
for(int j=0; j<n; j++){
21+
int dx = 0;
22+
int dy = 0;
23+
if(i-1>=0){
24+
dx = visited[i-1][j];
25+
}
26+
if(j-1>=0){
27+
dy = visited[i][j-1];
28+
}
29+
visited[i][j] = dx+dy == 0 ? 1 : dx+dy;
30+
}
31+
}
32+
33+
return visited[m-1][n-1];
34+
}
35+
36+
public static void main ( String[] args ) {
37+
int m = 3;
38+
int n = 2;
39+
40+
System.out.println(uniquePaths(m,n));
41+
42+
}
43+
}

0 commit comments

Comments
 (0)