ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [프로그래머스] 모의고사
    Coding Test 2023. 6. 11. 00:26
    728x90
    import java.util.*;
    
    class Solution {
        public ArrayList<Integer> solution(int[] answers) {
            
            // 수포자 정답 개수
            int count1 = 0;
            int count2 = 0;
            int count3 = 0;
          
            // 찍는 방식
            int[] noMath1 = {1, 2, 3, 4, 5};
            int[] noMath2 = {2, 1, 2, 3, 2, 4, 2, 5};
            int[] noMath3 = {3, 3, 1, 1, 2, 2, 4, 4, 5, 5};
            
            // return 값 ArrayList
            ArrayList<Integer> answer = new ArrayList<>();
            
            // 수포자 답안지
            ArrayList<Integer> arr1 = new ArrayList<>();
            ArrayList<Integer> arr2 = new ArrayList<>();
            ArrayList<Integer> arr3 = new ArrayList<>();
            
            // answers(정답) 길이만큼 답안지 작성
            for(int i = 0; i < answers.length; i++){
                for(int j = 0; j < noMath1.length; j++) {
                    arr1.add(noMath1[j]);
                }
            }
            
            for(int i = 0; i < answers.length; i++){
                for(int j = 0; j < noMath2.length; j++) {
                    arr2.add(noMath2[j]);
                }
            }
            
            for(int i = 0; i < answers.length; i++){
                for(int j = 0; j < noMath3.length; j++) {
                    arr3.add(noMath3[j]);
                }
            }
            
            // 정답과 일치하면 count 증가
            for(int i = 0; i < answers.length; i++) {
                if(answers[i] == arr1.get(i)) {
                    count1++;
                }
                if(answers[i] == arr2.get(i)) {
                    count2++;
                }
                if(answers[i] == arr3.get(i)) {
                    count3++;
                }
            }
            
            // 각 수포자에 해당하는 count 저장
            HashMap<Integer, Integer> map = new HashMap<>();
            map.put(1, count1);
            map.put(2, count2);
            map.put(3, count3);
            
            // map에 저장된 최대 value값(맞춘 문제 개수) 찾기
            int max = Collections.max(map.values());
            
            // Java 8 이후 버전은 key, value 찾기 간소화됨
            map.forEach((key, value) -> {
                if(max == value) { // 최대 value값과 일치하면
                    answer.add(key); // 해당 수포자 저장
                }
            });
            
            // 동점자 처리 오름차순
            Collections.sort(answer);
           
            return answer;
        }
    }
    728x90
    반응형
Designed by Tistory.