๋ฌธ์ œ

N*N ๊ฒฉ์žํŒ์ด ์ฃผ์–ด์ง€๋ฉด ๊ฐ ํ–‰์˜ ํ•ฉ, ๊ฐ ์—ด์˜ ํ•ฉ, ๋‘ ๋Œ€๊ฐ์„ ์˜ ํ•ฉ ์ค‘ ๊ฐ€์žฅ ํฐ ํ•ฉ์„ ์ถœ๋ ฅํ•˜์„ธ์š”.

 

์ž…๋ ฅ์˜ˆ์ œ |

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

์ถœ๋ ฅ์˜ˆ์ œ | 

155

 

 

 

ํ’€์ด

1. ๊ฐ ํ–‰์˜ ํ•ฉ, ๊ฐ ์—ด์˜ ํ•ฉ ๊ตฌํ•œ๋‹ค. (ํ•ฉ์„ ๊ตฌํ•ด์•ผํ•  ๋•Œ๋Š” sum ๋ณ€์ˆ˜ ๋งŒ๋“ค์–ด์ฃผ์–ด์•ผํ•จ. ์—ฌ๊ธฐ์„œ๋Š” ํ–‰์˜ ํ•ฉ, ์—ด์˜ ํ•ฉ ํ•„์š”ํ•ด์„œ 2๊ฐœ ์„ ์–ธ)

2. ๊ฐ ๋Œ€๊ฐ์„ ์˜ ํ•ฉ ๊ตฌํ•œ๋‹ค.

3. ๋น„๊ตํ•ด์„œ ์ตœ๋Œ€๊ฐ’์„ ๊ตฌํ•ด์„œ answer์— ๋„ฃ๋Š”๋‹ค. 

4. answer ์ถœ๋ ฅ

function solution(arr){
            let answer = Number.MIN_SAFE_INTEGER;
            // 1. answer๋Š” ์ตœ๋Œ€๊ฐ’์„ ์ฐพ์•„์•ผํ•˜๋‹ˆ๊นŒ ์ตœ์†Œ๊ฐ’์œผ๋กœ ์ดˆ๊ธฐํ™” ํ•ด๋†“๋Š”๋‹ค. 
            let n = arr.length;
            let sum1 = 0;
            let sum2 = 0;
            // ํ–‰์˜ ํ•ฉ์€ sum1๋กœ ์ €์žฅ
            // ์—ด์˜ ํ•ฉ์€ sum2๋กœ ์ €์žฅ
            // ํ–‰ ํƒ์ƒ‰, ์—ด ํƒ์ƒ‰์„ ์ด์ค‘for๋ฌธ์œผ๋กœ
            for(let i = 0; i < n; i++){
                sum1 = 0;
                sum2 = 0;
                // for๋ฌธ์ด ํ•œ ๋ฒˆ ๋๋‚  ๋•Œ๋งˆ๋‹ค ๋งค๋ฒˆ ์ดˆ๊ธฐํ™”๋ฅผ ์‹œ์ผœ์ค˜์•ผํ•œ๋‹ค.
                for(let j = 0; j < n; j++){
                    sum1 += arr[i][j]; // i๊ฐ€ ๋Œ๊ณ  j ๊ณ ์ •(i๊ธฐ์ค€ j์˜ ๋ชจ๋“  ๊ฐ’ ๋”ํ•˜์—ฌ ๋ˆ„์ )
                    sum2 += arr[j][i]; // j๊ฐ€ ๋Œ๊ณ  i ๊ณ ์ •(j๊ธฐ์ค€ i์˜ ๋ชจ๋“  ๊ฐ’ ๋”ํ•˜์—ฌ ๋ˆ„์ )
                    // ํ–‰์ด ๋Œ๊ณ  ์—ด์ด ๊ณ ์ •๋œ๋‹ค
                }
                answer = Math.max(answer, sum1, sum2); 
                // ์ตœ๋Œ€๊ฐ’ ๊ตฌํ•˜๋Š” ๋ฉ”์†Œ๋“œ
            }
            
            // ์ด์ œ ๋Œ€๊ฐ์„ ์˜ ํ•ฉ ๊ตฌํ•˜๋Š” for๋ฌธ ์ž‘์„ฑ
            // ์ƒˆ๋กœ ๋Œ€๊ฐ์„  ํ•ฉ ๊ตฌํ•ด์•ผํ•ด์„œ ์ดˆ๊ธฐํ™” ํ•ด์ฃผ์–ด์•ผํ•œ๋‹ค.
            sum1 = 0; 
            sum2 = 0;
            for(let i = 0; i < n; i++){
                sum1 = arr[i][i]; // ๋˜‘๊ฐ™์€ ์ธ๋ฑ์Šค์˜ ํ–‰๊ณผ ์—ด 
                sum2 = arr[i][n-i-1]; // sum1๊ณผ ๋Œ€์นญ์ธ ๊ฐ’์„ ๊ตฌํ•ด์ฃผ์–ด์•ผํ•œ๋‹ค.
            }
            answer = Math.max(answer, sum1, sum2);
            return answer;
    }
        

llet 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));

ํ–‰๊ณผ ์—ด์„ ๊ฐ™์ด ํƒ์ƒ‰ํ•ด์•ผํ•˜๋ฏ€๋กœ ์ด์ค‘for๋ฌธ์„ ์‚ฌ์šฉํ•ด์ค€๋‹ค.

ํ–‰ -> i / ์—ด -> j

function solution(arr){
        let answer;
        let n = arr.length;
        let sum1 = 0;
        let sum2 = 0;
        for(let i = 0; i < n; i++){
            sum1 = 0;
            sum2 = 0;
            for(let j = 0; j < n; j++){
                sum1 += arr[i][j];
                sum2 += arr[j][i];
            }
            answer = Math.max(sum1, sum2);
        }
        sum1 = 0;
        sum2 = 0;
        for(let i = 0; i < n; i++){
                sum1 += arr[i][i];
                sum2 += arr[i][n-i-1];
        }
        answer = Math.max(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));

answer์— ๊ฐ€์žฅ ์ž‘์€ ์ •์ˆ˜๋ฅผ ์จ์ฃผ์ง€ ์•Š๊ณ ๋„ ์ž‘์„ฑ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

 

์—ฌ๊ธฐ์„œ ๋” ์•Œ์•„๋ณด์•„์•ผํ•  ๊ฐœ๋…! 

for๋ฌธ์ด ๋Œ๋•Œ๋งˆ๋‹ค sum ๋ณ€์ˆ˜๋ฅผ ์ดˆ๊ธฐํ™”ํ•ด์ฃผ๋Š” ๊ฒƒ.

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์Šค์ฝ”ํ”„ ๊ฐœ๋… ์ •๋ฆฌํ•  ๊ฒƒ

 

 

 

์•Œ๊ฒŒ๋œ ๊ฐœ๋…

2์ฐจ์› ๋ฐฐ์—ด

๋ฐฐ์—ด์•ˆ์— ๋˜ ๋‹ค๋ฅธ ๋ฐฐ์—ด์ด ์žˆ๋Š” ๊ฒƒ 

2์ฐจ์› ๋ฐฐ์—ด์€  ํ–‰๊ณผ ์—ด์ด ์žˆ๋‹ค. 

ํ–‰๊ณผ ์—ด์ด ์žˆ์„๋•Œ, ํ–‰๋ถ€ํ„ฐ ์จ์ค€๋‹ค.

arr[ํ–‰][์—ด]

 

 

 

+ Recent posts