๋ณธ๋ฌธ์œผ๋กœ ๊ฑด๋„ˆ๋›ฐ๊ธฐ

42840. ๋ชจ์˜๊ณ ์‚ฌ

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ๋ชจ์˜๊ณ ์‚ฌ


๋ฌธ์ œ ์œ ํ˜•๋‚œ์ด๋„๊ฑธ๋ฆฐ ์‹œ๊ฐ„ํ•ด๊ฒฐ ์œ ๋ฌด(โœ…/โŒ)
์™„์ „ํƒ์ƒ‰lv.120๋ถ„โœ…

์„ค๊ณ„ ๋ฐฉ๋ฒ•#

  • ์ˆ˜ํฌ์ž๋“ค์˜ ๋ฒˆํ˜ธ ๊ทœ์น™์„ ๊ธฐ๋กํ•œ๋‹ค.

  • %ย ์—ฐ์‚ฐ์ž์™€ ๋ฒˆํ˜ธ ๊ทœ์น™ ๋ฐฐ์—ด์˜ ๊ธธ์ด๋ฅผ ์‚ฌ์šฉํ•˜์—ฌย answersย ์˜ index์™€ ์ˆ˜ํฌ์ž๋“ค์˜๋ฒˆํ˜ธ ๊ทœ์น™๊ณผ index๋ฅผ ๋งž์ถฐ ์ ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•œ๋‹ค.

  • ์ตœ๋Œ€ ์ ์ˆ˜๋ฅผ ๊ธฐ๋กํ•œ๋‹ค.

  • ์ตœ๋Œ€ ์ ์ˆ˜์™€ ๊ฐ™์€ ์‚ฌ๋žŒ์˜ ๋ฒˆํ˜ธ๋ฅผ ์ •๋‹ต ๋ฐฐ์—ด์— ์ˆœ์„œ๋Œ€๋กœ ๋„ฃ๋Š”๋‹ค.

์ฝ”๋“œ#

  • ์ฒซ ๋ฒˆ์งธ ํ’€์ด
function solution(answer) {    const person1 = [1, 2, 3, 4, 5];    const person2 = [2, 1, 2, 3, 2, 4, 2, 5];    const person3 = [3, 3, 1, 1, 2, 2, 4, 4, 5, 5];
    const score = [0, 0, 0];
    answer.forEach((n, i) => {        if (person1[i % person1.length] === n) {            score[0]++;        }        if (person2[i % person2.length] === n) {            score[1]++;        }        if (person3[i % person3.length] === n) {            score[2]++;        }    });
    return score.reduce((best, cur, idx) => {        const bestPerson = best.pop();        if (!bestPerson || cur > score[bestPerson - 1]) {            return [idx + 1];        }        best.push(bestPerson);        if (cur === score[bestPerson - 1]) {            best.push(idx + 1);        }        return best;    }, []);}
  • ๋ฆฌํŒฉํ† ๋ง
function solution(answers) {    const answer = [];
    const p1 = [1, 2, 3, 4, 5];    const p2 = [2, 1, 2, 3, 2, 4, 2, 5];    const p3 = [3, 3, 1, 1, 2, 2, 4, 4, 5, 5];
    const s1 = answers.filter((a, i) => a === p1[i % p1.length]).length;    const s2 = answers.filter((a, i) => a === p2[i % p2.length]).length;    const s3 = answers.filter((a, i) => a === p3[i % p3.length]).length;    const max = Math.max(s1, s2, s3);
    if (s1 === max) {        answer.push(1);    }    if (s2 === max) {        answer.push(2);    }    if (s3 === max) {        answer.push(3);    }
    return answer;}

์‹œ๊ฐ„ ๋ณต์žก๋„#

  • ์ •๋‹ต ๋ฐฐ์—ด ์ˆœํšŒ : O(n)

์–ด๋ ค์› ๋˜ ์ #

  • ์ตœ๋Œ€๊ฐ’์„ ๊ฐ€์ง„ ์‚ฌ๋žŒ์˜ ๋ฒˆํ˜ธ๋ฅผ ์ •๋‹ต ๋ฐฐ์—ด์— ๋„ฃ๋Š” ๋ถ€๋ถ„์ด ์–ด๋ ค์› ๋‹ค.

  • ํ•ด๊ฒฐ ํ›„ ๋‹ค๋ฅธ ์‚ฌ๋žŒ์˜ ํ’€์ด ์ค‘ ๊น”๋”ํ•œ ์ฝ”๋“œ๊ฐ€ ์žˆ์–ด์„œ, ์ฐธ๊ณ ํ•˜์—ฌ ๋ฆฌํŒฉํ† ๋งํ–ˆ๋‹ค.

์ฐธ๊ณ ์ž๋ฃŒ#