[Programmers / Level1] 118666. 성격 유형 검사하기 (Java)

이하얀·2024년 9월 12일
0

🕊️ 프로그래머스

목록 보기
43/47

💡 Info




입출력 조건




입출력 예시








문제 이해


  • 주어진 조건에 따라 성격 유형 검사 결과를 리턴하는 문제


알고리즘


  • HashMap으로 구현
    • put을 이용해 각 성격 유형을 모두 넣어주기(8가지)
    • 만약 choices[i]가 4보다 작은 경우
      • 첫 번째 문자가 survey[i]에서 선택
      • 점수를 4 - choices[i]만큼 증가
    • 만약 choices[i]가 4보다 큰 경우
      • 두 번째 문자가 survey[i]에서 선택
      • 점수를 choices[i] - 4 만큼 증가
    • 정답 결과 get하기
      • R, C, J, A를 type1으로 하고, T, F, M, N을 type2로 지정
      • 만약, type1 > type2라면 -> answer에 type1 넣기
      • 그렇지 않다면 -> answer에 type2 넣기

풀이 시간 : 58분

import java.util.*;

class Solution {
    public String solution(String[] survey, int[] choices) {
        String answer = "";
        
        
        HashMap<Character, Integer> map = new HashMap<>();
        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);
        
        for (int i = 0; i < survey.length; i++) {
            if (choices[i] < 4) {
                map.put(survey[i].charAt(0), map.get(survey[i].charAt(0)) + 4 - choices[i]);
            } else if (choices[i] > 4) {
                map.put(survey[i].charAt(1), map.get(survey[i].charAt(1)) + choices[i] - 4);
            }
        }
        
        char[] typeOne = {'R', 'C', 'J', 'A'};
        char[] typeTwo = {'T', 'F', 'M', 'N'};

        for (int i = 0; i < typeOne.length; i++) {
            if (map.get(typeOne[i]) >= map.get(typeTwo[i])) {
                answer += typeOne[i];
            } else {
                answer += typeTwo[i];
            }
        }
        return answer;
    }
}


결과

profile
언젠가 내 코드로 세상에 기여할 수 있도록, BE 개발 기록 노트☘️

0개의 댓글