💻 문제 출처 : 프로그래머스_귤 고르기
import java.util.*;
class Solution {
public int solution(int k, int[] tangerine) {
int total = 0;
int answer = 0;
int tangerineLength = tangerine.length;
Arrays.sort(tangerine);
int[] typeIndex = new int[tangerine[tangerineLength - 1] + 1];
for(int i : tangerine) {
typeIndex[i]++;
}
Arrays.sort(typeIndex);
for(int i = typeIndex.length - 1; i >= 0; i--) {
answer++;
total += typeIndex[i];
if(total >= k) {
return answer;
}
}
return answer;
}
}
import java.util.*;
class Solution {
public int solution(int k, int[] tangerine) {
int answer = 0;
HashMap<Integer,Integer> map =new HashMap<>();
for (int t : tangerine) {
map.put(t, map.getOrDefault(t, 0) + 1);
}
List<Integer> list = new ArrayList<>(map.keySet());
list.sort((o1, o2) -> map.get(o2)-map.get(o1));
for(Integer key:list){
k -=map.get(key);
answer++;
if(k<=0){
break;
}
}
return answer;
}
}