[Programmers] 2022 KAKAO TECH INTERNSHIP - 성격 유형 검사하기

zzenee·2022년 9월 15일
0

Algorithm&Coding-test

목록 보기
19/30
post-thumbnail

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

Problem

Code

import java.util.*;

// 1: RT, 2:CF, 3: JM, 4: AN

class Solution {
    public String solution(String[] survey, int[] choices) {
        String answer = "";
        String[] types = {"RT", "CF", "JM", "AN"};
        
        HashMap<Character, Integer> hmap = new HashMap<>();
        
        for (int i=0; i<survey.length; i++) {
            char ch1 = survey[i].toCharArray()[0];
            char ch2 = survey[i].toCharArray()[1];
            if (choices[i] > 4) hmap.put(ch2, hmap.getOrDefault(ch2, 0) + choices[i] - 4);
            else if (choices[i] < 4) hmap.put(ch1, hmap.getOrDefault(ch1, 0) + 4 - choices[i]);
        }
        
        for (String type : types) {
            Character result = type.toCharArray()[0];
            Integer score1 = hmap.getOrDefault(type.toCharArray()[0], 0);
            Integer score2 = hmap.getOrDefault(type.toCharArray()[1], 0);
            if (score1 == score2) {
                if (result > type.toCharArray()[1]) {
                    result = type.toCharArray()[1];
                }
            } else if (score1 < score2) {
                result = type.toCharArray()[1];
            }
            answer += result + "";
        }
    
        return answer;
    }
}

Result

profile
꾸준히

0개의 댓글