๋ฌธ์ : Lv.1 ์ฑ๊ฒฉ ์ ํ ๊ฒ์ฌํ๊ธฐ https://school.programmers.co.kr/learn/courses/30/lessons/118666 ์ธ์ด : JAVA ์ฒด๊ฐ Level : โ โ โ Review: map ์ด๋ผ๋ ์๋ฃ๊ตฌ์กฐ๋ฅผ ์ ํ์ฉํ๋ฉด ์ฝ๊ฒ ํ ์ ์๋ ๋ฌธ์ |
๐ก map ์๋ ๊ณ ์ ๋์ด์๋ ์ฑ๊ฒฉ์ ํ 8๊ฐ์ง๋ฅผ ๋ฃ์ด์ฃผ๊ณ
map2 ์์๋ ์ ํ์ง๋ณ ์นด์ดํ ๊ฐ์ ๋ฃ์ด์ค๋ค
์ด๋ map ์ ์์๋๋ก ๋ฃ์ด๋ ์์ ๋ณด์ฅ์ด ์๋๋ค
TreeMap ์ ์ฌ์ฉํ๋ฉด ์ ๋ ฌ์ ๋์ง๋ง ์ฑ๊ฒฉ ์ ํ์ ๋์นญ์ ๋ง๊ฒ ๋น๊ตํด์ผํ๊ธฐ๋๋ฌธ์ ๊ตณ์ด ?
๊ทธ๋์ list ์ ๋์นญ๋๋ ์ฑ๊ฒฉ์ ํ์ ๋ฃ์ด์ ๋น๊ตํด์คฌ๋ค.
import java.util.*;
class Solution {
public String solution(String[] survey, int[] choices) {
String answer = "";
Map<Character, Integer> map = new HashMap<>();
map.put('R',0); map.put('C',0); map.put('J',0); map.put('A',0);
map.put('T',0); map.put('F',0); map.put('M',0); map.put('N',0);
Map<Integer, Integer> map2 = new HashMap<>();
map2.put(1,-3); map2.put(2,-2); map2.put(3,-1);
map2.put(4,0);
map2.put(5,1); map2.put(6,2); map2.put(7,3);
int i = 0;
int hap = 0;
for(int c : choices){
if(map2.get(c) < 0){
hap = map.get(survey[i].charAt(0)) + (map2.get(c) * (-1));
map.put(survey[i].charAt(0), hap);
}else{
hap = map.get(survey[i].charAt(1)) + map2.get(c);
map.put(survey[i].charAt(1), hap);
}
i++;
hap = 0;
}
String[] list = {"RT", "CF", "JM", "AN"};
for (int j = 0; j < list.length; j++){
if (map.get(list[j].charAt(0)) > map.get(list[j].charAt(1))){
answer += list[j].charAt(0);
}else if (map.get(list[j].charAt(0)) < map.get(list[j].charAt(1))){
answer += list[j].charAt(1);
}else {
if ((int)list[j].charAt(0) > (int)list[j].charAt(1)){
answer += list[j].charAt(1);
}else {
answer += list[j].charAt(0);
}
}
}
return answer;
}
}