Leetcode Difficulty——Maximum Sliding Window (Ultra-detailed idea, with queue or without queue, you can pass)

(Recently I’m doing difficult questions, but most of them are similar to the comments, and I don’t have the desire to write solutions. I think the comments on this question are all written in queues, but they can be passed without queues, so I wrote them separately, using queues and No queue idea)

Question:
Given an integer array nums, there is a sliding window of size k that moves from the far left of the array to the far right of the array. You can only see k numbers within the sliding window. The sliding window only moves to the right one bit at a time.
Returns the maximum value in a sliding window.

answer:

① use the queue

First, look at the priority queue, but how to remove the unnecessary values ​​​​on the left when moving the window? At this time, we can use set to replace the priority queue, so how do we prevent the set from being deduplicated? We can use pair to store values ​​and values ​​at the same time. Subscript, so that it will not be duplicated. The next step is very simple. Every time you move the window, add the new value on the right, remove the unnecessary value on the left, and then take the maximum value.

typedef pair<int, int> P;
class Solution {
public:
    set<P> temp;
    vector<int> res;
    vector<int> maxSlidingWindow(vector<int>& nums, int k) {
        for(int i = 0; i < k; i++) {
            temp.insert(make_pair(nums[i], i));
        }
        res.push_back(temp.rbegin() -> first);
        for(int i = k; i < nums.size(); i++) {
            temp.erase(make_pair(nums[i - k], i - k));  // 去除
            temp.insert(make_pair(nums[i], i));  添加
            res.push_back(temp.rbegin() -> first);   // 取范围最大值
        }
        return res;
    }
};

But this can be optimized, we can still use the priority queue to write, the details are to consider the size of the added value, but this comment area is full, I will not go into details, you can see the comment area of ​​this question

② No queue

First of all, I look at the value range of this number -10 to the fourth power of 10. I feel that I can definitely consider starting from here, otherwise why not take a larger range

Then we build an array data[20005], which is the value range of all numbers. Next, we need to consider what kind of values ​​should be stored in it, nothing more than two situations

1. The current situation of the window
2. The situation of the entire array

If it is the first case, then we can store the number of values ​​in the range in the array, but how do we find the maximum value? Traverse? Impossible, must time out. Use a segment tree? No, the complexity is about the same as above, so it doesn't make much sense.

So we eliminate the first case, and then consider the second case, then we must not only store the number of each value, at this time the position of the value must also be very important, so we store all the indexes of the value (using vector live)

Then how do we use this value to operate? We can think that since we are looking for the maximum value of the range, the maximum value of all values ​​at this time must be the maximum value within its range, and the second largest value is except The maximum value of other ranges in the range where the maximum value exists, then continue in this order until the maximum value of all ranges is determined, so is there a solution?

The next step is how to operate. Our current value cannot repeatedly cover the range where the previous larger value was located. Then we can use bool array to store whether it is covered by the previous larger value, and then just skip the overwritten one. Each value is considered k times, which is very simple, but it will time out (pro-test)

So what should we do? We have to consider, if we use bool to save, will we miss any information and not save it? By the way, we can change bool to int to save the maximum position after the current position is overwritten. In this way, we don't need to traverse one by one, we can skip it directly, so that we can run through it within the specified time

typedef pair<int, int> P;
class Solution {
public:
    int flag1 = 0;
    int flag2 = 0;
    vector<int> res;
    vector<int> ddd[20005];
    int mmm[100005];
    int fff[100005] = { 0 };
    vector<int> maxSlidingWindow(vector<int>& nums, int k) {
        for(int i = 0; i < nums.size(); i++) {
            ddd[nums[i] + 10000].push_back(i);
            flag2 = max(flag2, nums[i] + 10000);
        }
        while(flag1 < nums.size()) {
            while(ddd[flag2].empty()) {
                flag2--;
            }
            int f = ddd[flag2][ddd[flag2].size() - 1];
            int p = 0;
            while(p < k && f < nums.size()) {
                if(fff[f] != 0) {
                    int temp = fff[f];
                    fff[f] = k - p;
                    p = p + temp;
                    f = f + temp;
                }
                else
                {
                    fff[f] = k - p;
                    mmm[f] = flag2 - 10000;
                    p++;
                    f++;
                    flag1++;
                }
            }
            ddd[flag2].pop_back();
        }
        for(int i = k - 1; i < nums.size(); i++) {
            res.push_back(mmm[i]);
        }
        return res;
    }
};

Guess you like

Origin blog.csdn.net/m0_52212261/article/details/128691202