개발/코딩테스트

프로그래머스 118666. 성격 유형 검사하기 (JAVA)

애쿠 2022. 12. 20. 23:40

1. 문제

https://school.programmers.co.kr/learn/courses/30/lessons/118666

 

2. 풀이

성격 유형의 갯수가 많지 않기 때문에 하드 코딩하고 풀면 쉽다. 그리고 성격 유형의 점수를 직관적으로 구분하기 위해 모르겠음을 0점으로 fix 해서 result 행렬에 성격 유형 점수의 통계를 구했다.

 

3. 코드

class Solution {
    public String solution(String[] survey, int[] choices) {
        String answer = "";
        int[] score = new int[]{-3,-2,-1,0,1,2,3};
        String[] type = new String[]{"RT","CF","JM","AN"};
        String[] reverseType = new String[]{"TR","FC","MJ","NA"};
        int[] result = new int[]{0,0,0,0};

        for(int i =0;i<survey.length;i++) {
            for(int j =0; j< 4 ; j++){
                if(survey[i].equals(type[j])) {
                    result[j] = result[j] + score[choices[i]-1];
                    break;
                }
                if(survey[i].equals(reverseType[j])) {
                    result[j] = result[j] - score[choices[i]-1];
                    break;
                }
            }
        }
        for(int i =0; i<4; i++) {
            String tempAnswer = Character.toString(type[i].charAt(0));
            if(result[i] > 0) {
                tempAnswer = Character.toString(type[i].charAt(1));
            }
            answer = answer + tempAnswer;
        }
        return answer;
    }
}