LeetCode 题解工作台

无法吃午餐的学生数量

学校的自助午餐提供圆形和方形的三明治,分别用数字 0 和 1 表示。所有学生站在一个队列里,每个学生要么喜欢圆形的要么喜欢方形的。 餐厅里三明治的数量与学生的数量相同。所有三明治都放在一个 栈 里,每一轮: 如果队列最前面的学生 喜欢 栈顶的三明治,那么会 拿走它 并离开队列。 否则,这名学生会 放…

category

4

题型

code_blocks

7

代码语言

hub

3

相关题

当前训练重点

简单 · 栈·状态

bolt

答案摘要

我们观察发现,学生位置可调整,而三明治位置不可调整。也就是说,若前面的三明治没被拿走,则往后的所有三明治也无法被拿走。 因此,我们先用计数器 统计学生喜欢的三明治种类和对应的数量。

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 栈·状态 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

学校的自助午餐提供圆形和方形的三明治,分别用数字 0 和 1 表示。所有学生站在一个队列里,每个学生要么喜欢圆形的要么喜欢方形的。
餐厅里三明治的数量与学生的数量相同。所有三明治都放在一个  里,每一轮:

  • 如果队列最前面的学生 喜欢 栈顶的三明治,那么会 拿走它 并离开队列。
  • 否则,这名学生会 放弃这个三明治 并回到队列的尾部。

这个过程会一直持续到队列里所有学生都不喜欢栈顶的三明治为止。

给你两个整数数组 students 和 sandwiches ,其中 sandwiches[i] 是栈里面第 i​​​​​​ 个三明治的类型(i = 0 是栈的顶部), students[j] 是初始队列里第 j​​​​​​ 名学生对三明治的喜好(j = 0 是队列的最开始位置)。请你返回无法吃午餐的学生数量。

 

示例 1:

输入:students = [1,1,0,0], sandwiches = [0,1,0,1]
输出:0 
解释:
- 最前面的学生放弃最顶上的三明治,并回到队列的末尾,学生队列变为 students = [1,0,0,1]。
- 最前面的学生放弃最顶上的三明治,并回到队列的末尾,学生队列变为 students = [0,0,1,1]。
- 最前面的学生拿走最顶上的三明治,剩余学生队列为 students = [0,1,1],三明治栈为 sandwiches = [1,0,1]。
- 最前面的学生放弃最顶上的三明治,并回到队列的末尾,学生队列变为 students = [1,1,0]。
- 最前面的学生拿走最顶上的三明治,剩余学生队列为 students = [1,0],三明治栈为 sandwiches = [0,1]。
- 最前面的学生放弃最顶上的三明治,并回到队列的末尾,学生队列变为 students = [0,1]。
- 最前面的学生拿走最顶上的三明治,剩余学生队列为 students = [1],三明治栈为 sandwiches = [1]。
- 最前面的学生拿走最顶上的三明治,剩余学生队列为 students = [],三明治栈为 sandwiches = []。
所以所有学生都有三明治吃。

示例 2:

输入:students = [1,1,1,0,0,1], sandwiches = [1,0,0,0,1,1]
输出:3

 

提示:

  • 1 <= students.length, sandwiches.length <= 100
  • students.length == sandwiches.length
  • sandwiches[i] 要么是 0 ,要么是 1 。
  • students[i] 要么是 0 ,要么是 1 。
lightbulb

解题思路

方法一:计数

我们观察发现,学生位置可调整,而三明治位置不可调整。也就是说,若前面的三明治没被拿走,则往后的所有三明治也无法被拿走。

因此,我们先用计数器 cntcnt 统计学生喜欢的三明治种类和对应的数量。

然后遍历三明治,若在 cntcnt 中找不到喜欢此三明治的学生,说明后面的三明治也无法被拿走,返回当前剩余的学生数量。

遍历结束。,说明所有学生都有三明治吃,返回 00

时间复杂度 O(n)O(n),其中 nn 为三明治数量。空间复杂度 O(1)O(1)

1
2
3
4
5
6
7
8
9
class Solution:
    def countStudents(self, students: List[int], sandwiches: List[int]) -> int:
        cnt = Counter(students)
        for v in sandwiches:
            if cnt[v] == 0:
                return cnt[v ^ 1]
            cnt[v] -= 1
        return 0
speed

复杂度分析

指标
时间O(n + m)
空间O(1)
psychology

面试官常问的追问

外企场景
  • question_mark

    A candidate should be able to efficiently simulate the problem with a queue and stack.

  • question_mark

    Look for an understanding of stack-based state management, which is central to solving this problem.

  • question_mark

    The candidate should avoid unnecessary operations or memory usage when simulating the process.

warning

常见陷阱

外企场景
  • error

    Over-complicating the simulation with unnecessary data structures or loops.

  • error

    Not handling the case when all students have the same sandwich preference, causing an infinite loop of students not getting food.

  • error

    Ignoring the constraint of student preferences and prematurely stopping the simulation without checking if all students can be served.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Change the number of sandwich types or preferences, introducing additional sandwich types.

  • arrow_right_alt

    Modify the queue order of students, such as processing them in reverse or with other variations in the way preferences are set.

  • arrow_right_alt

    Introduce scenarios where the number of sandwiches exceeds the number of students, testing how the solution handles surplus sandwiches.

help

常见问题

外企场景

无法吃午餐的学生数量题解:栈·状态 | LeetCode #1700 简单