LeetCode 2798 - Number of Employees Who Met the Target
The problem asks us to determine how many employees in a company have worked at least a minimum number of hours, specified by target. We are given a 0-indexed array hours, where hours[i] represents the total hours worked by employee i.
Difficulty: 🟢 Easy
Topics: Array
Solution
Problem Understanding
The problem asks us to determine how many employees in a company have worked at least a minimum number of hours, specified by target. We are given a 0-indexed array hours, where hours[i] represents the total hours worked by employee i. The task is to count all employees whose worked hours are greater than or equal to target and return this count as an integer.
The input array hours has a length n which ranges from 1 to 50, meaning the dataset is small. Each element in the array and the target value are non-negative integers up to 105. The constraints guarantee that the input is valid and non-empty.
Important edge cases to consider include:
- All employees worked fewer hours than the target. The output should be 0.
- All employees worked hours equal to or greater than the target. The output should be
n. - The target is 0, which should count every employee, since all non-negative numbers are ≥ 0.
- Hours values contain zeros, which should be ignored if the target > 0.
The problem is straightforward because it only requires a single pass through the array to compare each value against the target.
Approaches
A brute-force approach is simple: iterate through the array and count every employee whose hours meet or exceed the target. Since the input size is small (n <= 50), this approach is sufficient and essentially optimal.
The key insight is that we do not need sorting or extra data structures. A single linear pass with a counter is both correct and efficient because we are only comparing each element to the target and incrementing a counter.
| Approach | Time Complexity | Space Complexity | Notes |
|---|---|---|---|
| Brute Force | O(n) | O(1) | Iterate through the array, incrementing a counter for hours[i] >= target |
| Optimal | O(n) | O(1) | Single pass, linear scan; effectively the same as brute force for this problem |
Algorithm Walkthrough
- Initialize a counter variable
countto 0. This variable will track the number of employees meeting the target. - Iterate through each element
hin the arrayhours. - For each element, check if
his greater than or equal totarget. - If the condition is true, increment
countby 1. - Continue until all elements have been processed.
- Return
countas the final result.
Why it works: Each employee is checked exactly once, and the counter only increments for employees whose hours satisfy the requirement. This ensures the final count represents exactly the number of employees meeting or exceeding the target.
Python Solution
from typing import List
class Solution:
def numberOfEmployeesWhoMetTarget(self, hours: List[int], target: int) -> int:
count = 0
for h in hours:
if h >= target:
count += 1
return count
Explanation: We start with a counter count set to 0. We loop through each value in hours, checking if it meets or exceeds target. If it does, we increment count. After finishing the loop, count represents the total employees who met the target, which is returned.
Go Solution
func numberOfEmployeesWhoMetTarget(hours []int, target int) int {
count := 0
for _, h := range hours {
if h >= target {
count++
}
}
return count
}
Explanation: The Go solution follows the same logic as Python. We initialize count to 0 and loop through the hours slice using the range keyword. Each value h is compared to target, and count is incremented if the employee meets or exceeds the target. Finally, count is returned. No special handling is required for empty slices, as the constraints guarantee at least one employee.
Worked Examples
Example 1
hours = [0,1,2,3,4], target = 2
Iteration:
| Employee | Hours | Meets Target? | Count |
|---|---|---|---|
| 0 | 0 | No | 0 |
| 1 | 1 | No | 0 |
| 2 | 2 | Yes | 1 |
| 3 | 3 | Yes | 2 |
| 4 | 4 | Yes | 3 |
Output: 3
Example 2
hours = [5,1,4,2,2], target = 6
Iteration:
| Employee | Hours | Meets Target? | Count |
|---|---|---|---|
| 0 | 5 | No | 0 |
| 1 | 1 | No | 0 |
| 2 | 4 | No | 0 |
| 3 | 2 | No | 0 |
| 4 | 2 | No | 0 |
Output: 0
Complexity Analysis
| Measure | Complexity | Explanation |
|---|---|---|
| Time | O(n) | We iterate through the list exactly once, performing constant time operations per element |
| Space | O(1) | We use a single counter variable; no additional data structures required |
Because n is at most 50, this solution is extremely efficient and does not require any optimizations beyond a simple linear scan.
Test Cases
# Provided examples
assert Solution().numberOfEmployeesWhoMetTarget([0,1,2,3,4], 2) == 3 # Example 1
assert Solution().numberOfEmployeesWhoMetTarget([5,1,4,2,2], 6) == 0 # Example 2
# Edge cases
assert Solution().numberOfEmployeesWhoMetTarget([0,0,0], 0) == 3 # Target 0, all counted
assert Solution().numberOfEmployeesWhoMetTarget([0,0,0], 1) == 0 # All below target
assert Solution().numberOfEmployeesWhoMetTarget([10,20,30], 10) == 3 # All above or equal
assert Solution().numberOfEmployeesWhoMetTarget([1], 1) == 1 # Single element equal
assert Solution().numberOfEmployeesWhoMetTarget([0], 1) == 0 # Single element below target
assert Solution().numberOfEmployeesWhoMetTarget([105]*50, 105) == 50 # Max value edge case
| Test | Why |
|---|---|
| Example 1 | Standard case with some meeting target and some not |
| Example 2 | Standard case with none meeting target |
| Target 0 | All employees should count, tests zero target edge |
| All below target | Ensures no false positives |
| All above target | Ensures all counted correctly |
| Single element equal | Minimal array, meets target |
| Single element below | Minimal array, below target |
| Max values | Stress test with upper boundary values |
Edge Cases
Case 1: Single employee
With only one employee, the algorithm must correctly handle a list of length 1. The implementation works because the loop handles single-element arrays naturally, incrementing count if the condition is met.
Case 2: Zero target
When target is 0, every non-negative number in hours satisfies the condition. The solution correctly counts all employees by comparing each element against 0.
Case 3: All employees below target
If no employee meets the target, the counter remains 0, ensuring the correct result. The algorithm does not assume any employee meets the target, so it avoids false positives.
Case 4: Maximum allowed values
With hours[i] or target at 105, the algorithm still works because integer comparison and addition are safe within Python and Go integer types. No overflow occurs.