240430

๋ฌธ์ œ : ํฌ๊ธฐ๊ฐ€ ์ž‘์€ ๋ถ€๋ถ„ ๋ฌธ์ž์—ด ๋‚œ์ด๋„ : Lv.1 ์„ค๋ช… :

์ˆซ์ž๋กœ ์ด๋ฃจ์–ด์ง„ ๋ฌธ์ž์—ด t์™€ p๊ฐ€ ์ฃผ์–ด์งˆ ๋•Œ, t์—์„œ p์™€ ๊ธธ์ด๊ฐ€ ๊ฐ™์€ ๋ถ€๋ถ„๋ฌธ์ž์—ด ์ค‘์—์„œ, ์ด ๋ถ€๋ถ„๋ฌธ์ž์—ด์ด ๋‚˜ํƒ€๋‚ด๋Š” ์ˆ˜๊ฐ€ p๊ฐ€ ๋‚˜ํƒ€๋‚ด๋Š” ์ˆ˜๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ๊ฒƒ์ด ๋‚˜์˜ค๋Š” ํšŸ์ˆ˜๋ฅผ returnํ•˜๋Š” ํ•จ์ˆ˜ solution์„ ์™„์„ฑํ•˜์„ธ์š”. ์˜ˆ๋ฅผ ๋“ค์–ด, t="3141592"์ด๊ณ  p="271" ์ธ ๊ฒฝ์šฐ, t์˜ ๊ธธ์ด๊ฐ€ 3์ธ ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์€ 314, 141, 415, 159, 592์ž…๋‹ˆ๋‹ค. ์ด ๋ฌธ์ž์—ด์ด ๋‚˜ํƒ€๋‚ด๋Š” ์ˆ˜ ์ค‘ 271๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ˆ˜๋Š” 141, 159 2๊ฐœ ์ž…๋‹ˆ๋‹ค.

์ œํ•œ์‚ฌํ•ญ

  • 1 โ‰ค p์˜ ๊ธธ์ด โ‰ค 18

  • p์˜ ๊ธธ์ด โ‰ค t์˜ ๊ธธ์ด โ‰ค 10,000

  • t์™€ p๋Š” ์ˆซ์ž๋กœ๋งŒ ์ด๋ฃจ์–ด์ง„ ๋ฌธ์ž์—ด์ด๋ฉฐ, 0์œผ๋กœ ์‹œ์ž‘ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

ํ’€์ด

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(t, p) {
    let count = 0;
    for(let i=0; i<=t.length-p.length; i++) {
        let value = t.slice(i, i+p.length);
        if(+p >= +value) count++;
    }
    return count;
}

Last updated