File tree Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Original file line number Diff line number Diff line change 1+ class Solution {
2+ public int minimumTotal(List<List<Integer>> triangle) {
3+ for(int i = 1; i < triangle.size(); i++) {
4+ for(int j = 0; j < triangle.get(i).size(); j++){
5+ int sum = 0;
6+ if(j == 0) {
7+ sum = triangle.get(i).get(j) + triangle.get(i-1).get(j);
8+ }
9+ else if(j == triangle.get(i).size()-1) {
10+ sum = triangle.get(i).get(j) + triangle.get(i-1).get(triangle.get(i-1).size()-1);
11+ }
12+ else {
13+ int min = Math.min(triangle.get(i-1).get(j), triangle.get(i-1).get(j-1));
14+ sum = min+ triangle.get(i).get(j);
15+ }
16+
17+ triangle.get(i).set(j, sum);
18+ }
19+ }
20+ return Collections.min(triangle.get(triangle.size()-1));
21+ }
22+ }
23+
24+ class Solution {
25+ public int minimumTotal(List<List<Integer>> triangle) {
26+ for(int i = triangle.size()-2; i >= 0; i--) {
27+ for(int j = 0; j < triangle.get(i).size(); j++) {
28+ int min = Math.min(triangle.get(i+1).get(j), triangle.get(i+1).get(j+1));
29+ int sum = min + triangle.get(i).get(j);
30+ triangle.get(i).set(j, sum);
31+ }
32+ }
33+ return triangle.get(0).get(0);
34+ }
35+ }
You can’t perform that action at this time.
0 commit comments