Skip to content

Commit 2e1396b

Browse files
committedApr 6, 2017
add: Triangle
1 parent 8cae1cb commit 2e1396b

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed
 

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ This is the solutions collection of my LeetCode submissions, most of them are pr
3232
|73|[Set Matrix Zeroes](https://leetcode.com/problems/set-matrix-zeroes/) | [JavaScript](./src/set-matrix-zeroes/res.js)|Medium|
3333
|75|[Sort Colors](https://leetcode.com/problems/sort-colors/) | [JavaScript](./src/sort-colors/res.js)|Medium|
3434
|91|[Decode Ways](https://leetcode.com/problems/decode-ways/) | [JavaScript](./src/decode-ways/res.js)|Medium|
35+
|120|[Triangle](https://leetcode.com/problems/triangle/) | [JavaScript](./src/triangle/res.js)|Medium|
3536
|130|[Surrounded Regions](https://leetcode.com/problems/surrounded-regions/) | [JavaScript](./src/surrounded-regions/res.js)|Medium|
3637
|136|[Single Number](https://leetcode.com/problems/single-number/) | [JavaScript](./src/single-number/res.js)|Easy|
3738
|175|[Combine Two Tables](https://leetcode.com/problems/combine-two-tables/)| [SQL](./src/combine-two-tables/res.txt)|Easy|

‎src/triangle/res.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* res.js
3+
* @authors Joe Jiang (hijiangtao@gmail.com)
4+
* @date 2017-04-06 21:23:50
5+
*
6+
* Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.
7+
*
8+
* For example, given the following triangle
9+
* [
10+
* [2],
11+
* [3,4],
12+
* [6,5,7],
13+
* [4,1,8,3]
14+
* ]
15+
*
16+
* The minimum path sum from top to bottom is 11 (i.e., 2 + 3 + 5 + 1 = 11).
17+
*
18+
* @param {number[][]} triangle
19+
* @return {number}
20+
*/
21+
let minimumTotal = function(triangle) {
22+
let row = triangle.length;
23+
24+
if (!row) return null;
25+
26+
for (let i=row-2; i>=0; i--) {
27+
for (let j=0; j<=i; j++) {
28+
triangle[i][j] += min(triangle[i+1][j], triangle[i+1][j+1]);
29+
}
30+
}
31+
32+
return triangle[0][0];
33+
};
34+
35+
let min = function(a, b) {
36+
if (a>b) return b;
37+
else return a;
38+
};

0 commit comments

Comments
 (0)
Please sign in to comment.