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

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

[์•Œ๊ณ ๋ฆฌ์ฆ˜ || ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ๋ชจ์˜๊ณ ์‚ฌ

728x90

<๋ฌธ์ œ ์ถœ์ฒ˜>

https://school.programmers.co.kr/learn/courses/30/lessons/42840

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”.

programmers.co.kr

 

 

 

<๋‚˜์˜ ํ’€์ด>


function solution(answers) {
    const a = [1, 2, 3, 4, 5];
    const b = [2, 1, 2, 3, 2, 4, 2, 5];
    const c = [3, 3, 1, 1, 2, 2, 4, 4, 5, 5];

    const checkFn = (pattern, answers) => {
        let test = [...answers];
        const checkCount = test.reduce((acc, cur, ind) => {
            if (ind < pattern.length) {
                return cur === pattern[ind] ? acc + 1 : acc;
            }
            else {
                if (cur) {
                    return cur === pattern[ind % pattern.length] ? acc + 1 : acc;
                } else {
                    test.slice(1);
                    return acc;
                }
            }
        }, 0)
        return checkCount;
    }

    const aR = checkFn(a, answers);
    const bR = checkFn(b, answers);
    const cR = checkFn(c, answers);
    const max = Math.max(aR, bR, cR);
    let result = [];
    [aR, bR, cR].map((v, i) => v === max && result.push(i + 1));
    return result;
}

 

์žฌ๊ท€ ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด ๋ฌธ์ œ๋ฅผ ํ’€์—ˆ๋‹ค. 

checkFn์€ ๋ฌธ์ œ์˜ ๊ธธ์ด์™€ ํŒจํ„ด์˜ ๊ธธ์ด์— ๋”ฐ๋ผ ๋งž์ถ˜ ๋ฌธ์ œ์˜ ๊ฐœ์ˆ˜๋ฅผ return ํ•ด์ค€๋‹ค. 

reduce ๋ฅผ ์ง„ํ–‰ํ•˜๋‹ค๊ฐ€ ๋ฌธ์ œ์˜ ์ˆ˜๊ฐ€ ํŒจํ„ด๋ณด๋‹ค ์ž‘์œผ๋ฉด reduce ๋ฅผ break ํ•ด์ค€๋‹ค. 

 

๋ฐ›์€ ๋ฌธ์ œ ๊ฐœ์ˆ˜๋“ค์„ ๋น„๊ตํ•ด์„œ, 

๊ฐ€์žฅ ๋งŽ์ด ๋งž์ถ˜ ์‚ฌ๋žŒ๊ณผ ๋งž์ถ˜ ๋ฌธ์ œ์˜ ์ˆ˜๊ฐ€ ๊ฐ™๋‹ค๋ฉด result ๋ฐฐ์—ด์— index+1 ์„ ํ•ด์ค€๋‹ค. (๊ทธ๋Ÿผ 1, 2, 3 ๋ฒˆ์ด ๋“ค์–ด๊ฐ.)

 

์ตœ๋Œ€ 10,000 ๋ฌธ์ œ๊ฐ€ ์ฃผ์–ด์ง„๋‹ค๋Š” ์ œํ•œ ์กฐ๊ฑด์ด ์žˆ๋Š”๋ฐ,

๋ฒ”์œ„๊ฐ€ ํฐ๊ฑฐ์— ๋น„ํ•ด ์†๋„๊ฐ€ ๊ดœ์ฐฎ์€ ๊ฒƒ ๊ฐ™๋‹ค~~

 

 

 

 

<๋‹ค๋ฅธ ์‚ฌ๋žŒ์˜ ํ’€์ด>

๋‹ค๋ฅธ ์‚ฌ๋žŒ ํ’€์ด ๋ณด๋‹ˆ ์ •๋ง ๊น”๋”ํ•˜๊ฒŒ ํ’€์—ˆ๋‹ค!

๋‚ด๊บผ ๋‹ค์‹œ ๋ณด๋‹ˆ  ์ •๋ฆฌ๊ฐ€ ์•ˆ๋œ ๋Š๋‚Œ์ด์ง€๋งŒ ๊ทธ๋ž˜๋„ 1์‹œ๊ฐ„ ๋‚ด์— ํ’€์—ˆ๋‹ค๋Š” ๊ฒƒ์— ์˜์˜๋ฅผ,,

 

function solution(answers) {
    var answer = [];
    var a1 = [1, 2, 3, 4, 5];
    var a2 = [2, 1, 2, 3, 2, 4, 2, 5]
    var a3 = [ 3, 3, 1, 1, 2, 2, 4, 4, 5, 5];

    var a1c = answers.filter((a,i)=> a === a1[i%a1.length]).length;
    var a2c = answers.filter((a,i)=> a === a2[i%a2.length]).length;
    var a3c = answers.filter((a,i)=> a === a3[i%a3.length]).length;
    var max = Math.max(a1c,a2c,a3c);

    if (a1c === max) {answer.push(1)};
    if (a2c === max) {answer.push(2)};
    if (a3c === max) {answer.push(3)};


    return answer;
}

 reduce ๋Œ€์‹  ๋งž์ถ˜ ๊ฑธ filter ํ•ด์„œ length ๋ฅผ ๊ตฌํ•˜๋Š” ๋ฐฉ๋ฒ•...!!

ํ›จ์”ฌ ๊น”๋”ํ•˜์ง€๋งŒ, 

๋งŒ์•ฝ ์ˆ˜ํฌ์ž๊ฐ€ ๋งŽ์•„์ง€๋ฉด ๊ทธ๋งŒํผ ์ฝ”๋“œ๊ฐ€ ๊ธธ์–ด์ง€๋‹ˆ๊นŒ answers.filter ๋ถ€๋ถ„์„ ์žฌ๊ท€ํ•จ์ˆ˜๋กœ ๋งŒ๋“ค์–ด๋„ ์ข‹์„ ๋“ฏ ํ•˜๋‹ค.

์•ˆ์ข‹๋‹ค๋Š” ์—ฌ๋ก ๋„ ์žˆ๋‹ค..!

 

 

max ๋ฅผ array ๋ฅผ ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•์€ ๋‚ด๊บผ๋ž‘ ๊ฐ™์•„์„œ ๊ดœํžˆ ๊ธฐ๋ถ„์ด ์ข‹๋‹ค. 

 

 

์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ณต๋ถ€๋Š” ํ•˜๋ฉด ํ•  ์ˆ˜๋ก ์ฐธ ์–ด๋ ค์šด๋ฐ ๋˜ ์žฌ๋ฐŒ๊ธฐ๋„ ํ•˜๊ณ .. 

๊ทผ๋ฐ ๋˜ ์ƒ๊ฐํ•  ๊ฒŒ ๋งŽ์•„์„œ ์–ด๋ ต๊ณ .. ๊ทธ๋ ‡๋‹ค..!

 

728x90
๋ฐ˜์‘ํ˜•