본문 바로가기

카테고리 없음

프로그래머스>완전탐색>모의고사

import java.util.*;


class Solution {
    public int[] solution(int[] answers) {
        
        
        int [] first = {1,2,3,4,5};
        int [] second= {2,1,2,3,2,4,2,5};
        int [] third = {3,3,1,1,2,2,4,4,5,5};
        
        int [] counting = new int [3];
          
      
        for(int i=0; i<answers.length; i++)
        {
             if(first[i%first.length] == answers[i])
             {
                counting[0] ++;
             }
            
             if(second[i%second.length]== answers[i])
                counting[1] ++;
            
            if (third[i%third.length] == answers[i])
                counting[2] ++;
        }
        
        int themost= counting[0];
        
        for(int i=0; i<3;i++)
        {
             if(counting[i]> themost)
            {
                themost = counting[i];
            }
        }
       
        ArrayList<Integer> themostpl = new ArrayList<>();
        
        for(int i=0;i<3; i++)
        {
             if (themost== counting[i])
             {
                 themostpl.add(i);
             }
                
        }
        
        int[] answer={};
        answer = new int[themostpl.size()];
        
        for(int i=0; i<themostpl.size();i++)
            {
                 answer [i] = themostpl.get(i) +1;
            }
          
            
      
           
                return answer;
            
       
    }
}