Skip to content

Commit 2c80b43

Browse files
merge for loops
1 parent 9303509 commit 2c80b43

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

MEDIUM/src/medium/UniquePaths.java

+12
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,18 @@ public int uniquePaths(int m, int n) {
3131
return dp[m-1][n-1];
3232
}
3333

34+
//and we can actually put the two initialization for loop into the one
35+
public int uniquePaths_merged_for_loop(int m, int n) {
36+
int[][] dp = new int[m][n];
37+
for(int i = 0; i < m; i++){
38+
for(int j = 0; j < n; j++){
39+
if(i == 0 || j == 0) dp[i][j] = 0;
40+
else dp[i][j] = dp[i-1][j] + dp[i][j-1];
41+
}
42+
}
43+
return dp[m-1][n-1];
44+
}
45+
3446
public static void main(String...strings){
3547
UniquePaths test = new UniquePaths();
3648
int m = 1;

0 commit comments

Comments
 (0)