#2798
Number of Employees Who Met the Target
EasyArrayArrayCounting
Approaches
Brute ForceOptimal
Complexity Comparison
| Brute Force | Optimal Solution★ | |
|---|---|---|
| Time | O(n) | O(n) |
| Space | O(1) | O(1) |
💡
Intuition
Time O(n)Space O(1)
The optimal solution is essentially the same as the brute-force approach but emphasizes efficiency. Given the constraints, we can still iterate through the list once, which is efficient and straightforward.
⚙️
Algorithm
4 steps- 1Step 1: Initialize a count variable to zero.
- 2Step 2: Loop through each element in the hours array.
- 3Step 3: If the current employee's hours are greater than or equal to the target, increment the count.
- 4Step 4: Return the count after the loop.
solution.py11 lines
1# Full working Python code
2
3def countEmployees(hours, target):
4 count = 0
5 for hour in hours:
6 if hour >= target:
7 count += 1
8 return count
9
10# Example usage
11print(countEmployees([5, 1, 4, 2, 2], 6)) # Output: 0ℹ
Complexity note: The time complexity remains O(n) as we still iterate through the list once, and the space complexity is O(1) since we only use a counter.
- 1Iterating through the array is a direct way to solve the problem.
- 2The problem can be solved efficiently with a single pass through the data.
Solutions and explanations are original Tejav content. Problem titles © LeetCode — use the LeetCode button above for the full problem statement.