240426

1

๋ฌธ์ œ : ๋ถ€์กฑํ•œ ๊ธˆ์•ก ๊ณ„์‚ฐํ•˜๊ธฐ ๋‚œ์ด๋„ : Lv.0 ์„ค๋ช… :

์ƒˆ๋กœ ์ƒ๊ธด ๋†€์ด๊ธฐ๊ตฌ๋Š” ์ธ๊ธฐ๊ฐ€ ๋งค์šฐ ๋งŽ์•„ ์ค„์ด ๋Š์ด์งˆ ์•Š์Šต๋‹ˆ๋‹ค. ์ด ๋†€์ด๊ธฐ๊ตฌ์˜ ์›๋ž˜ ์ด์šฉ๋ฃŒ๋Š” price์› ์ธ๋ฐ, ๋†€์ด๊ธฐ๊ตฌ๋ฅผ N ๋ฒˆ ์งธ ์ด์šฉํ•œ๋‹ค๋ฉด ์›๋ž˜ ์ด์šฉ๋ฃŒ์˜ N๋ฐฐ๋ฅผ ๋ฐ›๊ธฐ๋กœ ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ฆ‰, ์ฒ˜์Œ ์ด์šฉ๋ฃŒ๊ฐ€ 100์ด์—ˆ๋‹ค๋ฉด 2๋ฒˆ์งธ์—๋Š” 200, 3๋ฒˆ์งธ์—๋Š” 300์œผ๋กœ ์š”๊ธˆ์ด ์ธ์ƒ๋ฉ๋‹ˆ๋‹ค. ๋†€์ด๊ธฐ๊ตฌ๋ฅผ count๋ฒˆ ํƒ€๊ฒŒ ๋˜๋ฉด ํ˜„์žฌ ์ž์‹ ์ด ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๊ธˆ์•ก์—์„œ ์–ผ๋งˆ๊ฐ€ ๋ชจ์ž๋ผ๋Š”์ง€๋ฅผ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•˜์„ธ์š”. ๋‹จ, ๊ธˆ์•ก์ด ๋ถ€์กฑํ•˜์ง€ ์•Š์œผ๋ฉด 0์„ return ํ•˜์„ธ์š”.

์ œํ•œ์‚ฌํ•ญ

  • ๋†€์ด๊ธฐ๊ตฌ์˜ ์ด์šฉ๋ฃŒ price : 1 โ‰ค price โ‰ค 2,500, price๋Š” ์ž์—ฐ์ˆ˜

  • ์ฒ˜์Œ ๊ฐ€์ง€๊ณ  ์žˆ๋˜ ๊ธˆ์•ก money : 1 โ‰ค money โ‰ค 1,000,000,000, money๋Š” ์ž์—ฐ์ˆ˜

  • ๋†€์ด๊ธฐ๊ตฌ์˜ ์ด์šฉ ํšŸ์ˆ˜ count : 1 โ‰ค count โ‰ค 2,500, count๋Š” ์ž์—ฐ์ˆ˜

ํ’€์ด

function solution(price, money, count) {
    let sumCount = 0
    let sumArray = []
   
    for(let i = 1; i <= count; i++) {
        sumCount += price
        sumArray.push(sumCount)
    }
    
    const sum = sumArray.reduce((acc, cur)=> {
        return acc + cur
    }, 0)
    
    
    return money > sum ? 0 : sum - money
}

๐Ÿ‘€ ๋‹ค๋ฅธ ์‚ฌ๋žŒ ํ’€์ด

function solution(price, money, count) {
    const tmp = price * count * (count + 1) / 2 - money;
    return tmp > 0 ? tmp : 0;
}
function solution(price, money, count) {
    let answer = 0;

    for (let i = 1; i <= count; i++) {
        answer += price * i;
    }

    return answer > money ? answer - money : 0;
}

Last updated