JavaScript - CodeKata #13 π

devmoonshΒ·2020λ 9μ 28μΌ
0

38/48

1. Question

μμλ‘ μ΄λ£¨μ΄μ§ m x n κ·Έλ¦¬λλ₯Ό μΈμλ‘ λλ¦½λλ€.
μλ¨ μΌμͺ½μμ μμνμ¬, νλ¨ μ€λ₯Έμͺ½κΉμ§ κ°λ κΈΈμ μμλ₯Ό λ€ λνμ λ,
κ°μ₯ μμ ν©μ μ°Ύμμ return ν΄μ£ΌμΈμ.

ν μ§μ μμ μ°μΈ‘μ΄λ μλλ‘λ§ μ΄λν  μ μμ΅λλ€.

Input:
[
[1,3,1],
[1,5,1],
[4,2,1]
]

Output: 7

μ€λͺ: 1β3β1β1β1 μ ν©μ΄ μ μΌ μμ

const minPathSum = grid => {
// μΌλ¨ μ²« rowμ μ²« columnμ κ°κ°, κ·Έ pathλ‘ κ°λ€κ³  μκ°νκ³  λν΄λλλ€.
for (let i = 1; i < grid.length; i++) {
grid[i][0] += grid[i-1][0];
}

for (let i = 1; i < grid[0].length; i++) {
grid[0][i] += grid[0][i-1];
}

for (let i = 1; i < grid.length; i++) {
for (let j = 1; j < grid[0].length; j++) {

//μμͺ½μ΄λ μΌμͺ½μμ, λ μμκ±Έλ‘ λν΄μ€λ€.
grid[i][j] += Math.min(grid[i-1][j], grid[i][j-1]);
}
}

return grid[grid.length-1][grid[0].length-1];
};

console.log(minPathSum([[1,2,3],[4,1,2]])); // 6
Junior front-end developer