forked from chihungyu1116/leetcode-javascript
-
Notifications
You must be signed in to change notification settings - Fork 14
/
120 Triangle.js
38 lines (33 loc) · 872 Bytes
/
120 Triangle.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// Leetcode 120
// Language: Javascript
// Problem: https://leetcode.com/problems/triangle/
// Author: Chihung Yu
/**
* @param {number[][]} triangle
* @return {number}
*/
var minimumTotal = function(triangle) {
var curLvlIndex = 0;
var cur = triangle[curLvlIndex];
var next;
while(curLvlIndex < (triangle.length-1)){
next = triangle[curLvlIndex + 1];
for(var i = 0; i < next.length; i++){
var a,b;
if(i === 0){
b = Infinity;
} else {
b = cur[i-1];
}
if(i < cur.length){
a = cur[i];
} else {
a = Infinity;
}
next[i] += Math.min(a,b);
}
cur = next;
curLvlIndex++;
}
return Math.min.apply(null, cur);
};