LeetCode 题解工作台
满足目标工作时长的员工数目
公司里共有 n 名员工,按从 0 到 n - 1 编号。每个员工 i 已经在公司工作了 hours[i] 小时。 公司要求每位员工工作 至少 target 小时。 给你一个下标从 0 开始、长度为 n 的非负整数数组 hours 和一个非负整数 target 。 请你用整数表示并返回工作至少 tar…
1
题型
6
代码语言
3
相关题
当前训练重点
简单 · 数组·driven
答案摘要
我们可以遍历数组 ,对于每个员工,如果其工作时长 大于等于 ,则将计数器 加一。 遍历结束后,返回答案即可。
Interview AiBoxInterview AiBox 实时 AI 助手,陪你讲清 数组·driven 题型思路
题目描述
公司里共有 n 名员工,按从 0 到 n - 1 编号。每个员工 i 已经在公司工作了 hours[i] 小时。
公司要求每位员工工作 至少 target 小时。
给你一个下标从 0 开始、长度为 n 的非负整数数组 hours 和一个非负整数 target 。
请你用整数表示并返回工作至少 target 小时的员工数。
示例 1:
输入:hours = [0,1,2,3,4], target = 2 输出:3 解释:公司要求每位员工工作至少 2 小时。 - 员工 0 工作 0 小时,不满足要求。 - 员工 1 工作 1 小时,不满足要求。 - 员工 2 工作 2 小时,满足要求。 - 员工 3 工作 3 小时,满足要求。 - 员工 4 工作 4 小时,满足要求。 共有 3 位满足要求的员工。
示例 2:
输入:hours = [5,1,4,2,2], target = 6 输出:0 解释:公司要求每位员工工作至少 6 小时。 共有 0 位满足要求的员工。
提示:
1 <= n == hours.length <= 500 <= hours[i], target <= 105
解题思路
方法一:遍历计数
我们可以遍历数组 ,对于每个员工,如果其工作时长 大于等于 ,则将计数器 加一。
遍历结束后,返回答案即可。
时间复杂度 ,其中 是数组 的长度。空间复杂度 。
class Solution:
def numberOfEmployeesWhoMetTarget(self, hours: List[int], target: int) -> int:
return sum(x >= target for x in hours)
复杂度分析
| 指标 | 值 |
|---|---|
| 时间 | complexity is O(n) because each employee is checked exactly once. Space complexity is O(1) since only a counter is maintained, making it very efficient for arrays up to length 50. |
| 空间 | Depends on the final approach |
面试官常问的追问
外企场景- question_mark
Check if you can solve this using only a simple array scan without additional data structures.
- question_mark
They may hint at counting instead of filtering or mapping to emphasize direct array operations.
- question_mark
Expect questions on handling edge cases where no employee meets the target or all employees meet it.
常见陷阱
外企场景- error
Forgetting to include employees whose hours exactly equal the target.
- error
Using extra arrays or structures unnecessarily instead of a single counter.
- error
Misinterpreting 0-based indexing when iterating over hours array.
进阶变体
外企场景- arrow_right_alt
Return a list of employee indices who met the target instead of the count.
- arrow_right_alt
Given a dynamic list of hours, continuously update the count of employees meeting changing targets.
- arrow_right_alt
Compute percentage of employees meeting the target instead of absolute count.