File tree Expand file tree Collapse file tree 1 file changed +42
-0
lines changed Expand file tree Collapse file tree 1 file changed +42
-0
lines changed Original file line number Diff line number Diff line change 1+ //  Time:  O(nlogn)
2+ //  Space: O(n)
3+ 
4+ //  heap
5+ class  Solution  {
6+ public: 
7+     vector<long  long > mostFrequentIDs (vector<int >& nums, vector<int >& freq) {
8+         vector<long  long > result;
9+         unordered_map<int , int64_t > cnt;
10+         priority_queue<pair<int64_t , int >> max_heap;
11+         for  (int  i = 0 ; i < size (nums); ++i) {
12+             cnt[nums[i]] += freq[i];
13+             max_heap.emplace (cnt[nums[i]], nums[i]);
14+             while  (!empty (max_heap) && max_heap.top ().first  != cnt[max_heap.top ().second ]) {
15+                 max_heap.pop ();
16+             }
17+             result.emplace_back (!empty (max_heap) ? max_heap.top ().first  : 0 );
18+         }
19+         return  result;
20+     }
21+ };
22+ 
23+ //  Time:  O(nlogn)
24+ //  Space: O(n)
25+ //  bst
26+ class  Solution2  {
27+ public: 
28+     vector<long  long > mostFrequentIDs (vector<int >& nums, vector<int >& freq) {
29+         vector<long  long > result;
30+         unordered_map<int , int64_t > cnt;
31+         map<int64_t , int > bst;
32+         for  (int  i = 0 ; i < size (nums); ++i) {
33+             if  (--bst[cnt[nums[i]]] == 0 ) {
34+                 bst.erase (cnt[nums[i]]);
35+             }
36+             cnt[nums[i]] += freq[i];
37+             ++bst[cnt[nums[i]]];
38+             result.emplace_back (rbegin (bst)->first );
39+         }
40+         return  result;
41+     }
42+ };
    
 
   
 
     
   
   
          
     
  
    
     
 
    
      
     
 
     
    You can’t perform that action at this time.
  
 
    
  
     
    
      
        
     
 
       
      
     
   
 
    
    
  
 
  
 
     
    
0 commit comments