-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinimum_Path_Sum.java
More file actions
28 lines (22 loc) · 868 Bytes
/
Minimum_Path_Sum.java
File metadata and controls
28 lines (22 loc) · 868 Bytes
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
64. Minimum Path Sum
Given a m x n grid filled with non-negative numbers, find a path from
top left to bottom right which minimizes the sum of all numbers along its path.
Note: You can only move either down or right at any point in time.
public class Solution {
public int minPathSum(int[][] grid) {
int[][] matrix = new int[grid.length][grid[0].length];
for(int i=0; i<grid.length; i++) {
for(int j=0; j<grid[0].length; j++) {
if(i==0 && j==0)
matrix[i][j] = grid[i][j];
else if(i==0)
matrix[i][j] = matrix[i][j-1] + grid[i][j];
else if(j==0)
matrix[i][j] = matrix[i-1][j] + grid[i][j];
else
matrix[i][j] = Math.min(matrix[i][j-1], matrix[i-1][j]) + grid[i][j];
}
}
return matrix[grid.length-1][grid[0].length-1];
}
}