-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sync LeetCode submission Runtime - 210 ms (43.21%), Memory - 17.5 MB …
…(74.07%)
- Loading branch information
1 parent
87ebf5c
commit 09731e9
Showing
2 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<p>The problem involves tracking the frequency of IDs in a collection that changes over time. You have two integer arrays, <code>nums</code> and <code>freq</code>, of equal length <code>n</code>. Each element in <code>nums</code> represents an ID, and the corresponding element in <code>freq</code> indicates how many times that ID should be added to or removed from the collection at each step.</p> | ||
|
||
<ul> | ||
<li><strong>Addition of IDs:</strong> If <code>freq[i]</code> is positive, it means <code>freq[i]</code> IDs with the value <code>nums[i]</code> are added to the collection at step <code>i</code>.</li> | ||
<li><strong>Removal of IDs:</strong> If <code>freq[i]</code> is negative, it means <code>-freq[i]</code> IDs with the value <code>nums[i]</code> are removed from the collection at step <code>i</code>.</li> | ||
</ul> | ||
|
||
<p>Return an array <code>ans</code> of length <code>n</code>, where <code>ans[i]</code> represents the <strong>count</strong> of the <em>most frequent ID</em> in the collection after the <code>i<sup>th</sup></code> step. If the collection is empty at any step, <code>ans[i]</code> should be 0 for that step.</p> | ||
|
||
<p> </p> | ||
<p><strong class="example">Example 1:</strong></p> | ||
|
||
<div class="example-block"> | ||
<p><strong>Input:</strong> <span class="example-io">nums = [2,3,2,1], freq = [3,2,-3,1]</span></p> | ||
|
||
<p><strong>Output:</strong> <span class="example-io">[3,3,2,2]</span></p> | ||
|
||
<p><strong>Explanation:</strong></p> | ||
|
||
<p>After step 0, we have 3 IDs with the value of 2. So <code>ans[0] = 3</code>.<br /> | ||
After step 1, we have 3 IDs with the value of 2 and 2 IDs with the value of 3. So <code>ans[1] = 3</code>.<br /> | ||
After step 2, we have 2 IDs with the value of 3. So <code>ans[2] = 2</code>.<br /> | ||
After step 3, we have 2 IDs with the value of 3 and 1 ID with the value of 1. So <code>ans[3] = 2</code>.</p> | ||
</div> | ||
|
||
<p><strong class="example">Example 2:</strong></p> | ||
|
||
<div class="example-block"> | ||
<p><strong>Input:</strong> <span class="example-io">nums = [5,5,3], freq = [2,-2,1]</span></p> | ||
|
||
<p><strong>Output:</strong> <span class="example-io">[2,0,1]</span></p> | ||
|
||
<p><strong>Explanation:</strong></p> | ||
|
||
<p>After step 0, we have 2 IDs with the value of 5. So <code>ans[0] = 2</code>.<br /> | ||
After step 1, there are no IDs. So <code>ans[1] = 0</code>.<br /> | ||
After step 2, we have 1 ID with the value of 3. So <code>ans[2] = 1</code>.</p> | ||
</div> | ||
|
||
<p> </p> | ||
<p><strong>Constraints:</strong></p> | ||
|
||
<ul> | ||
<li><code>1 <= nums.length == freq.length <= 10<sup>5</sup></code></li> | ||
<li><code>1 <= nums[i] <= 10<sup>5</sup></code></li> | ||
<li><code>-10<sup>5</sup> <= freq[i] <= 10<sup>5</sup></code></li> | ||
<li><code>freq[i] != 0</code></li> | ||
<li>The input is generated<!-- notionvc: a136b55a-f319-4fa6-9247-11be9f3b1db8 --> such that the occurrences of an ID will not be negative in any step.</li> | ||
</ul> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
func mostFrequentIDs(nums []int, freq []int) []int64 { | ||
curValues := map[int] int64 {} // value : frequency | ||
h := &KeyHeap{} | ||
heap.Init(h) | ||
res := make([]int64, len(nums)) | ||
|
||
for i := 0; i < len(nums); i++ { | ||
curValues[nums[i]] += int64(freq[i]) | ||
heap.Push(h, key{value: nums[i], freq: curValues[nums[i]]}) | ||
|
||
for curValues[(*h)[0].value] != (*h)[0].freq { | ||
heap.Pop(h) | ||
} | ||
|
||
res[i] = (*h)[0].freq | ||
} | ||
|
||
return res | ||
} | ||
|
||
type key struct { | ||
value int | ||
freq int64 | ||
} | ||
|
||
type KeyHeap []key | ||
|
||
func (h KeyHeap) Len() int { return len(h) } | ||
func (h KeyHeap) Less(i, j int) bool { return h[i].freq > h[j].freq } | ||
func (h KeyHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] } | ||
|
||
func (h *KeyHeap) Push(x any) { | ||
*h = append(*h, x.(key)) | ||
} | ||
|
||
func (h *KeyHeap) Pop() any { | ||
old := *h | ||
n := len(old) | ||
x := old[n-1] | ||
*h = old[0 : n-1] | ||
return x | ||
} |