본문 바로가기
알고리즘/프로그래머스

[카카오 알고리즘][완전탐색] 모의고사 JAVA

by Jay Son 아기 냥이 해린 짱💖 2020. 9. 5.

난의도 : 하
출제빈도 : 높음
평균 점수 : 낮음

 

문제 요약

배열로 전달 받은 값과 수포자 1, 2, 3의 답을 비교해서 가장 높은 점수를 받은 수포자를 찾기

 

문제 전체 내용 : www.welcomekakao.com/learn/courses/30/lessons/42840

 

코딩테스트 연습 - 모의고사

수포자는 수학을 포기한 사람의 준말입니다. 수포자 삼인방은 모의고사에 수학 문제를 전부 찍으려 합니다. 수포자는 1번 문제부터 마지막 문제까지 다음과 같이 찍습니다. 1번 수포자가 찍는 ��

www.welcomekakao.com

내 답안

 

/**
     * 테스트 1 〉	통과 (0.27ms, 69.3MB)
     * 테스트 2 〉	통과 (0.28ms, 69.8MB)
     * 테스트 3 〉	통과 (0.34ms, 70.3MB)
     * 테스트 4 〉	통과 (0.32ms, 68.7MB)
     * 테스트 5 〉	통과 (0.31ms, 69MB)
     * 테스트 6 〉	통과 (0.29ms, 69.9MB)
     * 테스트 7 〉	통과 (0.95ms, 68.8MB)
     * 테스트 8 〉	통과 (0.49ms, 68.9MB)
     * 테스트 9 〉	통과 (1.29ms, 69.5MB)
     * 테스트 10 〉	통과 (0.77ms, 69.8MB)
     * 테스트 11 〉	통과 (1.79ms, 70.9MB)
     * 테스트 12 〉	통과 (1.33ms, 70.3MB)
     * 테스트 13 〉	통과 (0.34ms, 69.7MB)
     * 테스트 14 〉	통과 (1.48ms, 69.6MB)
     *
     *
     */
    public static int[] Solution(int[] answers) {
        int[] answer = {};
        ArrayList<CountItem> countList = new ArrayList<>();

        int[] student1 = new int[] {1, 2, 3, 4, 5, 1, 2, 3, 4, 5};
        int[] student2 = new int[] {2, 1, 2, 3, 2, 4, 2, 5, 2, 1, 2, 3, 2, 4, 2, 5};
        int[] student3 = new int[] {3, 3, 1, 1, 2, 2, 4, 4, 5, 5, 3, 3, 1, 1, 2, 2, 4, 4, 5, 5};

        int student1Count = 0;
        int student2Count = 0;
        int student3Count = 0;

        for (int i = 0; i <= answers.length -1; i++) {
            if (answers[i] == student1[i%student1.length]) {
                student1Count++;
            }
            if (answers[i] == student2[i%student2.length]) {
                student2Count++;
            }
            if (answers[i] == student3[i%student3.length]) {
                student3Count++;
            }
        }

        countList.add(new CountItem(1, student1Count));
        countList.add(new CountItem(2, student2Count));
        countList.add(new CountItem(3, student3Count));

        int maxCount = 0;
        for (int i = 0; i <= countList.size()-1; i++) {
            if (maxCount <= countList.get(i).getCount()) {
                maxCount = countList.get(i).getCount();
            }
        }

        for (int i = countList.size()-1; i >= 0; i--) {
            if (maxCount > countList.get(i).getCount()) {
                countList.remove(countList.get(i));
            }
        }

        answer = new int[countList.size()];

        for (int i = 0; i <= countList.size()-1; i++) {
            answer[i] = countList.get(i).getStudentName();
        }

        return answer;
    }

    static class CountItem {
        int mStudentName;
        int mCount;

        public CountItem(int studentName, int count) {
            mStudentName = studentName;
            mCount = count;
        }

        public int getStudentName() {
            return mStudentName;
        }
        public int getCount() {
            return mCount;
        }
    }

 

카카오 제출 답안 가장 인기 있는 코드

코드가 깜끔.

     /**
     * 인기 있는 코드
     * 테스트 1 〉	통과 (13.09ms, 68.4MB)
     * 테스트 2 〉	통과 (3.68ms, 69.3MB)
     * 테스트 3 〉	통과 (2.90ms, 69.4MB)
     * 테스트 4 〉	통과 (4.17ms, 70.8MB)
     * 테스트 5 〉	통과 (2.79ms, 68.8MB)
     * 테스트 6 〉	통과 (9.76ms, 70.1MB)
     * 테스트 7 〉	통과 (5.36ms, 69.2MB)
     * 테스트 8 〉	통과 (8.14ms, 68.8MB)
     * 테스트 9 〉	통과 (10.06ms, 69MB)
     * 테스트 10 〉	통과 (7.13ms, 69MB)
     * 테스트 11 〉	통과 (7.40ms, 68.8MB)
     * 테스트 12 〉	통과 (8.72ms, 70.4MB)
     * 테스트 13 〉	통과 (3.19ms, 69.1MB)
     * 테스트 14 〉	통과 (13.08ms, 71.5MB)
     *
     */

    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();
        }
    }
반응형

'알고리즘 > 프로그래머스' 카테고리의 다른 글

[카카오 알고리즘][정렬] K번째수 JAVA  (0) 2020.09.04