[Programmers] 성격 유형 검사하기

2023. 8. 3. 14:01Computer Sciences/Problem Solve

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

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

문제 설명

간단한 구현 문제이다. 해결하는 방법에는 여러 방법이 있겠지만 필자는 HashMap으로 성격 유형 검사 결과를 관리했다. 고려해야할 부분은 성격 유형이 항상 일관되게 주어지지 않아 두 문자를 구별해야 하는 점이다. 이는 choice 값에서 4를 빼서 해결했다. choice - 4한 값이 0보다 크면 뒷 문자이고, 작으면 앞 문자라고 판단했다. 그 외에 특별히 어려운 점은 없다.

코드

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

class Solution {
    public String solution(String[] survey, int[] choices) {
        Map<String, Integer> personalityType = new HashMap<>();
        
        init(personalityType);
        
        for (int i = 0; i < survey.length; i++) {
            String[] types = survey[i].split("");
            int choice = choices[i];
            
            int result = choice - 4;
            int selectedType = result < 0 ? 0 : 1;
            
            personalityType.put(
                types[selectedType],
                personalityType.get(types[selectedType]) + Math.abs(result));
        }
        
        StringBuilder sb = new StringBuilder();
        
        sb.append(personalityType.get("R") >= personalityType.get("T") ? "R" : "T");
        sb.append(personalityType.get("C") >= personalityType.get("F") ? "C" : "F");
        sb.append(personalityType.get("J") >= personalityType.get("M") ? "J" : "M");
        sb.append(personalityType.get("A") >= personalityType.get("N") ? "A" : "N");
        
        return sb.toString();
    }
    
    private void init(Map<String, Integer> map) {
        map.put("R", 0);
        map.put("T", 0);
        map.put("C", 0);
        map.put("F", 0);
        map.put("J", 0);
        map.put("M", 0);
        map.put("A", 0);
        map.put("N", 0);
    }
}