๊ด€๋ฆฌ ๋ฉ”๋‰ด

Daehyunii's Dev-blog

์ž๋ฆฟ์ˆ˜์˜ ํ•ฉ(์™„์ „ ํƒ์ƒ‰) ๋ณธ๋ฌธ

๐Ÿ“š Language & CS knowledge/Algorithm (๊ธฐ์ดˆ๋ฌธ์ œํ’€์ด)

์ž๋ฆฟ์ˆ˜์˜ ํ•ฉ(์™„์ „ ํƒ์ƒ‰)

Daehyunii 2022. 8. 31. 23:52

๋ฌธ์ œ(์ถœ์ฒ˜ : ์ธํ”„๋Ÿฐ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œํ’€์ด ๊ฐ•์˜, ์ •๋ณด์˜ฌ๋ฆผํ”ผ์•„๋“œ)

 

N๊ฐœ์˜ ์ž์—ฐ์ˆ˜๊ฐ€ ์ž…๋ ฅ๋˜๋ฉด ๊ฐ ์ž์—ฐ์ˆ˜์˜ ์ž๋ฆฟ์ˆ˜์˜ ํ•ฉ์„ ๊ตฌํ•˜๊ณ , ๊ทธ ํ•ฉ์ด ์ตœ๋Œ€์ธ ์ž์—ฐ์ˆ˜๋ฅผ ์ถœ๋ ฅ ํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์„ธ์š”. ์ž๋ฆฟ์ˆ˜์˜ ํ•ฉ์ด ๊ฐ™์€ ๊ฒฝ์šฐ ์›๋ž˜ ์ˆซ์ž๊ฐ€ ํฐ ์ˆซ์ž๋ฅผ ๋‹ต์œผ๋กœ ํ•ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ 235 ์™€ 1234๊ฐ€ ๋™์‹œ์— ๋‹ต์ด ๋  ์ˆ˜ ์žˆ๋‹ค๋ฉด 1234๋ฅผ ๋‹ต์œผ๋กœ ์ถœ๋ ฅํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

โ–ฃ ์ž…๋ ฅ์„ค๋ช…
์ฒซ ์ค„์— ์ž์—ฐ์ˆ˜์˜ ๊ฐœ์ˆ˜ N(3<=N<=100)์ด ์ฃผ์–ด์ง€๊ณ , ๊ทธ ๋‹ค์Œ ์ค„์— N๊ฐœ์˜ ์ž์—ฐ์ˆ˜๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๊ฐ ์ž์—ฐ์ˆ˜์˜ ํฌ๊ธฐ๋Š” 10,000,000๋ฅผ ๋„˜์ง€ ์•Š๋Š”๋‹ค.

โ–ฃ ์ถœ๋ ฅ์„ค๋ช…
์ž๋ฆฟ์ˆ˜์˜ ํ•ฉ์ด ์ตœ๋Œ€์ธ ์ž์—ฐ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

 

โ–ฃ ์ž…๋ ฅ์˜ˆ์ œ 1
7
128 460 603 40 521 137 123

 

โ–ฃ ์ถœ๋ ฅ์˜ˆ์ œ 1

137

 

Tip

 

 

๋ฌธ์ œํ’€์ด

//๋‚ด๊ฐ€ ์ž‘์„ฑํ•œ ๋‹ต
function solution(nums){
    let result;
    let maxSum = Number.MIN_SAFE_INTEGER;

    for(let x of nums){
        let temp = x;
        let sum = 0;

        while(temp){
            sum += (temp%10);
            temp = Math.floor(temp/10);
        }

        if(sum > maxSum){
            maxSum = sum;
            result = x;
        }
        else if(sum === maxSum){
            if(x > result) result = x;
        }
    }
    return result;
}

let arr2=[128, 460, 603, 40, 521, 137, 123];
console.log(solution(arr2));