-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTriangleMinPath.java
34 lines (29 loc) · 1.23 KB
/
TriangleMinPath.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
package leetcode;
import java.util.List;
public class TriangleMinPath {
/**
* Tries to find the minimum path sum from top to bottom. However, this method starts
* from the bottom to achieve time & space optimization.
*
* @param triangle is the input triangle "matrix" of numbers.
* @return the minimum path sum from top to bottom
*/
public int minimumTotal(List<List<Integer>> triangle) {
int numOfRows = triangle.size();
// Keeps track of the sum so far for each element in the current row.
int[] sumSoFar = new int[numOfRows];
// The sum so far for each element in the last row is the same as the value itself.
List<Integer> currentRow = triangle.get(numOfRows - 1);
for (int i = 0; i < numOfRows; i++) {
sumSoFar[i] = currentRow.get(i);
}
// Updates the other rows according to the result from the row below.
for (int row = numOfRows - 2; row >= 0; row--) {
currentRow = triangle.get(row);
for (int column = 0; column <= row; column++) {
sumSoFar[column] = Math.min(sumSoFar[column], sumSoFar[column + 1]) + currentRow.get(column);
}
}
return sumSoFar[0];
}
}