-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution931.java
More file actions
33 lines (28 loc) · 959 Bytes
/
Copy pathSolution931.java
File metadata and controls
33 lines (28 loc) · 959 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
29
30
31
32
public class Solution931 {
public int minFallingPathSum(int[][] matrix) {
int n = matrix.length;
for (int y = 1; y < n; y++) {
for (int x = 0; x < n; x++) {
int minPath = matrix[y - 1][x];
if (x > 0) minPath = Math.min(minPath, matrix[y-1][x-1]);
if (x < n - 1) minPath = Math.min(minPath, matrix[y-1][x+1]);
matrix[y][x] += minPath;
}
}
int min = Integer.MAX_VALUE;
for (int el : matrix[n-1]) {
if (el < min) min = el;
}
return min;
}
public static void main(String[] args) {
int[][] matrix = {
{51, 24},
{-50, 82},
};
long start = System.currentTimeMillis();
System.out.println(new Solution931().minFallingPathSum(matrix));
long end = System.currentTimeMillis();
System.out.println(end - start);
}
}