Consider the following Java code snippet that finds the top 2 frequent elements in an array using a min-heap.
import java.util.*;
public class Solution {
public static List topKFrequent(int[] nums, int k) {
Map freqMap = new HashMap<>();
for (int num : nums) {
freqMap.put(num, freqMap.getOrDefault(num, 0) + 1);
}
PriorityQueue heap = new PriorityQueue<>((a, b) -> freqMap.get(a) - freqMap.get(b));
for (int num : freqMap.keySet()) {
heap.offer(num);
if (heap.size() > k) {
heap.poll();
}
}
List result = new ArrayList<>();
while (!heap.isEmpty()) {
result.add(heap.poll());
}
Collections.reverse(result);
return result;
}
public static void main(String[] args) {
int[] nums = {1,1,1,2,2,3};
System.out.println(topKFrequent(nums, 2));
}
} import java.util.*; public class Solution { public static List<Integer> topKFrequent(int[] nums, int k) { Map<Integer, Integer> freqMap = new HashMap<>(); for (int num : nums) { freqMap.put(num, freqMap.getOrDefault(num, 0) + 1); } PriorityQueue<Integer> heap = new PriorityQueue<>((a, b) -> freqMap.get(a) - freqMap.get(b)); for (int num : freqMap.keySet()) { heap.offer(num); if (heap.size() > k) { heap.poll(); } } List<Integer> result = new ArrayList<>(); while (!heap.isEmpty()) { result.add(heap.poll()); } Collections.reverse(result); return result; } public static void main(String[] args) { int[] nums = {1,1,1,2,2,3}; System.out.println(topKFrequent(nums, 2)); } }
Remember the min-heap keeps the least frequent element at the top, so after processing, the heap contains the top K frequent elements in ascending order of frequency.
The frequency map is {1=3, 2=2, 3=1}. The min-heap keeps the top 2 frequent elements. After adding all, the heap contains 1 and 2. Polling and reversing gives [1, 2].
In the top K frequent elements problem, why is a min-heap preferred over a max-heap when maintaining the heap size to K?
Think about which element you want to remove when the heap size grows beyond K.
We use a min-heap to keep the smallest frequency element at the top. When the heap size exceeds K, we remove the least frequent element, ensuring the heap contains the top K frequent elements.
Identify the error in the following code snippet:
import java.util.*;
public class Solution {
public static List topKFrequent(int[] nums, int k) {
Map freqMap = new HashMap<>();
for (int num : nums) {
freqMap.put(num, freqMap.getOrDefault(num, 0) + 1);
}
PriorityQueue heap = new PriorityQueue<>((a, b) -> freqMap.get(b) - freqMap.get(a));
for (int num : freqMap.keySet()) {
heap.offer(num);
if (heap.size() > k) {
heap.poll();
}
}
List result = new ArrayList<>();
while (!heap.isEmpty()) {
result.add(heap.poll());
}
Collections.reverse(result);
return result;
}
public static void main(String[] args) {
int[] nums = {1,1,1,2,2,3};
System.out.println(topKFrequent(nums, 2));
}
} import java.util.*; public class Solution { public static List<Integer> topKFrequent(int[] nums, int k) { Map<Integer, Integer> freqMap = new HashMap<>(); for (int num : nums) { freqMap.put(num, freqMap.getOrDefault(num, 0) + 1); } PriorityQueue<Integer> heap = new PriorityQueue<>((a, b) -> freqMap.get(b) - freqMap.get(a)); for (int num : freqMap.keySet()) { heap.offer(num); if (heap.size() > k) { heap.poll(); } } List<Integer> result = new ArrayList<>(); while (!heap.isEmpty()) { result.add(heap.poll()); } Collections.reverse(result); return result; } public static void main(String[] args) { int[] nums = {1,1,1,2,2,3}; System.out.println(topKFrequent(nums, 2)); } }
Determine if the comparator creates a min-heap or max-heap and simulate the heap operations.
The comparator (a, b) -> freqMap.get(b) - freqMap.get(a) creates a max-heap where the element with the highest frequency is at the top (pops first). When size exceeds k=2 after adding 3, it polls the most frequent (1, freq=3), leaving 2 and 3. Polling adds 2 then 3 to result, reversing gives [3, 2].
Fix the syntax error in this comparator used in a PriorityQueue:
PriorityQueueheap = new PriorityQueue<>((a, b) -> freqMap.get(a) - freqMap.get(b))
Check Java syntax for lambda expressions and statements.
The only syntax error is missing semicolon at the end of the statement. The arrow syntax is correct.
Given the input array nums = {4,4,4,1,1,2,2,3} and k = 3, how many elements remain in the min-heap after processing all frequencies?
Map<Integer, Integer> freqMap = new HashMap<>(); for (int num : nums) { freqMap.put(num, freqMap.getOrDefault(num, 0) + 1); } PriorityQueue<Integer> heap = new PriorityQueue<>((a, b) -> freqMap.get(a) - freqMap.get(b)); for (int num : freqMap.keySet()) { heap.offer(num); if (heap.size() > k) { heap.poll(); } }
The heap size is limited to k by polling when size exceeds k.
The frequency map has 4 keys: 4,1,2,3. The heap size is limited to k=3, so after processing all keys, the heap size is 3.
