https://school.programmers.co.kr/learn/courses/30/lessons/138476#
프로그래머스
SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프
programmers.co.kr
1. map 자료구조를 이용해서, 수확한 귤의 종류별 개수를 구한다
2. 제거해야 하는 개수만큼 제거한다
단, 종류별 개수를 오름차순 정렬하여 제거함으로써 최대한 많은 종류를 없애도록 한다
import java.util.*;
class Solution {
static Map<Integer, Integer> map;
public int solution(int k, int[] tangerine) { // k 고를 귤의 수
int answer = 0;
map = new HashMap<>();
for(int t = 0; t < tangerine.length; t++) {
int x = tangerine[t];
map.put(x, map.getOrDefault(x, 0) + 1);
}
List<Integer> values = new ArrayList<>(map.values());
Collections.sort(values);
int all = 0; // 모든 귤의 수
for(int i = 0; i < values.size(); i++) {
all += values.get(i);
}
int needRemoved = all - k;
int removedIdx = -1;
int removed = 0;
for(int removeI = 0; removeI < values.size(); removeI++) {
if (removed + values.get(removeI) <= needRemoved){
removed += values.get(removeI);
removedIdx = removeI;
continue;
} else {
break;
}
}
answer = values.size() - (removedIdx + 1);
return answer;
}
}
[프로그래머스] 괄호 회전하기 (0) | 2024.12.10 |
---|---|
[프로그래머스] 예상 대진표 (0) | 2024.12.09 |
[프로그래머스] 영어 끝말잇기 (0) | 2024.12.09 |
[프로그래머스] 점프와 순간 이동 (0) | 2024.12.09 |
[BJ] 1956 : 운동 (0) | 2024.12.03 |