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 means freq[i] IDs with the value nums[i] are added to the collection at step i.
  • Removal of IDs: If freq[i] is negative, it means -freq[i] IDs with the value nums[i] are removed from the collection at step i.

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
Inputnums = [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
Inputnums = [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.

Asked at 2 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate