GaGe

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

알고리즘/[2021] 프로그래머스

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

Sorrel 2021. 6. 1. 02:26
import java.util.Arrays;
import java.util.ArrayList;
class Solution {
    public int[] solution(int[] answers) {
       
        int num1_cnt =0;
        int num2_cnt =0;
        int num3_cnt =0;
        int num1=0;
        int[] list1 = {1,2,3,4,5};
        for(int i = 0; i<answers.length; i++){
            num1=list1[i%list1.length];
            if(answers[i]==num1){
                num1_cnt++;
            }
        }
        int num2=0;
        int[] list2 ={2,1,2,3,2,4,2,5};
        for(int i = 0; i<answers.length; i++){
            num2=list2[i%list2.length];
            if(answers[i]==num2){
                num2_cnt++;
            }
        }
        int num3=0;
        int[] list3 = {3,3,1, 1, 2,2,4, 4, 5,5};
        for(int i = 0; i<answers.length; i++){
            num3=list3[i%list3.length];
            if(answers[i]==num3){
                num3_cnt++;
            }
            
        }
        int[] result = {num1_cnt, num2_cnt, num3_cnt};
        Arrays.sort(result);
        ArrayList<Integer> array = new ArrayList<Integer>();
        if(num1_cnt==result[2])
            array.add(1);
        if(num2_cnt==result[2])
            array.add(2);
        if(num3_cnt==result[2])
            array.add(3);
        int[] answer = new int[array.size()];

        int size=0;

        for(int temp : array){

            answer[size++] = temp;

        }
        return answer;
    }
}

for문 세개를 하나도 합쳤어도 될 것 같다.

Comments