250x250
반응형
Notice
Recent Posts
Recent Comments
Link
N
(Leet Code JS)Triangle 본문
728x90
반응형
https://leetcode.com/problems/triangle/
Triangle - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
DP 알고리즘.
삼각형으로 이뤄진 배열에서 최소합을 구하면 된다.
최소합을 구하는 조건은 다음 행의 인접한 두 수 중 더 작은 값을 현재 행 값과 더하면 된다.
const minimumTotal = (triangle) => {
const length = triangle.length;
for(let i = length - 2; i >= 0; i--){
for(let j = 0; j < triangle[i].length; j++){
triangle[i][j] += Math.min(triangle[i + 1][j], triangle[i + 1][j + 1]);
}
}
return triangle[0][0];
};
728x90
반응형
'Leet Code 알고리즘' 카테고리의 다른 글
(Leet Code JS)Shuffle An Array (0) | 2022.03.02 |
---|---|
(Leet Code JS) Populating Next Right Pointers in Each Node (0) | 2022.03.02 |
(Leet Code JS)DI String Match (0) | 2022.02.28 |
(Leet Code JS)Lemonade Change (0) | 2022.02.28 |
(Leet Code JS)Can Place Flowers (0) | 2022.02.25 |