Leetcode -. 703 data stream major elements of K

Here Insert Picture Description
Small root stack ideas:
Firstly rootlets stack formed by a maximum of k elements, when compared to add an element if the element rain top of the stack element, directly returns top of the stack element, if greater than or equal top of the stack elements, delete top of the stack node and add elements into the stack. Insertion and deletion time complexity are O (logn).
Stack building process is complex, we must first consider the relationship between the initial and nums array size k, if k is larger than the stack built directly subsequent need only compare like, is less than k (k-1), will have for the first time add time to build the first reactor.
Remove top of the stack of elements: the top of the stack element value order to stack the last element values (because the heap with the array representation (array subscript start 1), the stack tail is the subscript k, top of the stack is the subscript 1), then Statute from top to bottom. Statute is also very simple, the left and right son (relationship with the parent node subscript n is 2n and 2n + 1) than the size, if it exceeds son, then the switch, until the change to the stack bottom.
Additive element: add the elements directly to the stack so that the tail, then the statute from the bottom up, i.e. the parent node of the comparison (child node j, compared to a parent node j / 2), if the child node is a parent node is less than the exchange, to change until top of the heap.
Here Insert Picture Description

With code

#include <iostream>
#include <vector>
using namespace std;

class KthLargest
{
public:
    int num;
    int countnum=0;
    vector<int> minHeap;
    KthLargest(int k, vector<int>& nums)
    {
        num=k;
        minHeap.resize(k+1);
        minHeap[0]=-1;
        int guodu;
        int j;  //j用来插入节点时维护堆
        //如果初始化的数组大小小于要求的k项
        if(nums.size()<=k)
        {
            for(int i=0;i<nums.size();i++)
            {
                j=i+1;
                minHeap[j]=nums[i];
                while (j/2 > 0 && minHeap[j] < minHeap[j/2])
                {
                    // 自下往上堆化
                    guodu=minHeap[j/2];
                    minHeap[j/2]=minHeap[j];
                    minHeap[j]=guodu;
                    j = j/2;
                }
                countnum++;
            }
        }
        //如果初始化的数组大小大于要求的k项
        else
        {
            countnum=k;
            //先建立一个k个元素的小根堆(用最大的k个元素)
            for(int i=0;i<k;i++)
            {
                j=i+1;
                minHeap[j]=nums[i];
                while (j/2 > 0 && minHeap[j] < minHeap[j/2])
                {
                    // 自下往上堆化
                    guodu=minHeap[j/2];
                    minHeap[j/2]=minHeap[j];
                    minHeap[j]=guodu;
                    j = j/2;
                }

            }
            for(int i=0;i<nums.size()-k;i++)
            {
                if(nums[i+k]>=minHeap[1])
                {
                    //删除顶点
                    minHeap[1]=minHeap[num];
                    int upToDown=1;
                    int minPos = upToDown;
                    while (true)
                    {
                        minPos = upToDown;
                        if (upToDown*2 <= num && minHeap[upToDown] > minHeap[upToDown*2])
                            minPos = upToDown*2;
                        if (upToDown*2+1 <= num && minHeap[minPos] > minHeap[upToDown*2+1])
                            minPos = upToDown*2+1;
                        if (minPos == upToDown)
                            break;
                        // swap(a, i, minPos);
                        guodu=minHeap[upToDown];
                        minHeap[upToDown]=minHeap[minPos];
                        minHeap[minPos]=guodu;
                        upToDown = minPos;
                    }
                    //插入新节点
                    minHeap[num]=nums[i+k];
                    j=num;
                    while (j/2 > 0 && minHeap[j] < minHeap[j/2])
                    {
                        // 自下往上堆化
                        guodu=minHeap[j/2];
                        minHeap[j/2]=minHeap[j];
                        minHeap[j]=guodu;
                        j = j/2;
                    }
                }
            }
        }
    }

    int add(int val)
    {
        int guodu;
        if(countnum<num)
        {
            countnum++;
            int index=countnum;
            minHeap[index]=val;
            int j=index;
            while (j/2 > 0 && minHeap[j] < minHeap[j/2])
            {
                // 自下往上堆化
                guodu=minHeap[j/2];
                minHeap[j/2]=minHeap[j];
                minHeap[j]=guodu;
                j = j/2;
            }
            return minHeap[1];
        }
        else
        {
            if(val<minHeap[1])
            {
                return minHeap[1];
            }
            else
            {
                //删除顶点
                minHeap[1]=minHeap[num];
                int i=1;
                int minPos = i;
                while (true)
                {
                    minPos = i;
                    if (i*2 <= num && minHeap[i] > minHeap[i*2])
                        minPos = i*2;
                    if (i*2+1 <= num && minHeap[minPos] > minHeap[i*2+1])
                        minPos = i*2+1;
                    if (minPos == i)
                        break;
                    guodu=minHeap[i];
                    minHeap[i]=minHeap[minPos];
                    minHeap[minPos]=guodu;
                    i = minPos;
                }
                //插入新节点
                minHeap[num]=val;
                int j=num;
                while (j/2 > 0 && minHeap[j] < minHeap[j/2])
                {
                    // 自下往上堆化
                    guodu=minHeap[j/2];
                    minHeap[j/2]=minHeap[j];
                    minHeap[j]=guodu;
                    j = j/2;
                }
                return minHeap[1];
            }
        }
    }
};

int main()
{
    int k=3;
    int val=3;
    vector<int> nums;
    nums.push_back(4);
    nums.push_back(5);
    KthLargest* obj = new KthLargest(k, nums);
    cout<<obj->add(3)<<endl;
    cout<<obj->add(5)<<endl;
    cout<<obj->add(10)<<endl;
    cout<<obj->add(9)<<endl;
    cout<<obj->add(3)<<endl;
    return 0;
}


Published 51 original articles · won praise 25 · views 2623

Guess you like

Origin blog.csdn.net/qq_37724465/article/details/104382322