LeetCode 3369: Design an Array Statistics Tracker Solution
Master LeetCode problem 3369 (Design an Array Statistics Tracker ), a hard challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.
3369. Design an Array Statistics Tracker
Problem Explanation
Explanation:
To design an Array Statistics Tracker, we can use a combination of data structures to efficiently keep track of various statistics of an array. We can use a hashmap to store the frequency of each element in the array and a min-heap and max-heap to maintain the median of the array.
Here are the steps:
- Create a hashmap to store the frequency of each element in the array.
- Create a max-heap to store the smaller half of the array elements.
- Create a min-heap to store the larger half of the array elements.
- Maintain the heaps such that the size difference between the two heaps is at most 1, to efficiently calculate the median.
- Implement methods to add elements to the array, remove elements from the array, and calculate the median of the array.
Time complexity:
- Adding an element: O(log n) for updating heaps and hashmap.
- Removing an element: O(log n) for updating heaps and hashmap.
- Calculating median: O(1).
Space complexity: O(n) for storing the elements in the heaps and hashmap.
:
Solution Code
import java.util.*;
class ArrayStatisticsTracker {
Map<Integer, Integer> freqMap;
PriorityQueue<Integer> maxHeap;
PriorityQueue<Integer> minHeap;
public ArrayStatisticsTracker() {
freqMap = new HashMap<>();
maxHeap = new PriorityQueue<>(Collections.reverseOrder());
minHeap = new PriorityQueue<>();
}
public void addNum(int num) {
freqMap.put(num, freqMap.getOrDefault(num, 0) + 1);
maxHeap.offer(num);
minHeap.offer(maxHeap.poll());
if (maxHeap.size() < minHeap.size()) {
maxHeap.offer(minHeap.poll());
}
}
public void removeNum(int num) {
if (!freqMap.containsKey(num)) {
return;
}
freqMap.put(num, freqMap.get(num) - 1);
while (!maxHeap.isEmpty() && freqMap.getOrDefault(maxHeap.peek(), 0) == 0) {
maxHeap.poll();
}
while (!minHeap.isEmpty() && freqMap.getOrDefault(minHeap.peek(), 0) == 0) {
minHeap.poll();
}
}
public double findMedian() {
if (maxHeap.size() == minHeap.size()) {
return (double) (maxHeap.peek() + minHeap.peek()) / 2;
} else {
return (double) maxHeap.peek();
}
}
}Try It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 3369 (Design an Array Statistics Tracker )?
This page provides optimized solutions for LeetCode problem 3369 (Design an Array Statistics Tracker ) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.
What is the time complexity of LeetCode 3369 (Design an Array Statistics Tracker )?
The time complexity for LeetCode 3369 (Design an Array Statistics Tracker ) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 3369 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 3369 in Java, C++, or Python.