๐Ÿงก๋ฌธ์ œ ์„ค๋ช…

XYZ ๋งˆํŠธ๋Š” ์ผ์ •ํ•œ ๊ธˆ์•ก์„ ์ง€๋ถˆํ•˜๋ฉด 10์ผ ๋™์•ˆ ํšŒ์› ์ž๊ฒฉ์„ ๋ถ€์—ฌํ•ฉ๋‹ˆ๋‹ค. XYZ ๋งˆํŠธ์—์„œ๋Š” ํšŒ์›์„ ๋Œ€์ƒ์œผ๋กœ ๋งค์ผ ํ•œ ๊ฐ€์ง€ ์ œํ’ˆ์„ ํ• ์ธํ•˜๋Š” ํ–‰์‚ฌ๋ฅผ ํ•ฉ๋‹ˆ๋‹ค. ํ• ์ธํ•˜๋Š” ์ œํ’ˆ์€ ํ•˜๋ฃจ์— ํ•˜๋‚˜์”ฉ๋งŒ ๊ตฌ๋งคํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์•Œ๋œฐํ•œ ์ •ํ˜„์ด๋Š” ์ž์‹ ์ด ์›ํ•˜๋Š” ์ œํ’ˆ๊ณผ ์ˆ˜๋Ÿ‰์ด ํ• ์ธํ•˜๋Š” ๋‚ ์งœ์™€ 10์ผ ์—ฐ์†์œผ๋กœ ์ผ์น˜ํ•  ๊ฒฝ์šฐ์— ๋งž์ถฐ์„œ ํšŒ์›๊ฐ€์ž…์„ ํ•˜๋ ค ํ•ฉ๋‹ˆ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด, ์ •ํ˜„์ด๊ฐ€ ์›ํ•˜๋Š” ์ œํ’ˆ์ด ๋ฐ”๋‚˜๋‚˜ 3๊ฐœ, ์‚ฌ๊ณผ 2๊ฐœ, ์Œ€ 2๊ฐœ, ๋ผ์ง€๊ณ ๊ธฐ 2๊ฐœ, ๋ƒ„๋น„ 1๊ฐœ์ด๋ฉฐ, XYZ ๋งˆํŠธ์—์„œ 15์ผ๊ฐ„ ํšŒ์›์„ ๋Œ€์ƒ์œผ๋กœ ํ• ์ธํ•˜๋Š” ์ œํ’ˆ์ด ๋‚ ์งœ ์ˆœ์„œ๋Œ€๋กœ ์น˜ํ‚จ, ์‚ฌ๊ณผ, ์‚ฌ๊ณผ, ๋ฐ”๋‚˜๋‚˜, ์Œ€, ์‚ฌ๊ณผ, ๋ผ์ง€๊ณ ๊ธฐ, ๋ฐ”๋‚˜๋‚˜, ๋ผ์ง€๊ณ ๊ธฐ, ์Œ€, ๋ƒ„๋น„, ๋ฐ”๋‚˜๋‚˜, ์‚ฌ๊ณผ, ๋ฐ”๋‚˜๋‚˜์ธ ๊ฒฝ์šฐ์— ๋Œ€ํ•ด ์•Œ์•„๋ด…์‹œ๋‹ค. ์ฒซ์งธ ๋‚ ๋ถ€ํ„ฐ ์—ดํ˜ ๊ฐ„์—๋Š” ๋ƒ„๋น„๊ฐ€ ํ• ์ธํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ์ฒซ์งธ ๋‚ ์—๋Š” ํšŒ์›๊ฐ€์ž…์„ ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋‘˜์งธ ๋‚ ๋ถ€ํ„ฐ ์—ดํ˜ ๊ฐ„์—๋Š” ๋ฐ”๋‚˜๋‚˜๋ฅผ ์›ํ•˜๋Š” ๋งŒํผ ํ• ์ธ๊ตฌ๋งคํ•  ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์— ๋‘˜์งธ ๋‚ ์—๋„ ํšŒ์›๊ฐ€์ž…์„ ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์…‹์งธ ๋‚ , ๋„ท์งธ ๋‚ , ๋‹ค์„ฏ์งธ ๋‚ ๋ถ€ํ„ฐ ๊ฐ๊ฐ ์—ดํ˜์€ ์›ํ•˜๋Š” ์ œํ’ˆ๊ณผ ์ˆ˜๋Ÿ‰์ด ์ผ์น˜ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์…‹ ์ค‘ ํ•˜๋ฃจ์— ํšŒ์›๊ฐ€์ž…์„ ํ•˜๋ ค ํ•ฉ๋‹ˆ๋‹ค.

