3092. Most Frequent IDs Medium

@problem@discussion
#Array#Hash Table#Heap (Priority Queue)#Ordered Set



1/**
2 * [3092] Most Frequent IDs
3 *
4 * 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.
5 * 
6 * 	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.
7 * 	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.
8 * 
9 * 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.
10 *  
11 * <strong class="example">Example 1:
12 * <div class="example-block">
13 * Input: <span class="example-io">nums = [2,3,2,1], freq = [3,2,-3,1]</span>
14 * Output: <span class="example-io">[3,3,2,2]</span>
15 * Explanation:
16 * After step 0, we have 3 IDs with the value of 2. So ans[0] = 3.<br />
17 * After step 1, we have 3 IDs with the value of 2 and 2 IDs with the value of 3. So ans[1] = 3.<br />
18 * After step 2, we have 2 IDs with the value of 3. So ans[2] = 2.<br />
19 * After step 3, we have 2 IDs with the value of 3 and 1 ID with the value of 1. So ans[3] = 2.
20 * </div>
21 * <strong class="example">Example 2:
22 * <div class="example-block">
23 * Input: <span class="example-io">nums = [5,5,3], freq = [2,-2,1]</span>
24 * Output: <span class="example-io">[2,0,1]</span>
25 * Explanation:
26 * After step 0, we have 2 IDs with the value of 5. So ans[0] = 2.<br />
27 * After step 1, there are no IDs. So ans[1] = 0.<br />
28 * After step 2, we have 1 ID with the value of 3. So ans[2] = 1.
29 * </div>
30 *  
31 * Constraints:
32 * 
33 * 	1 <= nums.length == freq.length <= 10^5
34 * 	1 <= nums[i] <= 10^5
35 * 	-10^5 <= freq[i] <= 10^5
36 * 	freq[i] != 0
37 * 	The input is generated<!-- notionvc: a136b55a-f319-4fa6-9247-11be9f3b1db8 --> such that the occurrences of an ID will not be negative in any step.
38 * 
39 */
40pub struct Solution {}
41
42// problem: https://leetcode.com/problems/most-frequent-ids/
43// discuss: https://leetcode.com/problems/most-frequent-ids/discuss/?currentPage=1&orderBy=most_votes&query=
44
45// submission codes start here
46
47impl Solution {
48    pub fn most_frequent_i_ds(nums: Vec<i32>, freq: Vec<i32>) -> Vec<i64> {
49        
50    }
51}
52
53// submission codes end
54
55#[cfg(test)]
56mod tests {
57    use super::*;
58
59    #[test]
60    fn test_3092() {
61    }
62}
63


Back
© 2025 bowen.ge All Rights Reserved.