LeetCode Problem Workspace
Number of Employees Who Met the Target
Determine how many employees reach the required work hours using a direct array-driven counting approach.
1
Topics
6
Code langs
3
Related
Practice Focus
Easy · Array-driven solution strategy
Answer-first summary
Determine how many employees reach the required work hours using a direct array-driven counting approach.
Ace coding interviews with Interview AiBoxInterview AiBox guidance for Array-driven solution strategy
The problem asks for a count of employees whose worked hours meet or exceed a target value. A simple iteration over the hours array allows checking each employee against the target. This array-driven approach ensures a clear, fast, and memory-efficient solution without unnecessary complexity.
Problem Statement
You are given an array hours of length n, where hours[i] represents the number of hours employee i worked. Each employee is expected to meet a minimum target number of hours.
Return the total number of employees whose worked hours are greater than or equal to the given target. Implement a solution that efficiently scans the array and counts qualifying employees.
Examples
Example 1
Input: hours = [0,1,2,3,4], target = 2
Output: 3
The company wants each employee to work for at least 2 hours.
- Employee 0 worked for 0 hours and didn't meet the target.
- Employee 1 worked for 1 hours and didn't meet the target.
- Employee 2 worked for 2 hours and met the target.
- Employee 3 worked for 3 hours and met the target.
- Employee 4 worked for 4 hours and met the target. There are 3 employees who met the target.
Example 2
Input: hours = [5,1,4,2,2], target = 6
Output: 0
The company wants each employee to work for at least 6 hours. There are 0 employees who met the target.
Constraints
- 1 <= n == hours.length <= 50
- 0 <= hours[i], target <= 105
Solution Approach
Iterate and Count
Traverse the hours array and increment a counter whenever an employee's hours are greater than or equal to target. This direct check ensures O(n) time complexity and minimal space usage.
Early Exit Optimization
If all hours are sorted or known to be below target, break early or skip unnecessary comparisons. While optional for small n, it avoids extra work in larger arrays.
Single-pass Array Scan
Use a single pass over the array to maintain simplicity. Avoid extra data structures since the pattern is purely array-driven and counting each valid element suffices.
Complexity Analysis
| Metric | Value |
|---|---|
| Time | Depends on the final approach |
| Space | Depends on the final approach |
Time 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.
What Interviewers Usually Probe
- Check if you can solve this using only a simple array scan without additional data structures.
- They may hint at counting instead of filtering or mapping to emphasize direct array operations.
- Expect questions on handling edge cases where no employee meets the target or all employees meet it.
Common Pitfalls or Variants
Common pitfalls
- Forgetting to include employees whose hours exactly equal the target.
- Using extra arrays or structures unnecessarily instead of a single counter.
- Misinterpreting 0-based indexing when iterating over hours array.
Follow-up variants
- Return a list of employee indices who met the target instead of the count.
- Given a dynamic list of hours, continuously update the count of employees meeting changing targets.
- Compute percentage of employees meeting the target instead of absolute count.
FAQ
What is the most efficient way to solve Number of Employees Who Met the Target?
Iterate through the hours array and count each employee whose hours are greater than or equal to target. This single-pass array approach is optimal.
Can I use built-in array functions to solve this problem?
Yes, functions like filter or reduce work, but a simple for-loop is clearer and avoids unnecessary overhead, keeping the solution O(n).
Does the array need to be sorted to count employees meeting the target?
No, sorting is unnecessary since the pattern only requires checking each element independently.
What happens if all employees have hours below target?
The function should return 0 because no employee meets or exceeds the target value.
How does the array-driven solution handle large target values?
Each employee's hours are compared directly to target, so even if target is large, the solution remains O(n) with minimal memory.
Solution
Solution 1: Iteration and Counting
We can iterate through the array $hours$. For each employee, if their working hours $x$ is greater than or equal to $target$, then we increment the counter $ans$ by one.
class Solution:
def numberOfEmployeesWhoMetTarget(self, hours: List[int], target: int) -> int:
return sum(x >= target for x in hours)Continue Topic
array
Practice more edge cases under the same topic.
arrow_forwardauto_awesomeContinue Pattern
Array-driven solution strategy
Expand the same solving frame across more problems.
arrow_forwardsignal_cellular_altSame Difficulty Track
Easy
Stay on this level to stabilize interview delivery.
arrow_forward