3092. Most Frequent IDs
Description
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 ith
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]
Explanation:
After step 0, we have 3 IDs with the value of 2. So ans[0] = 3
.
After step 1, we have 3 IDs with the value of 2 and 2 IDs with the value of 3. So ans[1] = 3
.
After step 2, we have 2 IDs with the value of 3. So ans[2] = 2
.
After step 3, we have 2 IDs with the value of 3 and 1 ID with the value of 1. So ans[3] = 2
.
Example 2:
Input: nums = [5,5,3], freq = [2,-2,1]
Output: [2,0,1]
Explanation:
After step 0, we have 2 IDs with the value of 5. So ans[0] = 2
.
After step 1, there are no IDs. So ans[1] = 0
.
After step 2, we have 1 ID with the value of 3. So ans[2] = 1
.
Constraints:
1 <= nums.length == freq.length <= 105
1 <= nums[i] <= 105
-105 <= freq[i] <= 105
freq[i] != 0
- The input is generated such that the occurrences of an ID will not be negative in any step.
Solutions
Solution 1: Hash Table + Priority Queue (Max Heap)
We use a hash table $cnt$ to record the occurrence times of each ID, a hash table $lazy$ to record the number of times each occurrence needs to be deleted, and a priority queue $pq$ to maintain the maximum occurrence times.
For each operation $(x, f)$, we need to update the occurrence times $cnt[x]$ of $x$, which means the value of $cnt[x]$ in $lazy$ needs to increase by $1$, indicating that the number of times this occurrence needs to be deleted increases by $1$. Then we update the value of $cnt[x]$, adding $f$ to $cnt[x]$. Then we add the updated value of $cnt[x]$ to the priority queue $pq$. Then we check the top element of the priority queue $pq$. If the number of times the corresponding occurrence needs to be deleted in $lazy$ is greater than $0$, we pop the top element. Finally, we judge whether the priority queue is empty. If it is not empty, the top element is the maximum occurrence times, and we add it to the answer array.
The time complexity is $O(n \times \log n)$, and the space complexity is $O(n)$. Where $n$ is the length of the array $nums$.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
|