기록하는 개발자

[프로그래머스][JAVA] 모의고사 본문

Algorithm

[프로그래머스][JAVA] 모의고사

밍맹030 2021. 6. 11. 21:41
728x90

import java.util.*;
class Solution {
    public int[] solution(int[] answers) {
        int[] temp = {0,0,0};
        int[] one={1,2,3,4,5};
        int[] two={2,1,2,3,2,4,2,5};
        int[] three={3,3,1,1,2,2,4,4,5,5};
        int max=0, count=3;
        
        for(int i=0; i<answers.length; i++){
            if (answers[i] == one[i % 5]) temp[0]++;
            if (answers[i] == two[i % 8]) temp[1]++;
            if (answers[i] == three[i % 10]) temp[2]++;
        }

        max=(temp[0]>=temp[1])? temp[0] : temp[1]; //최댓값 찾기
        max=(max>=temp[2])? max : temp[2];
        
        if (max == 0) //최댓값이 0인 경우 빈 배열 return
            return new int[] {};
		
        List<Integer> result = new ArrayList<Integer>();
        for (int i = 0; i < count; i++) { 
            if (temp[i] == max) //최댓값을 가진 사람을 list에 추가
                result.add(i);
        }

        int[] answer = new int[result.size()];	// 최종 배열 생성
        
        for (int i = 0; i < size; i++) 
            answer[i] = result.get(i) + 1; //i가 0부터 시작했으므로 1 더한 값으로 초기화

        Arrays.sort(answer); //오름차순 정렬

        return answer;
    }
}
출처: 프로그래머스 코딩 테스트 연습, https://programmers.co.kr/learn/courses/30/lessons/42840
728x90