알고리즘 공부/프로그래머스

프로그래머스 코딩테스트 연습 - 완전탐색 - 모의고사

HRuler 2020. 9. 16. 16:47

1. 나의 풀이

import java.util.ArrayList;

class Solution {
    public ArrayList<Integer> solution(int[] answers) {
        ArrayList<Integer> answer = new ArrayList<>();

        int [] supoza1 = {1, 2, 3, 4, 5};
        int [] supoza2 = {2, 1, 2, 3, 2, 4, 2, 5};
        int [] supoza3 = {3, 3, 1, 1, 2, 2, 4, 4, 5, 5};
        int supoza1_score = 0;
        int supoza2_score = 0;
        int supoza3_score = 0;
        for(int i = 0; i < answers.length; i = i + 1) {
            if(answers[i] == supoza1[i%supoza1.length]){
                supoza1_score += 1;
            };
            if(answers[i] == supoza2[i%supoza2.length]) {
                supoza2_score += 1;
            };
            if(answers[i] == supoza3[i%supoza3.length]) {
                supoza3_score += 1;
            };
        }
        System.out.println("1 = " + supoza1_score + ", 2 = " + supoza2_score + ", 3 = " + supoza3_score);

        answer.add(1);
        System.out.println(answer);
        if(supoza1_score > supoza2_score) {

        }else if(supoza1_score == supoza2_score) {
            answer.add(2);
        }else if(supoza1_score < supoza2_score) {
            answer.clear();
            answer.add(2);
        }
        System.out.println(answer);
        if(supoza1_score > supoza3_score || supoza2_score > supoza3_score) {

        }else if(answer.get(0) == 1) {
            if(supoza1_score > supoza3_score) {

            }else if(supoza1_score == supoza3_score) {
                answer.add(3);
            }else if(supoza1_score < supoza3_score) {
                answer.clear();
                answer.add(3);
            }
        }else if(answer.get(0) == 2) {
            if(supoza2_score > supoza3_score) {

            }else if(supoza2_score == supoza3_score) {
                answer.add(3);
            }else if(supoza2_score < supoza3_score) {
                answer.clear();
                answer.add(3);
            }
        }
        System.out.println(answer);

        return answer;
    }
}

 - 반복문 사용으로 각 3명의 정답을 기반으로 점수를 변수에 저장한 후 어레이 리스트를 활용하여 가장 높은 점수 혹은 동점의 최고점을 리턴하는 방법으로 해결

2. 다른 사람 풀이

import java.util.ArrayList;
class Solution {
    public int[] solution(int[] answer) {
        int[] a = {1, 2, 3, 4, 5};
        int[] b = {2, 1, 2, 3, 2, 4, 2, 5};
        int[] c = {3, 3, 1, 1, 2, 2, 4, 4, 5, 5};
        int[] score = new int[3];
        for(int i=0; i<answer.length; i++) {
            if(answer[i] == a[i%a.length]) {score[0]++;}
            if(answer[i] == b[i%b.length]) {score[1]++;}
            if(answer[i] == c[i%c.length]) {score[2]++;}
        }
        int maxScore = Math.max(score[0], Math.max(score[1], score[2]));
        ArrayList<Integer> list = new ArrayList<>();
        if(maxScore == score[0]) {list.add(1);}
        if(maxScore == score[1]) {list.add(2);}
        if(maxScore == score[2]) {list.add(3);}
        return list.stream().mapToInt(i->i.intValue()).toArray();
    }
}

 - 3명의 점수를 구하여 배열로 저장한 후 그 배열 중 최고점을 구하여 최고점과 동일한 사람은 ArrayList 변수에 저장하는 방법으로 해결