Deep_Dev
article thumbnail

๋ฌธ์ œ ์„ค๋ช…

์ˆ˜ํฌ์ž๋Š” ์ˆ˜ํ•™์„ ํฌ๊ธฐํ•œ ์‚ฌ๋žŒ์˜ ์ค€๋ง์ž…๋‹ˆ๋‹ค. ์ˆ˜ํฌ์ž ์‚ผ์ธ๋ฐฉ์€ ๋ชจ์˜๊ณ ์‚ฌ์— ์ˆ˜ํ•™ ๋ฌธ์ œ๋ฅผ ์ „๋ถ€ ์ฐ์œผ๋ ค ํ•ฉ๋‹ˆ๋‹ค. ์ˆ˜ํฌ์ž๋Š” 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ํ•˜๋Š” ๊ฐ’์„ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌํ•ด์ฃผ์„ธ์š”.
์ž…์ถœ๋ ฅ ์˜ˆanswersreturn
[1,2,3,4,5] [1]
[1,3,2,4,2] [1,2,3]

 

 

 

 

 


ํ’€๋ฉด์„œ ์ฃผ์˜ํ• ์ ์ด, ์˜ˆ์‹œ ์ž…์ถœ๋ ฅ์— ์ฃผ์–ด์ง„ answers์˜ ๊ธธ์ด๋Š” 5๋กœ ๊ณ ์ •๋œ๊ฒŒ์•„๋‹ˆ๋‹ค..

์˜ˆ์‹œ์ผ๋ฟ, answer์˜ ๊ธธ์ด๋Š” 10์ด ๋„˜์–ด๊ฐˆ์ˆ˜๋„์žˆ์Œ.

 

๊ทธ๋ž˜์„œ i%5, i%8,i%10์„ ํ•˜๋Š” ์ด์œ ๊ฐ€ 1,2,3๋ฒˆ์งธ ํ•™์ƒ์˜ ๋ฒ”์œ„๋ฅผ ๋„˜์–ด๊ฐ€๋ฉด %๋ฅผ ์ด์šฉํ•ด์„œ, ๋ฐ˜๋ณต๋˜๋Š” ํ•˜๋‚˜์˜ ๋ฐฐ์—ด์—์„œ

index๋ฅผ ๊ณ„์† ์ฐพ๊ธฐ์œ„ํ•ด์„œ .. 

 

 

import java.util.ArrayList;
class Solution {
    public int[] solution(int[] answers) {
        int[] first = {1,2,3,4,5}; // 5๊ฐœ์”ฉ ๋ฐ˜๋ณต
        int[] second = {2,1,2,3,2,4,2,5}; // 8๊ฐœ์”ฉ ๋ฐ˜๋ณต
        int[] third = {3,3,1,1,2,2,4,4,5,5}; // 10๊ฐœ์”ฉ ๋ฐ˜๋ณต
        int[] score = {0,0,0}; // ๊ฐ ์ˆ˜ํฌ์ž๋“ค์˜ ์ ์ˆ˜
        
        // ์ˆ˜ํฌ์ž๋“ค์˜ ์ ์ˆ˜ ๊ณ„์‚ฐ
        for(int i=0; i<answers.length; i++) {
            if(answers[i] == first[i%5]) score[0]++;
            if(answers[i] == second[i%8]) score[1]++;
            if(answers[i] == third[i%10]) score[2]++;
        }
        
        // ์ตœ๋Œ€ ์ ์ˆ˜ ๊ตฌํ•˜๊ธฐ
        int max = Math.max(score[0], Math.max(score[1], score[2]));
        
        // ์ตœ๋Œ€ ์ ์ˆ˜๋ฅผ ๊ฐ€์ง„ ์ˆ˜ํฌ์ž ๋ฆฌ์ŠคํŠธ ์ƒ์„ฑ
        ArrayList<Integer> answ = new ArrayList<Integer>();
        for(int i=0; i<score.length; i++) if(max == score[i]) answ.add(i+1);
        
        int[] answer = new int[answ.size()];
        for(int i=0; i<answ.size(); i++){
            answer[i] = answ.get(i);
        }
        return answer;
    }
}