Skip to content

Commit a2088b5

Browse files
Triangle
1 parent d36d06a commit a2088b5

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

MEDIUM/src/medium/Triangle.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package medium;
2+
import java.util.*;
3+
public class Triangle {
4+
5+
public static int minimumTotal(List<List<Integer>> triangle) {
6+
/**Looked at this post: https://discuss.leetcode.com/topic/1669/dp-solution-for-triangle, @stellari has a very excellent explanation.
7+
* Basically, we use the bottom-up approach, starting from the bottom row of this triangle, and we only need to store the shortest path of each node
8+
* from its last row, and keep overwriting it until we reach the top.*/
9+
int n = triangle.size();
10+
List<Integer> cache = triangle.get(n-1);
11+
12+
for (int layer = n-2; layer >= 0; layer--){//for each layer
13+
for (int i = 0; i <= layer; i++){//check its very node
14+
int value = Math.min(cache.get(i), cache.get(i+1)) + triangle.get(layer).get(i);
15+
cache.set(i, value);
16+
}
17+
}
18+
return cache.get(0);
19+
}
20+
21+
public static void main(String...strings){
22+
List<List<Integer>> triangle = new ArrayList();
23+
triangle.add(Arrays.asList(1));
24+
triangle.add(Arrays.asList(2,3));
25+
System.out.println(minimumTotal(triangle));
26+
}
27+
28+
}

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@
9999
|125|[Valid Palindrome](https://leetcode.com/problems/valid-palindrome/)|[Solution](../../blob/master/EASY/src/easy/ValidPalindrome.java)| O(n)|O(1) | Easy| Two Pointers
100100
|122|[Best Time to Buy and Sell Stock II](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/)|[Solution](../../blob/master/MEDIUM/src/medium/BestTimeToBuyAndSellStockII.java)| O(n)|O(1) | Medium | Greedy
101101
|121|[Best Time to Buy and Sell Stock](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/)|[Solution](../../blob/master/EASY/src/easy/BestTimeToBuyAndSellStock.java)| O(n)|O(1) | Easy| DP
102+
|120|[Triangle](https://leetcode.com/problems/triangle/)|[Solution](../../blob/master/MEDIUM/src/medium/Triangle.java)| O(m*n)|O(n) | Medium| DP
102103
|119|[Pascal's Triangle II](https://leetcode.com/problems/pascals-triangle-ii/)|[Solution](../../blob/master/EASY/src/easy/PascalsTriangleII.java)| O(n^2)|O(1) | Easy|
103104
|118|[Pascal's Triangle](https://leetcode.com/problems/pascals-triangle/)|[Solution](../../blob/master/EASY/src/easy/PascalsTriangle.java)| O(n^2)|O(1) | Easy|
104105
|117|[Populating Next Right Pointers in Each Node II](https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/)|[Solution](../../blob/master/HARD/src/hard/PopulatingNextRightPointersinEachNodeII.java)| O(n)|O(1) | Hard| BFS

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy