File tree 2 files changed +39
-0
lines changed
2 files changed +39
-0
lines changed Original file line number Diff line number Diff line change @@ -32,6 +32,7 @@ This is the solutions collection of my LeetCode submissions, most of them are pr
32
32
| 73| [ Set Matrix Zeroes] ( https://leetcode.com/problems/set-matrix-zeroes/ ) | [ JavaScript] ( ./src/set-matrix-zeroes/res.js ) | Medium|
33
33
| 75| [ Sort Colors] ( https://leetcode.com/problems/sort-colors/ ) | [ JavaScript] ( ./src/sort-colors/res.js ) | Medium|
34
34
| 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|
35
36
| 130| [ Surrounded Regions] ( https://leetcode.com/problems/surrounded-regions/ ) | [ JavaScript] ( ./src/surrounded-regions/res.js ) | Medium|
36
37
| 136| [ Single Number] ( https://leetcode.com/problems/single-number/ ) | [ JavaScript] ( ./src/single-number/res.js ) | Easy|
37
38
| 175| [ Combine Two Tables] ( https://leetcode.com/problems/combine-two-tables/ ) | [ SQL] ( ./src/combine-two-tables/res.txt ) | Easy|
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments