Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita2088b5

Browse files
Triangle
1 parentd36d06a commita2088b5

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+
packagemedium;
2+
importjava.util.*;
3+
publicclassTriangle {
4+
5+
publicstaticintminimumTotal(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+
intn =triangle.size();
10+
List<Integer>cache =triangle.get(n-1);
11+
12+
for (intlayer =n-2;layer >=0;layer--){//for each layer
13+
for (inti =0;i <=layer;i++){//check its very node
14+
intvalue =Math.min(cache.get(i),cache.get(i+1)) +triangle.get(layer).get(i);
15+
cache.set(i,value);
16+
}
17+
}
18+
returncache.get(0);
19+
}
20+
21+
publicstaticvoidmain(String...strings){
22+
List<List<Integer>>triangle =newArrayList();
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)

[8]ページ先頭

©2009-2025 Movatter.jp