LeetCode-1700. Number of students unable to eat lunch

Title 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.

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 front 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.
    Example 2:

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

code show as below:

class Solution {
    
    
    public int countStudents(int[] students, int[] sandwiches) {
    
    
        int m = students.length;
        int n = sandwiches.length;
        if (m == 0) {
    
    
            return 0;
        }
        if (n == 0) {
    
    
            return m;
        }
        Queue<Integer> que = new LinkedList<>();
        for (int student : students) {
    
    
            que.offer(student);
        }
        for (int i = 0; i < n; i++) {
    
    
            if (que.isEmpty()) {
    
    
                return 0;
            }
            int size = que.size();
            boolean flag = false;
            for (int j = 0; j < size; j++) {
    
    
                int poll = que.poll();
                if (poll != sandwiches[i]) {
    
    
                    que.offer(poll);
                } else {
    
    
                    flag = true;
                    break;
                }
            }
            if (!flag) {
    
    
                return size;
            }
        }
        return que.size();
    }
}

Results of the:
Insert picture description here

Guess you like

Origin blog.csdn.net/FYPPPP/article/details/114278813