λ¬Έμ
νμ΄ κ³Όμ
첫λ²μ§Έ νμμ λ§μ§λ§ νκΉμ§ λλ¬νκΈ° μν 거리μ μ΅μ ν©μ ꡬνλ λ¬Έμ μ λλ€.
dp(row, idx) = row νμ idx μ΄μμ λ§μ§λ§ νμ λλ¬νκΈ° μν μ΅μ 거리
λΌκ³ μ μνλ€λ©΄
λ€μκ³Ό κ°μ μ νμμ μ»μ μ μμ΅λλ€.
dp[row][idx] = min(dp[row + 1][idx], dp[row + 1][idx + 1]) + triangle[row][idx]
μ½λ
/**
* @param {number[][]} triangle
* @return {number}
*/
var minimumTotal = function (triangle) {
const INF = 9999999;
const rowLimit = triangle.length;
const memo = Array.from(Array(rowLimit), () => Array(rowLimit + 1).fill(-1));
function dp(row, index) {
if (row.length >= index) {
return INF;
}
if (row === rowLimit - 1) {
return triangle[row][index];
}
if (memo[row][index] !== -1) {
return memo[row][index];
}
memo[row][index] =
Math.min(dp(row + 1, index), dp(row + 1, index + 1)) +
triangle[row][index];
return memo[row][index];
}
const answer = dp(0, 0);
return answer;
};
λ°μν
'π algorithm > leetcode' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
LeetCode 300 - Longest Increasing Subsequence (Medium) (0) | 2021.06.14 |
---|---|
LeetCode 36 - Valid Sudoku (Medium) (0) | 2021.04.13 |
LeetCode 38 - Count and Say (Medium) (0) | 2021.04.13 |
LeetCode 199 - Binary Tree Right Side View (Medium) (0) | 2021.04.05 |
LeetCode 344 - Reverse String (Easy) (0) | 2021.04.05 |
π¬ λκΈ