






나의 풀이
import java.util.*;
class Solution {
public String solution(String[] survey, int[] choices) {
String answer = "";
HashMap<Character, Integer> map = new HashMap<>();
for (int i = 0; i < 26; i++) { // 1
map.put((char)(i + 65), 0);
}
for (int i = 0; i < survey.length; i++) { // 2
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);
}
if (map.get('T') > map.get('R')) answer += "T"; // 3
else answer += "R";
if (map.get('F') > map.get('C')) answer += "F";
else answer += "C";
if (map.get('M') > map.get('J')) answer += "M";
else answer += "J";
if (map.get('N') > map.get('A')) answer += "N";
else answer += "A";
return answer;
}
}
과정
- map에 알파벳 대문자를 key값으로 넣어준다. 밸류는 0으로 모두 초기화
- survey를 순회하며 choices[i]와 survey[i]를 비교하여 map에 넣어준다(survey와 choices는 길이가 같다)
- 점수가 같으면 사전 순으로 빠른 성격 유형을 넣어주면 되니 더 늦은 유형이 크다면 늦은 유형을 넣어주고, 그 외에는 빠른 유형을 넣어주면 된다
다른 사람 풀이
import java.util.HashMap;
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;
}
}