贴下我的解法:
import java.util.*;
public class SmartSale {
public static void main(String[] args) {
int[] ids = {1, 1, 1, 2, 2, 3};
int m = 2;
System.out.println(deleteProducts(m, ids));
}
static int deleteProducts(int m, int[] ids) {
HashMap<Integer, Integer> map = new HashMap<>();
for (int id : ids) {
map.put(id, map.getOrDefault(id, 0) + 1);
}
PriorityQueue<Integer> heap = new PriorityQueue<>(map.values());
while (true) {
if (m - heap.peek() > 0) {
m -= heap.poll();
} else {
return heap.size();
}
}
}
}