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

홈런볼·2023년 7월 12일
0

프로그래머스

목록 보기
13/36

문제링크

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

문제접근

각 성격유형과 값을 가지고 있어야 하므로 키-값 을 갖는 map 자료구조를 이용

  1. 성격유형 키-값을 담을 type 변수 선언 후 각 성격유형을 0으로 초기화
  2. choices를 돌면서 각 원소가 4 초과 -> type의 두번째 문자의 값을 get하고, 4를 기준으로 값이 커지기 때문에 4를 빼서 put
  3. 각 원소가 4 미만 -> type의 첫번째 문자의 값을 get하고, 4를 기준으로 값이 작아지기 때문에 4에서 원소를 빼서 put
  4. 각 지표끼리 비교하고 지표가 큰 값을 사전 순으로 answer에 append 해줌

코드

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

class Solution {
    public String solution(String[] survey, int[] choices) {
        StringBuilder answer = new StringBuilder();
        Map<Character,Integer> type = new HashMap<>();

        type.put('R',0); 
        type.put('T',0);
        type.put('C',0); 
        type.put('F',0);
        type.put('J',0); 
        type.put('M',0);
        type.put('A',0); 
        type.put('N',0);

        for(int i=0;i<choices.length;i++){
            if(choices[i]> 4) {
                char t = survey[i].charAt(1);
                type.put(t,type.get(t)+choices[i] - 4);
            }
            else if(choices[i] < 4) {
                char t = survey[i].charAt(0);
                type.put(t,type.get(t)+ (4-choices[i]));
            }
        }

        answer.append(type.get('R')<type.get('T') ? 'T':'R'); 
        answer.append(type.get('C')<type.get('F') ? 'F':'C');
        answer.append(type.get('J')<type.get('M') ? 'M':'J');
        answer.append(type.get('A')<type.get('N') ? 'N':'A');

        return answer.toString();
    }
}

정확성 테스트

0개의 댓글