-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTriangle.java
More file actions
38 lines (30 loc) · 1.32 KB
/
Copy pathTriangle.java
File metadata and controls
38 lines (30 loc) · 1.32 KB
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
35
36
37
38
// Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.
// See: https://leetcode.com/problems/triangle/
package leetcode.dynamic_programming;
import java.util.List;
public class Triangle {
/**
* Solution 1 - O(n^2) time, O(1) space
*/
public int minimumTotal(List<List<Integer>> triangle) {
int min = Integer.MAX_VALUE;
for (int i = 1; i < triangle.size(); i++) {
for (int j = 0; j < triangle.get(i).size(); j++) {
if (j == 0) {
triangle.get(i).set(j, triangle.get(i).get(j) + triangle.get(i - 1).get(0));
} else if (j == triangle.get(i).size() - 1) {
triangle.get(i).set(j, triangle.get(i).get(j)
+ triangle.get(i - 1).get(triangle.get(i).size() - 2));
} else {
int curr = Math.min(triangle.get(i - 1).get(j - 1), triangle.get(i - 1).get(j))
+ triangle.get(i).get(j);
triangle.get(i).set(j, curr);
}
}
}
int lastRow = triangle.size() - 1;
for (int i = 0; i < triangle.get(lastRow).size(); i++)
min = Math.min(min, triangle.get(lastRow).get(i));
return min;
}
}