Skip to content

Latest commit

 

History

History
147 lines (110 loc) · 4.54 KB

1450.md

File metadata and controls

147 lines (110 loc) · 4.54 KB
title description keywords
1450. 在既定时间做作业的学生人数
LeetCode 1450. 在既定时间做作业的学生人数题解,Number of Students Doing Homework at a Given Time,包含解题思路、复杂度分析以及完整的 JavaScript 代码实现。
LeetCode
1450. 在既定时间做作业的学生人数
在既定时间做作业的学生人数
Number of Students Doing Homework at a Given Time
解题思路
数组

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

🟢 Easy  🔖  数组  🔗 力扣 LeetCode

题目

Given two integer arrays startTime and endTime and given an integer queryTime.

The ith student started doing their homework at the time startTime[i] and finished it at time endTime[i].

Return the number of students doing their homework at time queryTime. More formally, return the number of students where queryTime lays in the interval [startTime[i], endTime[i]] inclusive.

Example 1:

Input: startTime = [1,2,3], endTime = [3,2,7], queryTime = 4

Output: 1

Explanation: We have 3 students where:

The first student started doing homework at time 1 and finished at time 3 and wasn't doing anything at time 4.

The second student started doing homework at time 2 and finished at time 2 and also wasn't doing anything at time 4.

The third student started doing homework at time 3 and finished at time 7 and was the only student doing homework at time 4.

Example 2:

Input: startTime = [4], endTime = [4], queryTime = 4

Output: 1

Explanation: The only student was doing their homework at the queryTime.

Constraints:

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

题目大意

给你两个整数数组 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

解题思路

  1. 定义 count 变量,初始值为 0,用于统计符合条件的学生人数。

  2. 遍历所有学生的时间段:

    • 对于每个学生,检查 queryTime 是否在 startTime[i]endTime[i] 之间,包括边界。
    • 如果符合条件,递增 count
  3. 遍历结束后,返回计数器 count 的值。

复杂度分析

  • 时间复杂度: O(n),其中 n 是学生数量,遍历所有学生的时间段。
  • 空间复杂度: O(1),仅使用了常数空间变量 count,空间复杂度为常数。

代码

/**
 * @param {number[]} startTime
 * @param {number[]} endTime
 * @param {number} queryTime
 * @return {number}
 */
var busyStudent = function (startTime, endTime, queryTime) {
	let count = 0;
	for (let i = 0; i < startTime.length; i++) {
		if (queryTime >= startTime[i] && queryTime <= endTime[i]) {
			count++;
		}
	}
	return count;
};