알고리즘, 코딩테스트/알고리즘 풀이

[프로그래머스/ 2022 KAKAO TECH INTERNSHIP] 성격 유형 검사하기

jiyoon0000 2025. 3. 10. 21:50

Q. 성격 유형 검사하기

A1.

class Solution {
    public String solution(String[] survey, int[] choices) {
        // 성격 유형 검사 선택 점수를 저장할 배열
        // ASCII 코드 인덱스 사용 -> 배열 크기 128로 지정
        int[] scores = new int[128];
        
        // survey와 choices 배열의 길이 같음
        // 각 질문마다 두 글자로 구성된 survey[i], choices[i]가 주어질 때 점수 계산
        for (int i = 0; i < survey.length; i++) {
            String sur = survey[i];
            int choice = choices[i];
            
            // 선택지가 4(모르겠음)인 경우 점수가 0이므로 이를 기준으로 점수 계산
            // 4보다 작을 경우 비동의, 4보다 클 경우 동의
            // 비동의 choices 1~3일 경우 survey의 첫번째 문자가 점수를 가짐
            // 동의 choices 5~7일 경우 survey의 두번째 문자가 점수를 가짐
            if (choice < 4) {
                scores[sur.charAt(0)] += (4 - choice);
            } else if (choice > 4) {
                scores[sur.charAt(1)] += (choice - 4);
            }
        }
        
        // 각 지표별로 최종 성격 유형 결정
        StringBuilder answer = new StringBuilder();
        answer.append(type(scores['R'], scores['T'], 'R', 'T'));
        answer.append(type(scores['C'], scores['F'], 'C', 'F'));
        answer.append(type(scores['J'], scores['M'], 'J', 'M'));
        answer.append(type(scores['A'], scores['N'], 'A', 'N'));
        
        return answer.toString();
    }
    
    // type이라는 메서드를 만들어 두 점수와 이에 해당하는 두 유형을 받아옴
    // 점수가 같거나 높은 쪽의 문자 반환(동점일 경우 사전 순)
    private char type(int score1, int score2, char type1, char type2) {
        if (score1 >= score2){
            return type1;
        } else {
            return type2;
        }
    }   
}

 

A2.

import java.util.HashMap;
import java.util.Map;

class Solution {
    public String solution(String[] survey, int[] choices) {
        // 각 성격 유형의 점수를 저장할 HashMap
        Map<Character, Integer> scores = new HashMap<>();
        
        // 지표별 두 성격 유형을 담은 2차원 배열 생성
        char[][] types = {
            {'R','T'},
            {'C','F'},
            {'J','M'},
            {'A','N'}
        };
        
        // types 배열의 각 행에 대한 유형을 scores에 추가하고, 초기 점수 0으로 설정
        // type[0] : 앞 유형, type[1] : 뒤 유형 -> 2차원 배열
        for (char[] type : types) {
            scores.put(type[0], 0);
            scores.put(type[1], 0);
        }
        
        // survey와 choices 배열을 통해 점수 계산
        for (int i = 0; i < survey.length; i++) {
            String sur = survey[i];
            int choice = choices[i];
            
            // abs를 사용해 선택지와 4 차이의 절대값 구해서 점수 계산
            int score = Math.abs(choice - 4);
            
            // 선택지가 비동의인 경우 첫번째 문자가 점수를 받고, 동의인 경우 두번째 문자가 점수 받음
            if (choice < 4) {
                char disagree = sur.charAt(0);
                scores.put(disagree, scores.get(disagree) + score);
            } else if (choice > 4) {
                char agree = sur.charAt(1);
                scores.put(agree, scores.get(agree) + score);
            }
        }
        
        // 성격 유형을 결정하기 위해 각 지표별로 점수 비교
        StringBuilder answer = new StringBuilder();
        
        // 두 성격 유형의 점수가 같거나 첫 번째가 더 높으면 첫 번째 문자 반환
        // 아니면 두 번째 문자 반환
        for (char[] type : types) {
            answer.append(scores.get(type[0]) >= scores.get(type[1]) ? type[0] : type[1]);
        }
        
        // 성격 유형 문자열 반환
        return answer.toString();
    }
}