[JAVA 문제 풀이] 313. 성격 유형 검사하기

프로그래머스 (118666)
Stupefyee's avatar
Jun 12, 2025
[JAVA 문제 풀이] 313. 성격 유형 검사하기
notion image
notion image
notion image
 

내가 작성한 코드

import java.util.*; class Solution { public String solution(String[] survey, int[] choices) { StringBuilder answer = new StringBuilder(); Map<Character, Integer> scoreMap = new HashMap<>(); // 유형별 점수를 저장 // 점수 매핑 for (int i = 0; i < survey.length; i++) { String s = survey[i]; // 설문 항목 int choice = choices[i]; // 선택지 char first = s.charAt(0); // 첫 번째 성격 유형 char second = s.charAt(1); // 두 번째 성격 유형 // 점수 계산 if (choice < 4) { scoreMap.put(first, scoreMap.getOrDefault(first, 0) + (4 - choice)); } else if (choice > 4) { scoreMap.put(second, scoreMap.getOrDefault(second, 0) + (choice - 4)); } } // 성격 유형 계산 >> 점수가 같은 경우를 대비 각 질문지별 성격 유형을 사전적 순서로 처리 String[] types = {"RT", "CF", "JM", "AN"}; for (String type : types) { char first = type.charAt(0); char second = type.charAt(1); int firstScore = scoreMap.getOrDefault(first, 0); // first 성격 유형의 점수 int secondScore = scoreMap.getOrDefault(second, 0); // second 성격 유형의 점수 if (firstScore >= secondScore) { answer.append(first); } else { answer.append(second); } } return answer.toString(); } }
 

다른 사람의 코드

import java.util.*; class Solution { public String solution(String[] survey, int[] choices) { String answer = ""; char[][] type = { { 'R', 'T' }, { 'C', 'F' }, { 'J', 'M' }, { 'A', 'N' } }; // 성격 유형 쌍 int[] score = { 0, 3, 2, 1, 0, 1, 2, 3 }; // 점수 배점 HashMap<Character, Integer> point = new HashMap<Character, Integer>(); // 유형 별 점수 저장 맵 // 점수 기록할 배열 초기화 for (char[] t : type) { point.put(t[0], 0); point.put(t[1], 0); } // 점수 기록 for (int idx = 0; idx < choices.length; idx++) { if (choices[idx] > 4) { point.put(survey[idx].charAt(1), point.get(survey[idx].charAt(1)) + score[choices[idx]]); } else { point.put(survey[idx].charAt(0), point.get(survey[idx].charAt(0)) + score[choices[idx]]); } } // 지표 별 점수 비교 후 유형 기입 for (char[] t : type) { answer += (point.get(t[1]) <= point.get(t[0])) ? t[0] : t[1]; } return answer; } }
 
Share article

stupefyee