[LeetCode] 1700. The number of students who cannot eat lunch (C++)

1 topic description

  • The school’s lunch buffet provides round and square sandwiches, denoted by the numbers 0 and 1, respectively. All students stand in a queue, and each student likes either round or square.
  • The number of sandwiches in the restaurant is the same as the number of students. All sandwiches are placed in a stack, each round:
  • If the student at the top of the queue likes the sandwich at the top of the stack, they will take it and leave the queue.
  • Otherwise, the student will give up the sandwich and return to the end of the queue.
  • This process will continue until all students in the queue dislike the sandwich on the top of the stack.
  • Give you two integer arrays students and sandwiches, where sandwiches[i] is the type of the i-th sandwich in the stack (i = 0 is the top of the stack), students[j] is the j-th in the initial queue​​​​​​ Students' preference for sandwiches (j = 0 is the beginning of the queue). Please return the number of students who were unable to eat lunch.

2 Example description

2.1 Example 1

Input: students = [1,1,0,0], sandwiches = [0,1,0,1]
Output: 0
Explanation:

  • The first student abandons the top sandwich and returns to the end of the queue. The student queue becomes students = [1,0,0,1].
  • The first student abandons the top sandwich and returns to the end of the queue. The student queue becomes students = [0,0,1,1].
  • The front student takes the top sandwich, the remaining student queue is students = [0,1,1], and the sandwich stack is sandwiches = [1,0,1].
  • The first student abandons the top sandwich and returns to the end of the queue. The student queue becomes students = [1,1,0].
  • The front student takes the top sandwich, the remaining student queue is students = [1,0], and the sandwich stack is sandwiches = [0,1].
  • The first student abandons the top sandwich and returns to the end of the queue. The student queue becomes students = [0,1].
  • The first student takes the top sandwich, the remaining student queue is students = [1], and the sandwich stack is sandwiches = [1].
  • The front student takes the top sandwich, the remaining student queue is students = [], and the sandwich stack is sandwiches = [].
    So all students have sandwiches to eat.

2.2 Example 2

Input: students = [1,1,1,0,0,1], sandwiches = [1,0,0,0,1,1]
Output: 3

3 Problem solving tips

1 <= students.length, sandwiches.length <= 100
students.length == sandwiches.length
sandwiches[i] is either 0 or 1.
students[i] is either 0 or 1.

4 Problem-solving ideas

Loop array solution, simple and violent.

5 Detailed source code (C++)

class Solution {
    
    
public:
    int countStudents(vector<int>& students, vector<int>& sandwiches) {
    
    
        int index = 0 ;
        while ( true )
        {
    
    
            bool flag = false ;
            for ( int i = 0 ; i < students.size() ; i ++ )
            {
    
    
                if ( students[i] == -1 )
                {
    
    
                    continue;
                }
                if ( students[i] == sandwiches[index] )
                {
    
    
                    flag = true ;
                    students[i] = -1 ;
                    index ++ ;
                }
            }

            if ( !flag )
            {
    
    
                break ;
            }
        }
        return ( students.size() - index ) ;
    }
};

Guess you like

Origin blog.csdn.net/Gyangxixi/article/details/114095707