Novice queue (example)

Posted by BlaineSch on Wed, 02 Feb 2022 22:29:50 +0100

Queue learning can be compared to stack learning. Queue is an important data structure to realize breadth first search.  

queue<int> aa; //Define a queue

-Basic operation of queue——

aa.front() //Returns the first element of the queue
aa.back()  //Returns the last element of the queue
aa.empty() //Returns true if the queue is empty
aa.push()  //Add an element to the end of the queue
aa.pop()   //Delete an element at the head of the queue
aa.size()  //Returns the number of elements in the queue

Example 1: number of students unable to eat lunch (leetcode 1700)

https://leetcode-cn.com/problems/number-of-students-unable-to-eat-lunch/https://leetcode-cn.com/problems/number-of-students-unable-to-eat-lunch/

Title Description:

The school buffet lunch offers round and square sandwiches, represented by the numbers , 0 , and , 1 , respectively. All the students stand in a queue. Each student likes either round or square.
The number of sandwiches in the restaurant is the same as that of students. All sandwiches are put in a stack, each round:

If the student at the front of the queue likes the sandwich at the top of the stack, he will take it and leave the queue.
Otherwise, the student will give up the sandwich and go back to the end of the queue.
This process will continue until all the students in the queue don't like the sandwich at the top of the stack.

Here are two integer arrays, students and sandwiches, where , sandwiches[i] is the type of the , th sandwich in the stack (i = 0 , is the top of the stack, and students[j] is the preference of the , th student in the initial queue for sandwiches (j = 0 , is the beginning of the queue). Please return the number of students who cannot have lunch.

Example:

Topic analysis:

The requirement of the topic is to have a food stack and a student queue. The length of the stack and queue are the same. According to the meaning of the question, use circulation
In each cycle, compare whether the stack top data is the same as the team head data;
Same, out of the stack, out of the team;
Different, out of the team, in the team (intermediate variable);
When the data of all elements in the queue is different from the data at the top of the stack, the number of queue elements is returned as the solution.

Number of cycles
Since it is impossible to determine when to jump out of the cycle, it is not known whether any students will get sandwiches in the next cycle.
But we know that when all the students in the queue don't eat sandwiches at the top of the stack, they can jump out of the loop;
So you can set the number of cycles as a variable, which is the length of the queue.
Whenever the queue length changes, set the loop variable i to - 1 to enter the next loop.

Problem solving Code:

class Solution {
public:
    int countStudents(vector<int>& students, vector<int>& sandwiches) {
         stack<int> aa;
         queue<int> bb;

         for(int i=sandwiches.size()-1;i>=0;i--){
             aa.push(sandwiches[i]);
         }

         for(int j=0;j<students.size();j++){
             bb.push(students[j]);
         }

         int k=0;
         while(1){
             if(bb.empty()){
                 break;
             }

             if(bb.front()==aa.top()){
                 bb.pop();
                 aa.pop();
                 k=0;

             }else{
                 bb.push(bb.front());
                 bb.pop();
                 k++;      //Use k to record whether the remaining elements are compared with the elements at the top of the stack

                 if(k==bb.size()){
                      break;
                 }
             }

         }

         return bb.size();
    }
};

Example 2: jumping game VI (leetcode 1696)

https://leetcode-cn.com/problems/jump-game-vi/https://leetcode-cn.com/problems/jump-game-vi/ Title Description:

Give you an array of integers with subscripts starting from 0, nums # and an integer k.

At first you were at the subscript , 0 ,. At each step, you can jump up to {K} steps forward, but you can't jump out of the boundary of the array. That is, you can jump from the subscript , I , to any position where [i + 1, min(n - 1, i + k)] contains two endpoints.

Your goal is to reach the last position of the array (subscript n - 1), and your score is the sum of all the numbers passed.

Please return the maximum score you can get.

Example 1:

Example 2:

Topic analysis:

We maintain a double ended monotonically decreasing queue (the value decreases from the head of the queue to the tail of the queue, but only the subscript is saved here). For each element entering the queue, we successively compare its value with the value of the element at the tail of the queue. If the current element is larger, we pop up the element at the tail of the queue and repeat this process until the queue is empty or the current element is smaller than the element at the tail of the queue, Then insert the current element, and finally take out the qualified team head element (maximum value).

Illustration:

Problem solving Code:

class Solution {
public:
    int maxResult(vector<int>& nums, int k) {
        deque<pair<int,int>> aa;

        aa.emplace_back(nums[0],0);

        int n=nums.size();
        int count=nums[0];

        for(int i=1;i<n;i++){
            while(aa.front().second + k < i){
                  aa.pop_front();
            }

            count=nums[i] + aa.front().first;

            while(!aa.empty() && count >= aa.back().first){
                  aa.pop_back();
            }

            aa.emplace_back(count,i);
        }

        return count;
    }
};

Topics: C++ leetcode queue