Class Solution
-
- All Implemented Interfaces:
public final class Solution3092 - Most Frequent IDs.
Medium
The problem involves tracking the frequency of IDs in a collection that changes over time. You have two integer arrays,
numsandfreq, of equal lengthn. Each element innumsrepresents an ID, and the corresponding element infreqindicates 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
ansof lengthn, whereans[i]represents the count of the most frequent ID in the collection after the <code>i<sup>th</sup></code> 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. Soans[1] = 3. After step 2, we have 2 IDs with the value of 3. Soans[2] = 2. After step 3, we have 2 IDs with the value of 3 and 1 ID with the value of 1. Soans[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. Soans[1] = 0. After step 2, we have 1 ID with the value of 3. Soans[2] = 1.Constraints:
<code>1 <= nums.length == freq.length <= 10<sup>5</sup></code>
<code>1 <= numsi<= 10<sup>5</sup></code>
<code>-10<sup>5</sup><= freqi<= 10<sup>5</sup></code>
freq[i] != 0The input is generated such that the occurrences of an ID will not be negative in any step.
-
-
Constructor Summary
Constructors Constructor Description Solution()
-
Method Summary
Modifier and Type Method Description final LongArraymostFrequentIDs(IntArray nums, IntArray freq)-
-
Method Detail
-
mostFrequentIDs
final LongArray mostFrequentIDs(IntArray nums, IntArray freq)
-
-
-
-