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

youngkyu MIn·2024년 1월 3일

문제링크 - 프로그래머스 - 성격 유형 검사하기

class Solution {
    public String solution(String[] survey, int[] choices) {
      
        int[] scores = new int[8]; 
        for (int i = 0; i < survey.length; i++) {
            int score = 0;
            if (choices[i] <= 3) {               
                score = 4 - choices[i]; 
                scores[getIndex(survey[i].charAt(0))] += score;
            } else if (choices[i] >= 5) {
                score = choices[i] - 4; 
                scores[getIndex(survey[i].charAt(1))] += score;
            }
        }

        return getType(scores);
    }

    private int getIndex(char type) {
        return "RTCFJMAN".indexOf(type);
    }

    private String getType(int[] scores) {
        StringBuilder result = new StringBuilder();
        String[] types = {"RT", "CF", "JM", "AN"};

        for (String type : types) {
            if (scores[getIndex(type.charAt(0))] >= scores[getIndex(type.charAt(1))]) {
                result.append(type.charAt(0));
            } else {
                result.append(type.charAt(1));
            }
        }

        return result.toString();
    }
}

1 각 유형들에 대한 점수를 기록하기 위한 scores 배열

2 각 survey 를 순회하여 답변에 따라 각 유형에 점수 추가

3 getType 에서 각각 쌍을 이루는 유형에 대해 점수를 비교하여 올바른 유형을 반환

4 getIndex 는 각 유형들에 대해 index 값을 작성

profile
한 줄 소개

0개의 댓글