๋ฐ์ํ
๋์ ํ์ด
var minPathSum = function (grid) {
let mLen = grid.length;
let nLen = grid[0].length;
const DP = [...new Array(mLen)].map(() => []);
DP[0][0] = grid[0][0];
for (let i = 0; i < mLen; i++) {
for (let j = 0; j < nLen; j++) {
if (i === 0 && j === 0) DP[i][j] = grid[i][j];
// ์ฐ์ธก ๋ฐฉํฅ
else if (j === 0) DP[i][j] = grid[i][j] + DP[i - 1][j];
// ํ๋จ ๋ฐฉํฅ
else if (i === 0) DP[i][j] = grid[i][j] + DP[i][j - 1];
// ๋๊ฐ์ ๋ฐฉํฅ
else {
DP[i][j] = grid[i][j] + Math.min(DP[i][j - 1], DP[i - 1][j]);
}
}
}
return DP[mLen - 1][nLen - 1];
};
1. DP ๋ฐฐ์ด์ ์์ฑํ ํ
์ฐธ๊ณ ๋ก new Array(3).fill([]) ์ ํ๋ฉด DP[ [1] , [1] , [1] ] ์ด๋ ๊ฒ ๊ฐ ๋ฐฐ์ด์ ๊ฐ์ด ๋ณต์ฌ๋๋ฏ๋ก ์์ ๊ฐ์ด ์์ฑํด์ผ ํ๋ค.
2. DP์ ์๋ง์ ๊ฐ์ ๋ฃ์ด์ฃผ๋๋ฐ
์ฐ์ธก ๋ฐฉํฅ
ํ๋จ ๋ฐฉํฅ
๊ทธ์ธ ๋ฐฉํฅ
๊ฒฝ์ฐ์ ๋ฐ๋ผ ๋๋ ์ DP[i][j]์ ๊ฐ์ ์ฑ์๋ฃ๋๋ค.
ํด๋น ๋ฌธ์ ๋ ์ฝ๋์๋ ์๊ณ ๋ฆฌ์ฆ์ ๋ณด๊ณ ์ถ๊ฐ์ ์ผ๋ก ํ์ตํ์ต๋๋ค.
'๐ ์๊ณ ๋ฆฌ์ฆ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[LeetCode] 125_Palindrome (0) | 2021.12.07 |
---|---|
[LeetCode] 28_ImplementstrStr -JavaScript (0) | 2021.12.02 |
[LeetCode] 746 Min Cost Climbing Stairs-javascript (0) | 2021.11.20 |
[codility] JS Lv4-3 MaxCounter (0) | 2021.09.24 |
[codility] JS Lv4-1 ForgRiverOne (0) | 2021.09.18 |
๋๊ธ