Imagine you're building a real-time analytics system that tracks the popularity of different content IDs on a social media platform. As users interact with content throughout the day, IDs get added to or removed from your tracking system, and you need to continuously monitor which content is currently most popular.
You're given two arrays of equal length n:
- nums: An array where each element represents a content ID
- freq: An array where each element indicates how many times the corresponding ID should be added (positive values) or removed (negative values) from your tracking system
At each step i, you update your collection based on nums[i] and freq[i], then need to report the count of the most frequent ID currently in your system.
Example: If nums = [2, 3, 2, 1] and freq = [3, 2, -2, 1]:
- Step 0: Add 3 copies of ID 2 → Collection: {2: 3} → Most frequent count: 3
- Step 1: Add 2 copies of ID 3 → Collection: {2: 3, 3: 2} → Most frequent count: 3
- Step 2: Remove 2 copies of ID 2 → Collection: {2: 1, 3: 2} → Most frequent count: 2
- Step 3: Add 1 copy of ID 1 → Collection: {2: 1, 3: 2, 1: 1} → Most frequent count: 2
Return an array where each element represents the count of the most frequent ID after each step. If the collection becomes empty at any point, return 0 for that step.
Input & Output
Constraints
- 1 ≤ nums.length == freq.length ≤ 105
- 1 ≤ nums[i] ≤ 106
- -105 ≤ freq[i] ≤ 105
- freq[i] ≠ 0
- The number of any ID will not become negative at any point