窗口最大值|双端队列

介绍

给定一个一维数组,和一个窗口的大小(所谓窗口即为给定长度的子数组,注意子数组要求连续而不是子集),要求求出每个窗口中的最大值,O(N)时间复杂度

思想

我们可以使用双端队列结构来解决此问题。
双端队列即可以在队头进行插入和出队操作,也可以在队尾进行插入和出队操作。
下面介绍双端队列的维护:
在此题中,我们需要保证整个队列由队头至队尾是由大到小的,因此我们每次加入从队尾插入元素时需要进行比较,将队尾小于当前需要插入的元素,在队尾执行出队操作,直到队尾大于当前需要插入的元素为止,最后将该元素从队尾插入。
注意,我们每次插入的都是下标,当需要进行比较的时候可以通过下标在数组中找到相应的值进行比较。
当窗口左端滑过一个位置的时候,如果这个位置恰好是队头元素指向的位置,则执行队头出栈操作。
当窗口左端滑至某个位置的时候,当前位置所构成的窗口中的最大值为队头的元素。
文字不好描述,描述不清楚的地方欢迎评论

完整代码

双端队列是自己为了练习手动实现的,其实完全可以使用STL的deque

#include <iostream>

using namespace std;

class Node{
public:
    int data;
    Node *father;
    Node *next;
public:
    Node(){
        father = NULL;
        next = NULL;
    }
    void print(){
        cout << data << " ";
        if(father!=NULL)
            cout << "father:" << father->data << " ";
        else
            cout << "father:NULL ";
        if(next!=NULL)
            cout << "next:" << next->data << " ";
        else
            cout << "next:NULL ";
        cout << endl;
    }
    friend class Queue;
};

class Queue{
public:
    Node *first;
    Node *last;
public:
    Queue(){
        first = new Node;
        last = new Node;
    }
    bool isEmpty(){
        return first->next == NULL;
    }
    int peekFirst(){
        return first->next->data;
    }
    int peekLast(){
        return last->next->data;
    }
    void pollFirst(){
        if(this->isEmpty()) return;
        Node *delNode = first->next;
        if(last->next == delNode){
            last->next = NULL;
        }
        first->next = delNode->next;
        delete delNode;
    }
    void pollLast(){
        if(this->isEmpty()) return;
        Node *delNode = last->next;
        if(first->next == delNode){
            first->next = NULL;
            last->next = NULL;
        }else{
            last->next = delNode->father;
            last->next->next = NULL;
        }
        delete delNode;
    }
    void addLast(int data){
        Node *newNode = new Node;
        newNode->data = data;
        if(this->isEmpty()) {
            first->next = newNode;
        }else{
            newNode->father = last->next;
            newNode->father->next = newNode;
        }
        last->next = newNode;
        newNode->next = NULL;
    }
    void output(){
        if(this->isEmpty()) return;
        Node *p =first->next;
        cout << "-------" << endl;
        while(p!=NULL){
            p->print();
            p = p->next;
        }cout << "-------" << endl;
    }
};

int *getMaxWindow(int arr[], int size, int width){
    Queue Q;
    int index = 0;
    int *res = new int[size - width + 1];
    for(int i = 0; i < size; i++){
        while(!Q.isEmpty() && arr[Q.peekLast()] <= arr[i]){
            Q.pollLast();
        }
        Q.addLast(i);
        if(Q.peekFirst() == i - width){
            Q.pollFirst();
        }
        if(i >= width-1){
            res[index++] = arr[Q.peekFirst()];
        }
    }
    return res;
}

int main()
{
    int n, w;
    cin >> n >> w;
    int *arr = new int[n];
    for(int i = 0; i < n; i++) cin >> arr[i];
    int *res = getMaxWindow(arr, n, w);
    for(int i = 0; i < n-w+1; i++) cout << res[i] << " ";
    cout << endl;
    return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值