leetcode 1700. Number of Students Unable to Eat Lunch(python)

描述

The school cafeteria offers circular and square sandwiches at lunch break, referred to by numbers 0 and 1 respectively. All students stand in a queue. Each student either prefers square or circular sandwiches.

The number of sandwiches in the cafeteria is equal to the number of students. The sandwiches are placed in a stack. At each step:

  • If the student at the front of the queue prefers the sandwich on the top of the stack, they will take it and leave the queue.
  • Otherwise, they will leave it and go to the queue’s end.
    This continues until none of the queue students want to take the top sandwich and are thus unable to eat.

You are given 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) and students[j] is the preference of the j​​​​​​th student in the initial queue (j = 0 is the front of the queue). Return the number of students that are unable to eat.

Example 1:

Input: students = [1,1,0,0], sandwiches = [0,1,0,1]
Output: 0 
Explanation:
- Front student leaves the top sandwich and returns to the end of the line making students = [1,0,0,1].
- Front student leaves the top sandwich and returns to the end of the line making students = [0,0,1,1].
- Front student takes the top sandwich and leaves the line making students = [0,1,1] and sandwiches = [1,0,1].
- Front student leaves the top sandwich and returns to the end of the line making students = [1,1,0].
- Front student takes the top sandwich and leaves the line making students = [1,0] and sandwiches = [0,1].
- Front student leaves the top sandwich and returns to the end of the line making students = [0,1].
- Front student takes the top sandwich and leaves the line making students = [1] and sandwiches = [1].
- Front student takes the top sandwich and leaves the line making students = [] and sandwiches = [].
Hence all students are able to eat.	

Example 2:

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

Example 3:

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

Note:

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

解析

根据题意,就是有两种三明治 0 和 1 ,有一排和三明治数量相同的学生在排队,他们喜欢两种三明治中的任意一种,当轮到一个学生的时候,面前正好有喜欢的三明治类型,直接拿了走人,否则去队尾重新排队,当目前的三明治都没人想要的时候则结束,算最后剩下几个没饭吃挑食的家伙。题目虽然长但是不难,只需要使用 while 语句循环遍历 students ,如果当理想情况,则 students 和 sandwiches 都 pop(0),否则只能将当前学生放到 students 最后,记录没有得到食物的学生和当前 students 数量相等则跳出循环,最后的 students 数量就是结果。

解答

class Solution(object):
    def countStudents(self, students, sandwiches):
        """
        :type students: List[int]
        :type sandwiches: List[int]
        :rtype: int
        """
        count = 0
        while students:
            student = students.pop(0)
            if student == sandwiches[0]:
                count=0
                sandwiches.pop(0)
            else:
                count+=1
                students.append(student)
            if count==len(students):
                break
        return len(students)         	      

运行结果

Runtime: 24 ms, faster than 70.16% of Python online submissions for Number of Students Unable to Eat Lunch.
Memory Usage: 13.4 MB, less than 69.63% of Python online submissions for Number of Students Unable to Eat Lunch.

原题链接:https://leetcode.com/problems/number-of-students-unable-to-eat-lunch/

您的支持是我最大的动力

你可能感兴趣的:(leetcode,leetcode,python,算法)