-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMinimumPathSum.java
56 lines (50 loc) · 1.53 KB
/
MinimumPathSum.java
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
package techQuestions;
import java.util.Arrays;
public class MinimumPathSum {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[][] grid = {{1,3,1},{1,5,1},{4,2,1}};
System.out.println(minPathSum(grid));
}
public static int minPathSum(int[][] grid) {
// brute force method is to traverse every path possible
// best way to do this is probably recursion
// if (grid.length == 0) {
// return 0;
// }
// return pathRecurse(grid, 0, 0);
// let's attempt dynamic programming method
// we start at the grid at the bottom right
for (int i = grid.length - 1; i >= 0; i--) {
for (int j = grid[0].length - 1; j >= 0; j--) {
if (i == grid.length - 1) {
if (j == 0) {
continue;
}
grid[i][j - 1] += grid[i][j];
} else if (j == grid[0].length - 1 && i != grid.length - 1) {
grid[i][j] += grid[i+1][j];
} else {
grid[i][j] += Math.min(grid[i+1][j],grid[i][j+1]);
}
}
}
return grid[0][0];
}
public static int pathRecurse(int[][] grid, int i, int j) {
int thisValue = grid[i][j];
// base case is when we reach bottom of grid
if (i == grid.length - 1 && j == grid[0].length - 1) {
return thisValue;
}
if (i < grid.length - 1 && j < grid[0].length - 1) {
return thisValue + Math.min(pathRecurse(grid, i + 1, j),
pathRecurse(grid, i, j + 1));
} else if (i == grid.length - 1) {
return thisValue + pathRecurse(grid, i, j+1);
} else if (j == grid[0].length - 1) {
return thisValue + pathRecurse(grid, i+1, j);
}
return 0;
}
}