๋ฌธ์ œ 

์ˆ˜ํฌ์ž๋Š” ์ˆ˜ํ•™์„ ํฌ๊ธฐํ•œ ์‚ฌ๋žŒ์˜ ์ค€๋ง์ž…๋‹ˆ๋‹ค. ์ˆ˜ํฌ์ž ์‚ผ์ธ๋ฐฉ์€ ๋ชจ์˜๊ณ ์‚ฌ์— ์ˆ˜ํ•™ ๋ฌธ์ œ๋ฅผ ์ „๋ถ€ ์ฐ์œผ๋ ค ํ•ฉ๋‹ˆ๋‹ค. ์ˆ˜ํฌ์ž๋Š” 1๋ฒˆ ๋ฌธ์ œ๋ถ€ํ„ฐ ๋งˆ์ง€๋ง‰ ๋ฌธ์ œ๊นŒ์ง€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ฐ์Šต๋‹ˆ๋‹ค. 1๋ฒˆ ์ˆ˜ํฌ์ž๊ฐ€ ์ฐ๋Š” ๋ฐฉ์‹: 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, ... 2๋ฒˆ ์ˆ˜ํฌ์ž๊ฐ€ ์ฐ๋Š” ๋ฐฉ์‹: 2, 1, 2, 3, 2, 4, 2, 5, 2, 1, 2, 3, 2, 4, 2, 5, ... 3๋ฒˆ ์ˆ˜ํฌ์ž๊ฐ€ ์ฐ๋Š” ๋ฐฉ์‹: 3, 3, 1, 1, 2, 2, 4, 4, 5, 5, 3, 3, 1, 1, 2, 2, 4, 4, 5, 5, ... 1๋ฒˆ ๋ฌธ์ œ๋ถ€ํ„ฐ ๋งˆ์ง€๋ง‰ ๋ฌธ์ œ๊นŒ์ง€์˜ ์ •๋‹ต์ด ์ˆœ์„œ๋Œ€๋กœ ๋“ค์€ ๋ฐฐ์—ด answers๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ๊ฐ€์žฅ ๋งŽ์€ ๋ฌธ์ œ๋ฅผ ๋งžํžŒ ์‚ฌ๋žŒ์ด ๋ˆ„๊ตฌ์ธ์ง€ ๋ฐฐ์—ด์— ๋‹ด์•„ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”.

 

์กฐ๊ฑด

์‹œํ—˜์€ ์ตœ๋Œ€ 10,000 ๋ฌธ์ œ๋กœ ๊ตฌ์„ฑ๋˜์–ด์žˆ์Šต๋‹ˆ๋‹ค.
๋ฌธ์ œ์˜ ์ •๋‹ต์€ 1, 2, 3, 4, 5์ค‘ ํ•˜๋‚˜์ž…๋‹ˆ๋‹ค.
๊ฐ€์žฅ ๋†’์€ ์ ์ˆ˜๋ฅผ ๋ฐ›์€ ์‚ฌ๋žŒ์ด ์—ฌ๋Ÿฟ์ผ ๊ฒฝ์šฐ, returnํ•˜๋Š” ๊ฐ’์„ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌํ•ด์ฃผ์„ธ์š”.

 

 

์ฝ”๋“œ

const solution = (answers) => {
  let answer = [];
  const list = [
    [1, 2, 3, 4, 5],
    [2, 1, 2, 3, 2, 4, 2, 5],
    [3, 3, 1, 1, 2, 2, 4, 4, 5, 5]
  ];

  let point = [0, 0, 0];
  let max = 0;

  for (let i = 0; i < answers.length; i++) {
    if (answers[i] === list[0][i % 5]) {
      point[0]++;
    }
    if (answers[i] === list[1][i % 8]) {
      point[1]++;
    }
    if (answers[i] === list[2][i % 10]) {
      point[2]++;
    }
  }

  for (let j = 0; j < point.length; j++) {
    if (point[j] > max) {
      max = point[j];
    }
  }

  for (let k = 0; k < point.length; k++) {
    if (max === point[k]) {
      answer.push(k + 1);
    }
  }

  if (answer.length !== 1) {
    answer.sort((a, b) => a - b);
  }
  return answer;
}

์ฐ๋Š” ๋ฐฉ์‹์„ ๋ฆฌ์ŠคํŠธ๋กœ ๋ฐฐ์—ด ์•ˆ์— ๋‹ด๋Š”๋‹ค. for๋ฌธ์œผ๋กœ answers์˜ i์™€ list[0][0] -  list[0][5]๊นŒ์ง€ ์ฐพ๋Š”๋‹ค.  

 

 

๋‹ค๋ฅธ ํ’€์ด

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;
}

์ฐ๋Š” ๋ฐฉ์‹ 1,2,3์„ ๋ฐฐ์—ด๋กœ ๋งŒ๋“ค์–ด๋†“๋Š”๋‹ค. answers ๋ฐฐ์—ด๊ณผ a1, a2, a3 ๋ฐฐ์—ด์„ filter๋ฉ”์†Œ๋“œ๋กœ ๋‹ค๋ฅธ ๊ฒƒ์€ ๊ฑท์–ด๋‚ด๋ฉฐ ๊ฐ‡์€ ๊ฒƒ ๋งŒ ์žˆ๋Š” ๋ฐฐ์—ด์„ ๊ตฌํ•˜๊ณ  length๋ฅผ ๊ตฌํ•œ๋‹ค. ์„ธ ๊ฐœ ์ค‘์—์„œ ํฐ ๊ฐ’์„ ์ฐพ๋Š”๋‹ค. max์ธ ๊ฐ’์„ answer ๋ฐฐ์—ด์— push ํ•œ๋‹ค. 

+ Recent posts