LeetCode 题解工作台

在既定时间做作业的学生人数

给你两个整数数组 startTime (开始时间)和 endTime (结束时间),并指定一个整数 queryTime 作为查询时间。 已知,第 i 名学生在 startTime[i] 时开始写作业并于 endTime[i] 时完成作业。 请返回在查询时间 queryTime 时正在做作业的学生人数…

category

1

题型

code_blocks

7

代码语言

hub

3

相关题

当前训练重点

简单 · 数组·driven

bolt

答案摘要

我们可以直接遍历两个数组,对于每个学生,判断 是否在他们的作业时间区间内,若是,答案加一。 遍历结束后,返回答案即可。

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 数组·driven 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

给你两个整数数组 startTime(开始时间)和 endTime(结束时间),并指定一个整数 queryTime 作为查询时间。

已知,第 i 名学生在 startTime[i] 时开始写作业并于 endTime[i] 时完成作业。

请返回在查询时间 queryTime 时正在做作业的学生人数。形式上,返回能够使 queryTime 处于区间 [startTime[i], endTime[i]](含)的学生人数。

 

示例 1:

输入:startTime = [1,2,3], endTime = [3,2,7], queryTime = 4
输出:1
解释:一共有 3 名学生。
第一名学生在时间 1 开始写作业,并于时间 3 完成作业,在时间 4 没有处于做作业的状态。
第二名学生在时间 2 开始写作业,并于时间 2 完成作业,在时间 4 没有处于做作业的状态。
第三名学生在时间 3 开始写作业,预计于时间 7 完成作业,这是是唯一一名在时间 4 时正在做作业的学生。

示例 2:

输入:startTime = [4], endTime = [4], queryTime = 4
输出:1
解释:在查询时间只有一名学生在做作业。

示例 3:

输入:startTime = [4], endTime = [4], queryTime = 5
输出:0

示例 4:

输入:startTime = [1,1,1,1], endTime = [1,3,2,4], queryTime = 7
输出:0

示例 5:

输入:startTime = [9,8,7,6,5,4,3,2,1], endTime = [10,10,10,10,10,10,10,10,10], queryTime = 5
输出:5

 

提示:

  • startTime.length == endTime.length
  • 1 <= startTime.length <= 100
  • 1 <= startTime[i] <= endTime[i] <= 1000
  • 1 <= queryTime <= 1000
lightbulb

解题思路

方法一:直接遍历

我们可以直接遍历两个数组,对于每个学生,判断 queryTime\textit{queryTime} 是否在他们的作业时间区间内,若是,答案加一。

遍历结束后,返回答案即可。

时间复杂度 O(n)O(n),其中 nn 为学生数量。空间复杂度 O(1)O(1)

1
2
3
4
5
6
class Solution:
    def busyStudent(
        self, startTime: List[int], endTime: List[int], queryTime: int
    ) -> int:
        return sum(x <= queryTime <= y for x, y in zip(startTime, endTime))
speed

复杂度分析

指标
时间Depends on the final approach
空间Depends on the final approach
psychology

面试官常问的追问

外企场景
  • question_mark

    The problem tests the candidate's ability to iterate over arrays and check conditions efficiently.

  • question_mark

    A good candidate will demonstrate knowledge of array-driven solutions and optimization techniques.

  • question_mark

    Look for efficient handling of edge cases such as a single student's homework or multiple overlapping intervals.

warning

常见陷阱

外企场景
  • error

    Forgetting to include the endpoints of the interval, which may cause missing valid students.

  • error

    Overcomplicating the solution by using unnecessary optimizations, when the problem can be solved with simple iteration.

  • error

    Not handling edge cases like overlapping intervals or when `startTime[i]` equals `endTime[i]`.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    What if `startTime` and `endTime` were sorted? This would allow for more advanced solutions like binary search.

  • arrow_right_alt

    What if the number of students was significantly larger, requiring space or time optimizations?

  • arrow_right_alt

    Consider the situation where we need to return a range of times when students are doing homework, not just a single query time.

help

常见问题

外企场景

在既定时间做作业的学生人数题解:数组·driven | LeetCode #1450 简单