Exercises on stacks and queues [6]

The study arrangement is based on "Code Caprice" leetcode239

This topic has a milestone significance! ! ! It means that the thinking is clear, and I also have the ability to solve difficult problems! It indicates that on the way to solve the problem, suffering and suffering are meaningful!

After being touched by myself, enter the topic~

In fact, I didn't expect that difficult questions could be written in "Deixue", but it seems that it is not difficult to understand after reading the questions. Although it may be marked as difficult questions, it is actually a medium difficulty level~

So, I did it without looking at any solution to the problem~

The first pass uses a sliding window and uses an array to store the largest value, but only 23 examples pass. One of the reasons is that the array itself is not returned when k==1. After adding, 25 examples pass. Then I tangled at this point for a few hours, without giving up.

The second time I want to use the stack and queue in this section, but I still can't think of how to operate on these two data structures to get the desired result. I struggled for more than an hour and gave up.

For the third time, in order to seek help, I went to see the official problem solution, which was obscure and tangled and gave up half a point.

The fourth time I searched for the unofficial problem solution, I found the problem solution with graphics-assisted comprehension.

After going back to bed for the fifth time, I found the question again to clarify my thoughts and then look at the code and the code in the comment area, but it was still difficult to understand and give up;

The sixth time I came to the teaching and research room in the morning, I found the solution to the problem on the scratch paper again and again, combined the problem to clarify the idea, and tried to write the answer myself, but reported many errors, and the instructor gave up at the meeting;

After the seventh meeting, come back to solve the cause of the error and repeatedly debug on VS finally! pass through!

class Solution {
public:
    vector<int> maxSlidingWindow(vector<int>& nums, int k)
{
    deque<int>dq;
    vector<int>v;
    if(k==1)return nums;
    for (int left = 0, right = 0; right < nums.size(); right++)
    {
        if (dq.empty() || dq.back() >= nums[right])
        {
            dq.push_back(nums[right]);
        }
        else if ( !dq.empty()&&nums[right] > dq.back() )
        {
            while (!dq.empty()&&nums[right] > dq.back() )
            {
                dq.pop_back();
            }
            dq.push_back(nums[right]);
        }
        if (right - left == k - 1)
        {
            v.push_back(dq.front());
            continue;
        }
        else if (right - left == k)
        {
            left++;
            if (dq.front() <= nums[left-1])
            {
                dq.pop_front();
                v.push_back(dq.front());
            }
            else
            {
                v.push_back(dq.front());
                continue;
            }
        }
    } 
        return v;    
    }
};

The result is passed, although there is still room for improvement, it is already the greatest encouragement to me! Now to summarize the code:

1. The sliding window idea must be selected, and it has been clearly stated in the title;

2. Why choose double-ended queue deque: because the following steps are required in the operation

  • Tail storage data: queue, array, stack, double-ended queue, linked list, etc.;
  • Head delete data: queue, stack, double-ended, linked list;
  • Tail delete data: double-ended

Therefore, the only eligible option is to choose double-ended

3. How to ensure that front() is always the largest:

        The idea of ​​monotonic queue is adopted, that is, the elements in the array are compared with the back() of the queue. If the back() is small, the value at the tail will not be the next largest number, and it will be deleted from the tail. If back() greater/equal, the trailing value may be the next largest number, inserting the end of the array element. In this case, monotonicity is guaranteed.

4. How to ensure the window size and pop-up numbers:

        When the window size is k, the front() number pops up; when the window is larger than three, the left pointer moves to the right, ensuring that the window is always k.

  • When the subscript of the front() element is on the left or equal to the left, the window no longer contains the front() element, the front() is deleted from the head, and the current front() is popped up;
  • When the subscript of the front() element is on the right or equal to the left, the front() will pop up directly.

 When the actual code is running, the front() subscript cannot be determined, but the maximum value is definitely close to the window boundary value, so just judge front() and nums[left-1].

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=324081206&siteId=291194637