Day 28 — Minimum Falling Path Sum

class MinFallingPathSum{
public int minFallingPathSumMethod(int[][] A) {

if(A == null || A.length == 0 || A[0].length == 0) return 0;
int n = A.length;
for(int i = 1; i < n; i++) {
for(int j = 0; j < n; j++) {
int min = A[i-1][j];
if(j > 0) min = Math.min(min, A[i-1][j-1]);
if(j < n-1) min = Math.min(min, A[i-1][j+1]);
A[i][j] += min;
}
}
int result = Integer.MAX_VALUE;
for(int i : A[n-1]) {
result = Math.min(i, result);
}
return result;
}
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store