์ •ํ˜„์ด๊ฐ€ ์›ํ•˜๋Š” ์ œํ’ˆ์„ ๋‚˜ํƒ€๋‚ด๋Š” ๋ฌธ์ž์—ด ๋ฐฐ์—ด want์™€ ์ •ํ˜„์ด๊ฐ€ ์›ํ•˜๋Š” ์ œํ’ˆ์˜ ์ˆ˜๋Ÿ‰์„ ๋‚˜ํƒ€๋‚ด๋Š” ์ •์ˆ˜ ๋ฐฐ์—ด number, XYZ ๋งˆํŠธ์—์„œ ํ• ์ธํ•˜๋Š” ์ œํ’ˆ์„ ๋‚˜ํƒ€๋‚ด๋Š” ๋ฌธ์ž์—ด ๋ฐฐ์—ด discount๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ํšŒ์›๋“ฑ๋ก์‹œ ์ •ํ˜„์ด๊ฐ€ ์›ํ•˜๋Š” ์ œํ’ˆ์„ ๋ชจ๋‘ ํ• ์ธ ๋ฐ›์„ ์ˆ˜ ์žˆ๋Š” ํšŒ์›๋“ฑ๋ก ๋‚ ์งœ์˜ ์ด ์ผ์ˆ˜๋ฅผ return ํ•˜๋Š” solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•˜์‹œ์˜ค. ๊ฐ€๋Šฅํ•œ ๋‚ ์ด ์—†์œผ๋ฉด 0์„ return ํ•ฉ๋‹ˆ๋‹ค.


๐Ÿ’›์ œํ•œ์‚ฌํ•ญ

  • 1 โ‰ค want์˜ ๊ธธ์ด = number์˜ ๊ธธ์ด โ‰ค 10
  • 1 โ‰ค number์˜ ์›์†Œ โ‰ค 10
  • number[i]๋Š” want[i]์˜ ์ˆ˜๋Ÿ‰์„ ์˜๋ฏธํ•˜๋ฉฐ, number์˜ ์›์†Œ์˜ ํ•ฉ์€ 10์ž…๋‹ˆ๋‹ค.
  • 10 โ‰ค discount์˜ ๊ธธ์ด โ‰ค 100,000
  • want์™€ discount์˜ ์›์†Œ๋“ค์€ ์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž๋กœ ์ด๋ฃจ์–ด์ง„ ๋ฌธ์ž์—ด์ž…๋‹ˆ๋‹ค.
  • 1 โ‰ค want์˜ ์›์†Œ์˜ ๊ธธ์ด, discount์˜ ์›์†Œ์˜ ๊ธธ์ด โ‰ค 12

๐Ÿ’š์ž…์ถœ๋ ฅ ์˜ˆ

wantnumberdiscountresult
["banana", "apple", "rice", "pork", "pot"][3, 2, 2, 2, 1]["chicken", "apple", "apple", "banana", "rice", "apple", "pork", "banana", "pork", "rice", "pot", "banana", "apple", "banana"]3
["apple"][10]["banana", "banana", "banana", "banana", "banana", "banana", "banana", "banana", "banana", "banana"]0

๐Ÿ’™์ž…์ถœ๋ ฅ ์˜ˆ ์„ค๋ช…

์ž…์ถœ๋ ฅ ์˜ˆ #1

  • ๋ฌธ์ œ ์˜ˆ์‹œ์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ #2

  • ์‚ฌ๊ณผ๊ฐ€ ํ• ์ธํ•˜๋Š” ๋‚ ์ด ์—†์œผ๋ฏ€๋กœ 0์„ return ํ•ฉ๋‹ˆ๋‹ค.

๐Ÿ’œ๋‚˜์˜ ํ’€์ด

function solution(want, number, discount) {
    // ์ •๋‹ต ๋ฐฐ์—ด
    let result = 0
    // ์‚ฌ์•ผํ•˜๋Š” ํ’ˆ๋ชฉ * ๊ฐœ์ˆ˜
    let wantThing = []
    want.forEach((a,i) => {
        for(let j = 0 ; j < number[i] ; j ++) {
            wantThing.push(want[i])
        }
    })
    // ์‚ฌ์•ผํ•˜๋Š” ํ’ˆ๋ชฉ์„ ์‚ฌ์ „์ˆœ ์ •๋ ฌ
    wantThing = wantThing.sort()
    // ํ• ์ธํ–‰์‚ฌ ์ˆœํšŒ
    for(let i = 0 ; i < discount.length ; i ++) {
        // ์•ž์œผ๋กœ 10์ผ๊ฐ„์˜ ํ–‰์‚ฌ ํ’ˆ๋ชฉ
        const nowDiscount = discount.slice(i, i+10).sort()
        // ์›ํ•˜๋Š” ๋ฌผ๊ฑด์ด ๋ชจ๋‘ ๋“ค์–ด์žˆ๋‹ค๋ฉด result++
        if(
            wantThing.every((a,j) => a === nowDiscount[j])
        ) {
            result++
        }
    }
    return result
}
profile
๋‚ด ์ง€์‹์„ ๊ณต์œ ํ•  ์ˆ˜ ์žˆ๋Š” ๋Œ€๋‹ดํ•จ

0๊ฐœ์˜ ๋Œ“๊ธ€