CodeWars 열 아홉 번째 문제
Updated:
Greed is Good
public static int greedy(int[] dice){
int score = 0;
HashMap<Integer,Integer> diceMap = new HashMap<Integer,Integer>();
for(int number : dice) {
if(!diceMap.containsKey(number)) {
diceMap.put(number, 1);
} else {
diceMap.put(number, diceMap.get(number)+1);
}
}
for(Integer key : diceMap.keySet()) {
int value = diceMap.get(key);
if(key == 1) {
if(value >= 3) {
score += 1000;
}
score += (value%3)*100;
} else {
if(value >= 3) {
score += key*100;
}
if(key == 5) {
score += (value%3)*50;
}
}
}
return score;
}
*나는 hashmap에 담아서 점수별로 몇 개가 있는지 확인한 다음 score를 계산하려고 했다.
그러나 아래 처럼 점수가 정해져 있기 때문에 이를 배열의 index로 활용하여 각각의 점수에 맞게 계산하면 쉽게 해결 되었다… ㅠㅠ 나의 머리는 여기까지인가 보다.
int n[] = new int[7];
for (int d : dice) {
n[d]++;
}
return n[1]/3*1000 + n[1]%3*100 + n[2]/3*200 + n[3]/3*300 + n[4]/3*400 + n[5]/3*500 + n[5]%3*50 + n[6]/3*600;