[프로그래머스] 성격 유형 검사하기

Choi Seong Jin·2022년 11월 7일
0

프로그래머스

목록 보기
3/33

문제 링크 : 성격 유형 검사하기








작성한 코드

public String solution(String[] survey, int[] choices) {
        String answer = "";
        String[][] indicators = {{"R", "T"}, {"C", "F"}, {"J", "M"}, {"A", "N"}};
        HashMap<String, Integer> map = new HashMap<>();
        map.put("R", 0); map.put("T", 1); map.put("C", 0); map.put("F", 1);
        map.put("J", 0); map.put("M", 1); map.put("A", 0); map.put("N", 1);
        int[][] sum = new int[4][2];
        int indicatorsIdx = 0;
        int[] score = {3,2,1,0,1,2,3};
        for(int i = 0; i < survey.length; i++){
            switch(survey[i]){
                case "RT":  case "TR":
                    indicatorsIdx = 0;
                    break;
                case "CF":  case "FC":
                    indicatorsIdx = 1;
                    break;
                case "JM":  case "MJ":
                    indicatorsIdx = 2;
                    break;
                case "AN":  case "NA":
                    indicatorsIdx = 3;
                    break;
                default:
                    indicatorsIdx = -1;
                    break;
            }
            if(choices[i] < 4){
                sum[indicatorsIdx][map.get(survey[i].substring(0,1))] += score[choices[i] - 1];
            }else{
                sum[indicatorsIdx][map.get(survey[i].substring(1))] += score[choices[i] - 1];
            }
        }
        for(int i = 0; i < 4; i++){
            if(sum[i][0] >= sum[i][1]){
                answer += indicators[i][0];
            }else{
                answer += indicators[i][1];
            }
        }
        return answer;
    }

다른 사람들의 풀이

public String solution(String[] survey, int[] choices) {
        String answer = "";
        char [][] type = {{'R', 'T'}, {'C', 'F'}, {'J', 'M'}, {'A', 'N'}};
        int [] score = {0, 3, 2, 1, 0, 1, 2, 3};
        HashMap<Character, Integer> point = new HashMap<Character, Integer>();

        // 점수 기록할 배열 초기화 
        for (char[] t : type) {
            point.put(t[0], 0);
            point.put(t[1], 0);
        }

        // 점수 기록 
        for (int idx = 0; idx < choices.length; idx++){
            if(choices[idx] > 4){
                point.put(survey[idx].charAt(1), point.get(survey[idx].charAt(1)) + score[choices[idx]]);
            } else {
                point.put(survey[idx].charAt(0), point.get(survey[idx].charAt(0)) + score[choices[idx]]);
            }
        }

        // 지표 별 점수 비교 후 유형 기입
        for (char[] t : type) {
            answer += (point.get(t[1]) <= point.get(t[0])) ? t[0] : t[1];
        }

        return answer;
    }

풀이

내가 자바를 좀 더 잘 다뤘고, 알고리즘에 익숙해져 있다면 훨씬 더 깔끔하게 풀었을 것 같아서 다른 사람의 풀이또한 찾아봤다.
나는 문제 유형에 따라서 점수가 저장되는 배열의 인덱스를 따로 구하고, 그 인덱스와 HashMap을 통해 점수를 저장했다.
다른 사람의 풀이에서는 점수가 저장되는 배열의 인덱스를 따로 구하지 않고, 구한 점수 자체를 HashMap을 통해 저장해서 코드가 좀 더 단순해지고, 구하는 과정이 깔끔해졌다.
자바의 내장함수나 자료구조 등의 이해가 높지 않다는 것이 보여지는 문제 풀이였던 것 같다.

profile
백엔드 개발자 지망생입니다!

0개의 댓글