Skip to content

Instantly share code, notes, and snippets.

@bilbo3000
Created June 8, 2018 02:27
Show Gist options
  • Save bilbo3000/03d67cbdcdfbeead2ac62faf71d3d2c8 to your computer and use it in GitHub Desktop.
Save bilbo3000/03d67cbdcdfbeead2ac62faf71d3d2c8 to your computer and use it in GitHub Desktop.
class Solution {
public int minimumTotal(List<List<Integer>> triangle) {
if (triangle == null || triangle.size() == 0) return 0;
int n = triangle.size();
int[] arr = new int[n];
arr[0] = triangle.get(0).get(0);
for (int i = 1; i < n; i++) {
List<Integer> row = triangle.get(i);
for (int j = row.size() - 1; j >= 0; j--) {
if (j == row.size() - 1) {
arr[j] = row.get(j) + arr[j - 1];
} else if (j == 0) {
arr[j] = arr[0] + row.get(j);
} else {
arr[j] = Math.min(arr[j - 1], arr[j]) + row.get(j);
}
}
}
int min = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] < min) {
min = arr[i];
}
}
return min;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment