Most Frequent IDs
The problem involves tracking the frequency of IDs in a collection that changes over time. You have two integer arrays, nums and freq, of equal length n. Each element in nums represents an ID, and the corresponding element in freq indicates how many times that ID should be added to or removed from the collection at each step.
- Addition of IDs: If
freq[i]is positive, it meansfreq[i]IDs with the valuenums[i]are added to the collection at stepi. - Removal of IDs: If
freq[i]is negative, it means-freq[i]IDs with the valuenums[i]are removed from the collection at stepi.
Return an array ans of length n, where ans[i] represents the count of the most frequent ID in the collection after the i^th step. If the collection is empty at any step, ans[i] should be 0 for that step.
Example 1
Input
nums = [2,3,2,1], freq = [3,2,-3,1]Output
[3,3,2,2]After the four steps, the maximum ID counts are 3, 3, 2, and 2 respectively.
Example 2
Input
nums = [5,5,3], freq = [2,-2,1]Output
[2,0,1]After step 1 the collection is empty, so the maximum counts after the three steps are 2, 0, and 1 respectively.
Constraints
- 1 <= nums.length == freq.length <= 10^5
- 1 <= nums[i] <= 10^5
- -10^5 <= freq[i] <= 10^5
- freq[i] != 0
- The input is generated such that the occurrences of an ID will not be negative in any step.