๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

ํ”„์—” ๊ณต๋ถ€/๐Ÿซง ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ณต๋ถ€

[์•Œ๊ณ ๋ฆฌ์ฆ˜ || ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์™„์ฃผํ•˜์ง€ ๋ชปํ•œ ์„ ์ˆ˜

728x90

๋ฌธ์ œ ์ž์ฒด๋Š” ์‰ฌ์›Œ๋ณด์˜€์œผ๋‚˜, 

๊ทธ๋ž˜ ๊ทธ๋Ÿผ ๊ทธ๋ ‡์ง€! 10๋งŒ..????

 

์–ด๋–ป๊ฒŒ ํ•ด์•ผ ๊ฐ„๋‹จํ•˜๊ณ  ์‹œ๊ฐ„๋„ ์ ๊ฒŒ ๊ฑธ๋ฆด๊นŒ..............

includes ๋“ฑ์˜ ๋ฐฐ์—ด์„ ์ „๋ถ€ ํƒ์ƒ‰ํ•˜๊ฑฐ๋‚˜ ์žฌํ• ๋‹น ์‹œํ‚ค๋Š” ๋“ฑ์˜ ๋กœ์ง์€ ์‹œ๊ฐ„์ด ๋„ˆ๋ฌด ์˜ค๋ž˜ ๊ฑธ๋ฆฐ๋‹ค!

 

๊ฐœ์ˆ˜๋ฅผ ์„ธ์„œ ๋น„๊ตํ•ด๋ณด๊ธฐ๋กœ ํ–ˆ๋‹ค.

 

function solution(participant, completion) {
    const pCount = {};
    participant.map(v => pCount[v] = 0)
    participant.map(v => pCount[v]++)

    const cCount = {};
    completion.map(v => cCount[v] = 0)
    completion.map(v => cCount[v]++)

    for (const name of Object.keys(pCount)) {
        if (!cCount[name]) return name;
        if (cCount[name] !== pCount[name]) return name;
    }

}

 

์˜ค๋Š˜๋„ ์ค‘๋ณต ์ฝ”๋“œ ๋‚จ๋ฐœํ•จ. 

 

์˜ค๋Š˜์€ ๋‹ด๋‹˜์ด๋ž‘ ๋‘˜์ด ํ•ด์„œ 

๋‹ด๋‹˜ ์ฝ”๋“œ ์ค‘์— ์ข‹์€ ๋ถ€๋ถ„ ์Œ”๋น„์ง€ ํ–ˆ๋‹คใ…‹ใ…‹ใ…‹ใ…‹ใ…‹

 

function makeObj (arr,obj){
    for(i of arr){
        obj[i] = (obj[i] || 0) + 1;
    }
}

ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ํ—ˆ๋ฝ ๋งก๊ณ  ์Œ”๋น„์ง€

์•ˆ๊ทธ๋ž˜๋„ map ์„ ๋‘๋ฒˆ ์“ฐ๋ฉด์„œ ํ•œ๋ฒˆ์œผ๋กœ ์–ด๋–ป๊ฒŒ ์ค„์ผ ์ˆ˜ ์žˆ๋‚˜ ๊ถ๊ธˆํ–ˆ๋Š”๋ฐ, 

์†์ด ์‹œ์›ํ•˜๋‹ค!

 

 

function solution(participant, completion) {
    const count = (arr) => {
        const count = {};
        arr.map(v => {
            count[v] = (count[v] || 0) + 1;
        })
        return count;
    }
    for (const name of Object.keys(count(participant))) {
        if (!count(completion)[name]) return name;
        if (count(completion) !== count(participant)[name]) return name;
    }
}

๊ต‰์žฅํžˆ ๊น”๋”ํ•ด์ง„ ์ฝ”๋“œ!

 

์˜ค๋Š˜ ์‹œ๊ฐ„ ๋‚ด์— ํ’€์–ด์„œ ๊ธฐ๋ถ„ ์ข‹๊ฒŒ ๋!

 

728x90
๋ฐ˜์‘ํ˜•