-
Notifications
You must be signed in to change notification settings - Fork 0
/
leetcode120.js
33 lines (28 loc) · 887 Bytes
/
leetcode120.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
/**
* @param {number[][]} triangle
* @return {number}
* https://leetcode.com/problems/triangle/
* https://leetcode.com/submissions/detail/550131657/
*/
var minimumTotal = function (triangle) {
if (triangle.length === 1) return triangle[0][0];
let last = triangle[0]; //map for last tier;
let current; //map for current tier;
for (let i = 1; i < triangle.length; i++) {
current = triangle[i];
//loop thru current tier and increment value at current[j] by smaller of last[j - 1] or last[j]
//provided both value exist
for (let j = 0; j < current.length; j++) {
if (j === 0) {
current[j] += last[j];
} else {
current[j] += Math.min(
last[j] === undefined ? last[j - 1] : last[j], //gotta check if last[j] exist!!
last[j - 1]
);
}
}
last = current;
}
return Math.min(...current);
};