Classic topic: the maximum value of the sliding window + the first K high-frequency elements

1. The maximum value of the sliding window:
classic algorithm: depth-first queue (deque)

class myqueue{
    public:
        deque<int> que;
        void pop(int value){
        if(!que.empty() && value == que.front()){      //由于队列只维护单调递减的序列,因此在第一个元素之前可能还有元素不过被弹出了,导致目前元素数目小于窗口大小,因此只有当value == que.front()时才弹出,否则不操作;
            que.pop_front();
            
        }
    }
    void push(int value){
        while(!que.empty()&&value>que.back()){
            que.pop_back();//如果要压入的元素比队列末尾的大,说明此时不是单调减的,从末尾弹出直到队列为单调递减,这样可以保证front的元素为最大。
            
        }
        que.push_back(value);
    }
    int front(){
        return que.front();
    }
};


class Solution {
public:
   
    
    vector<int> maxInWindows(const vector<int>& num, unsigned int size) {
         myqueue que;
        int max = INT_MIN;
        vector<int> result;
        if(size==0 || size>num.size()) {return result;}
      
        for(int i = 0;i<size;i++){
            que.push(num[i]);
        }
       
        result.push_back(que.front());
        for(int i = size; i<num.size();i++){
            que.pop(num[i-size]);//先弹出再压
            que.push(num[i]);
            result.push_back(que.front());
        }
       return result;
    }
};

2. Top K high-frequency elements:

//升序队列
priority_queue <int,vector<int>,greater<int> > q;
//降序队列
priority_queue <int,vector<int>,less<int> >q;

Find the first k high-frequency elements:
1: Count the frequency of elements
2: Sort the frequency
3: Find the top K high-frequency elements
Idea: use priority queue, big top heap (heap head is the largest element), small Top heap (heap head is the smallest element), why not use quick sort, use quick sort to convert the map into a vector structure, and then sort the entire array, and in this scenario, we only need to maintain k orderly The sequence of is fine, so using a priority queue is optimal. We want to use the small top heap, because to count the largest top k elements, only the small top heap pops the smallest element every time, and finally the top k largest elements are accumulated in the small top heap. If the queue size is greater than k, then directly pop up;

Note: The three parameters of the priority queue: among them, Container must be
the definition of vector construction: priority_queue<Type, Container, Functional>
will create a small top heap when the left is greater than the right. When using basic data types, only the data type needs to be passed in. The default is the big top heap

class mycompare{
    public:
        bool operator()(const pair<int, int> &left, const pair<int, int> &right){
          return left.second > right.second; }

};
class Solution {
public:
    
    vector<int> topKFrequent(vector<int>& nums, int k) {
        unordered_map<int, int> map;
        for(int i = 0;i<nums.size();i++){
            map[nums[i]]++;
        }
        priority_queue<pair<int, int>, vector<pair<int, int>>, mycompare> pri_que;
        for(unordered_map<int, int>::iterator it=map.begin();it != map.end();it++){
            pri_que.push(*it);
            if(pri_que.size()>k) pri_que.pop();
        }
    
    vector<int> result(k);
    for(int i = k-1;i>=0;i--){
        result[i] = pri_que.top().first;
         pri_que.pop();
    }
return result;

    }
};

3. Use priority queue sorting to output the first k small elements;
build a large top heap, pop up the largest element every time, and the rest are small;

class Solution {
public:
    vector<int> GetLeastNumbers_Solution(vector<int> input, int k) {
        if(k==0||k>input.size()) return {};
        
        priority_queue<int, vector<int>> pri_que;
        for(const int val:input){
            if(pri_que.size()<k) pri_que.push(val);
            else{
                if(val<pri_que.top()) {
                    pri_que.pop();
                    pri_que.push(val);
                }
            }
        }
        vector<int> res(k);
        for(int i=k-1;i>=0;i--){
            res[i] = pri_que.top();
            pri_que.pop();
        }
        return res;
    }
};

おすすめ

転載: blog.csdn.net/PETERPARKERRR/article/details/122825132