728x90
λ°μν
π λ¬Έμ
5 * 5 격μνμ μλμ κ°μ΄ μ«μκ° μ νμλ€.
N * Nμ 격μνμ΄ μ£Όμ΄μ§λ©΄ κ° νμ ν©, κ° μ΄μ ν©, λ λκ°μ μ ν© μ€ κ°μ₯ ν° ν©μ μΆλ ₯νλ λ¬Έμ μ΄λ€.
1οΈβ£ μ΄μ€ forλ¬Έμ μ¬μ©νμ¬ κ° νμ ν©, κ° μ΄μ ν©μ ꡬνκ³ , answer λ³μμ κ°κ³Ό λΉκ΅νμ¬ κ°μ₯ ν° κ°μ answer λ³μμ μ¬ν λΉνλ€.
2οΈβ£ λκ°μ μ μ’μΈ‘ μλ¨μμ μ°μΈ‘ νλ¨ λ°©ν₯μ λκ°μ arr[i][i]κ³Ό μ°μΈ‘ μλ¨μμ μ’μΈ‘ νλ¨ λ°©ν₯μ λκ°μ arr[i][n - (i+1)]μ ꡬνκ³ , μ΅μ’ μ μΌλ‘ answer λ³μμ κ°κ³Ό λΉκ΅νμ¬ κ°μ₯ ν° ν©μ μ°Ύλλ€.
π νμ΄
function solution(arr) {
let answer = Number.MIN_SAFE_INTEGER;
const n = arr.length;
let sum1 = (sum2 = 0);
for (let i = 0; i < n; i++) {
sum1 = sum2 = 0;
for (let j = 0; j < n; j++) {
sum1 += arr[i][j]; // κ° νμ ν©
sum2 += arr[j][i]; // κ° μ΄μ ν©
}
answer = Math.max(answer, sum1, sum2);
}
sum1 = sum2 = 0;
for (let i = 0; i < n; i++) {
sum1 += arr[i][i];
sum2 += arr[i][n - (i + 1)];
}
answer = Math.max(answer, sum1, sum2);
return answer;
}
let arr = [
[10, 13, 10, 12, 15],
[12, 39, 30, 23, 11],
[11, 25, 50, 53, 15],
[19, 27, 29, 37, 27],
[19, 13, 30, 13, 19],
];
console.log(solution(arr));
'Algorithm > μΈνλ°(inflearn)' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[JavaScript/section 3] 01 - νλ¬Έ λ¬Έμμ΄ (0) | 2022.09.08 |
---|---|
[JavaScript/section 2] 07 - λ΄μ°λ¦¬ (0) | 2022.09.07 |
[JavaScript/section 2] 05 - λ±μ ꡬνκΈ° (0) | 2022.09.07 |
[JavaScript/section 2] 04 - μ μ κ³μ° (0) | 2022.09.06 |
[JavaScript/section 2] 03 - κ°μ λ°μ 보 (0) | 2022.09.06 |