Leet Code 알고리즘
(Leet Code JS)Triangle
naeunchan
2022. 3. 1. 09:58
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
반응형