Skip to content

Commit bac2624

Browse files
committed
Add solution #120
1 parent cc074b4 commit bac2624

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
116|[Populating Next Right Pointers in Each Node](./0116-populating-next-right-pointers-in-each-node.js)|Medium|
7171
118|[Pascal's Triangle](./0118-pascals-triangle.js)|Easy|
7272
119|[Pascal's Triangle II](./0119-pascals-triangle-ii.js)|Easy|
73+
120|[Triangle](./0120-triangle.js)|Medium|
7374
121|[Best Time to Buy and Sell Stock](./0121-best-time-to-buy-and-sell-stock.js)|Easy|
7475
128|[Longest Consecutive Sequence](./0128-longest-consecutive-sequence.js)|Medium|
7576
133|[Clone Graph](./0133-clone-graph.js)|Medium|

solutions/0120-triangle.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* 120. Triangle
3+
* https://leetcode.com/problems/triangle/
4+
* Difficulty: Medium
5+
*
6+
* Given a triangle array, return the minimum path sum from top to bottom.
7+
*
8+
* For each step, you may move to an adjacent number of the row below. More
9+
* formally, if you are on index i on the current row, you may move to either
10+
* index i or index i + 1 on the next row.
11+
*/
12+
13+
/**
14+
* @param {number[][]} triangle
15+
* @return {number}
16+
*/
17+
var minimumTotal = function(triangle) {
18+
for (let i = triangle.length - 2; i > -1; i--) {
19+
for (let j = 0; j < triangle[i].length; j++) {
20+
triangle[i][j] += Math.min( triangle[i + 1][j], triangle[i + 1][j + 1]);
21+
}
22+
}
23+
24+
return triangle[0][0];
25+
};

0 commit comments

Comments
 (0)