Welcome! Everything is fine.
[프로그래머스/Lv.1] 성격 유형 검사하기(2022 KAKAO TECH INTERNSHIP) - Java 본문
📌 문제
📌 풀이
성격 유형과 선택지, 성격 유형 점수를 각각 해시맵에 저장한 후 푼 문제였다. 일단 나는 아래 코드와 같이 한땀한땀 해시맵에 저장하긴 했는데..더 간단하고 효율적인 방법이 있을 수도 있다.🤔
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);
HashMap<Integer, Integer> score = new HashMap<>();
score.put(1, 3);
score.put(2, 2);
score.put(3, 1);
score.put(4, 0);
score.put(5, 1);
score.put(6, 2);
score.put(7, 3);
이렇게 저장한 후 해시맵을 모두 출력해보면 다음과 같이 나온다.(출력 부분)
그리고나서 survey 배열을 돌면서 choices[i]의 값에 따라 map에 value를 저장한다. 1부터 3까지는 비동의 관련 선택, 5부터 7까지는 동의 관련 선택이다. 비동의 관련 선택일 경우 survey[i]의 첫 번째 캐릭터가 성격 유형으로 주어지고, 해당 value에 choices[i]의 값에 따른 점수를 더한다. 동의 관련 선택일 경우 survey[i]의 두 번째 캐릭터가 성격 유형으로 주어지며 나머지 과정은 똑같다.
for (int i = 0; i < survey.length; i++) {
if (choices[i] == 1 || choices[i] == 2 || choices[i] == 3) {
map.put(survey[i].charAt(0), map.get(survey[i].charAt(0)) + score.get(choices[i]));
} else if (choices[i] == 5 || choices[i] == 6 || choices[i] == 7) {
map.put(survey[i].charAt(1), map.get(survey[i].charAt(1)) + score.get(choices[i]));
}
}
이 부분은 다른 사람들의 코드를 보니 아래 코드와 같이 4를 더하고 빼는 과정을 통해 score라는 해시맵 없이 간단하게 짤 수도 있었다. 물론 choices[i]의 범위도 4 이상/ 이하로 더 깔끔하게 적으면 된다.(왜 나는 저렇게 하나하나 적었는가...)
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);
}
}
이렇게 survey 배열을 모두 돌고난 후 해시맵을 출력해보면 다음과 각 성격 유형마다 얼만큼의 점수를 얻었는지 나온다.
이제 이렇게 나온 해시맵을 가지고 지표별로 값을 판단해 StringBuilder에 추가해주면 끝! 여기서는 삼항연산자를 이용해 나온 값을 바로 append()할 수 있도록 짰다.
StringBuilder answer = new StringBuilder();
answer.append(map.get('R') >= map.get('T') ? 'R' : 'T');
answer.append(map.get('C') >= map.get('F') ? 'C' : 'F');
answer.append(map.get('J') >= map.get('M') ? 'J' : 'M');
answer.append(map.get('A') >= map.get('N') ? 'A' : 'N');
📌 전체 코드
import java.util.*;
class Solution {
public String solution(String[] survey, int[] choices) {
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);
HashMap<Integer, Integer> score = new HashMap<>();
score.put(1, 3);
score.put(2, 2);
score.put(3, 1);
score.put(4, 0);
score.put(5, 1);
score.put(6, 2);
score.put(7, 3);
for (int i = 0; i < survey.length; i++) {
if (choices[i] == 1 || choices[i] == 2 || choices[i] == 3) {
map.put(survey[i].charAt(0), map.get(survey[i].charAt(0)) + score.get(choices[i]));
} else if (choices[i] == 5 || choices[i] == 6 || choices[i] == 7) {
map.put(survey[i].charAt(1), map.get(survey[i].charAt(1)) + score.get(choices[i]));
}
}
StringBuilder answer = new StringBuilder();
answer.append(map.get('R') >= map.get('T') ? 'R' : 'T');
answer.append(map.get('C') >= map.get('F') ? 'C' : 'F');
answer.append(map.get('J') >= map.get('M') ? 'J' : 'M');
answer.append(map.get('A') >= map.get('N') ? 'A' : 'N');
return answer.toString();
}
}
📌 다른 사람들의 코드
성격 유형과 성격 유형 점수를 배열에 저장한 뒤 푼 코드이다. 이렇게도 풀 수 있군! 하고 깨달았다. 그러나 속도는 기존 코드가 더 빠르긴 하다.
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;
}
}
'프로그래머스 > Lv.1' 카테고리의 다른 글
[프로그래머스/Lv.1] 햄버거 만들기 - Java (0) | 2024.04.25 |
---|---|
[프로그래머스/Lv.1] 덧칠하기 - Java (0) | 2024.04.23 |
[프로그래머스/Lv.1] 소수 만들기(feat.에라토스테네스의 체) - Java (1) | 2024.04.22 |
[프로그래머스/Lv.1] 기사단원의 무기 - Java (1) | 2024.04.19 |
[프로그래머스/Lv.1] 신규 아이디 추천(2021 KAKAO BLIND RECRUITMENT) - Java (1) | 2024.04.19 |