본문 바로가기
프로그래머스/[프로그래머스 - JAVA] Lv.1

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

by 코딩하는 흰둥이 2023. 4. 7.
반응형


입출력 예

answers return
[1,2,3,4,5] [1]
[1,3,2,4,2] [1,2,3]

 

입출력 예 설명

입출력 예 #1

  • 수포자 1은 모든 문제를 맞혔습니다.
  • 수포자 2는 모든 문제를 틀렸습니다.
  • 수포자 3은 모든 문제를 틀렸습니다.

따라서 가장 문제를 많이 맞힌 사람은 수포자 1입니다.

 

입출력 예#2

  • 모든 사람이 2문제씩을 맞췄습니다.

  • 내 풀이
import java.util.ArrayList;
import java.util.List;
class Solution {
    public int[] solution(int[] answers) {
        int[] answer = {};
        
        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[] score = new int[3];
        // 3명의 변수값 확인
        int[] check = new int[3];

        for (int i = 0; i < answers.length; i++) {

            /**
             * 삼인방의 답과 답안지의 답을 비교해서 같으면 ++
             */
            if (one[check[0]++] == answers[i]){
                score[0]++;
            }

            if (two[check[1]++] == answers[i]){
                score[1]++;
            }

            if (three[check[2]++] == answers[i]){
                score[2]++;
            }

            /**
             * 삼인방의 답안지 배열이 끝이면 0부터 다시 시작
             */
            if(check[0] >= one.length){
                check[0] = 0;
            }
            if(check[1] >= two.length){
                check[1] = 0;
            }
            if(check[2] >= three.length){
                check[2] = 0;
            }
        }

        // 맞은 사람 중에 제일 높은 점수 가져오기
        int max = Math.max(score[0] , Math.max(score[1], score[2]));

        // 제일 높은 점수와 같은 사람이 있으면 list에 넣는다 동점자가 있으면 순차적으로 넣어지게 되므로 오름차순이 된다
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < score.length; i++) {
            if (max == score[i]){
                list.add(i+1);
            }
        }
        // answer 배열 초기화 및 순서 담기
        answer = new int[list.size()];
        for (int i = 0; i < list.size(); i++) {
            answer[i] = list.get(i);
        }
        
        return answer;
    }
}

다른 분들 풀이 참고해서 풀었다

삼인방의 배열이 끝나면 각각 0부터 다시 시작해야하는게 관건.

제일 많이 맞춘 사람을 구하는 건데 문제를 잘못 생각해서 마지막 부분에서 오래 걸렸다

제일 많이 맞춘 사람 혹은 동점인 사람으로 오름차순으로 할것

 

  • 다른 사람 풀이
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();
    }
}

a[i%a.length] 

이 방법을 사용하면 따로 0부터 초기화 시켜줄 필요 없이 배열의 length를 계속 돌릴 수 있다.

참고

댓글