1 분 소요

📁 [Lv1_118666] 성격 유형 검사하기

import java.util.*;
import java.io.*;

class Solution {
    // 문자와 그 문자의 개수
    class Set{
        private char type;
        private int count;

        public Set(char type, int count){
            this.type = type;
            this.count = count;
        }

    }
    public String solution(String[] survey, int[] choices) {
        String answer = "";

        // 리스트에 문자와 개수 넣기
        List<Set> list = new ArrayList<Set>();
        list.add(new Set('R',0));
        list.add(new Set('T',0));
        list.add(new Set('C',0));
        list.add(new Set('F',0));
        list.add(new Set('J',0));
        list.add(new Set('M',0));
        list.add(new Set('A',0));
        list.add(new Set('N',0));

        for(int i=0; i<survey.length; i++){
            // 숫자
            int num = choices[i];
            // 점수
            int score = 0;
            // 만약 4보다 작으면
            if(num < 4){
                char temp = survey[i].charAt(0);
                score = 4-num;
                // 리스트 돌면서 확인
                for(int j=0; j<list.size(); j++){
                    if(temp == list.get(j).type){
                        list.get(j).count += score;
                        break;
                    }
                }
            }
            // 4보다 크면
            else if(num > 4){
                char temp = survey[i].charAt(1);
                score = num-4;
                // 리스트 돌면서 확인
                for(int j=0; j<list.size(); j++){
                    if(temp == list.get(j).type){
                        list.get(j).count += score;
                        break;
                    }
                }
            }
        }

        // 점수 체크하기
        for(int i=0; i<list.size(); i+=2){
            // 앞에것이 더 큰 경우
            if(list.get(i).count > list.get(i+1).count){
                answer += list.get(i).type;
            }
            // 뒤에것이 더 큰 경우
            else if(list.get(i).count < list.get(i+1).count){
                answer += list.get(i+1).type;
            }
            // 같은 경우 ( 사전 순으로 )
            else{
                answer += list.get(i).type;
            }
        }

        return answer;
    }
}

🤔 나의 생각

레벨 1 카카오 문제다..
이번 문제는 비교적 쉽게 접근할 수 있었다.
먼저 MBTI 처럼 4개의 지표에 2가지의 유형들이 있는데, 점수를 판단해서 더 높은 것을 선택해주면 되는 것이다.
나는 class를 이용해서 type과 count를 생성하여 표시해 주었고, 1~7인 숫자를 중간을 기점으로 1,2,3이면 앞에 type, 5,6,7이면 뒤에 type에 score를 넣어주었다.
그리고 마지막에 list를 다 돌면서 ( 2칸씩 이동 ) 더 큰 type을 answer에 넣어줘서 구현해 주었다.
어차피 MBTI의 단어들은 직접 입력해야해서 list의 Set class에 처음에 다 0으로 초기화해서 넣어줬다.
예시를 보면서 이해하면 더 쉽게 이해할 수 있던 문제였다.