Watch 10 video solutions for Number of Employees Who Met the Target, a easy level problem involving Array. This walkthrough by Developer Docs has 613 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
There are n employees in a company, numbered from 0 to n - 1. Each employee i has worked for hours[i] hours in the company.
The company requires each employee to work for at least target hours.
You are given a 0-indexed array of non-negative integers hours of length n and a non-negative integer target.
Return the integer denoting the number of employees who worked at least target hours.
Example 1:
Input: hours = [0,1,2,3,4], target = 2 Output: 3 Explanation: 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 Explanation: 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 <= 500 <= hours[i], target <= 105Problem Overview: You receive an integer array hours where each value represents the number of hours an employee worked. Given a target value, count how many employees worked at least that many hours.
Approach 1: Iterative Counting (O(n) time, O(1) space)
The most direct solution is a single pass through the array. Iterate over each value in hours and check whether it is greater than or equal to target. If the condition is true, increment a counter. This works because the problem only requires counting valid elements, not storing them or modifying the array.
The key idea is simple conditional evaluation during traversal. Every element is checked exactly once, so the runtime grows linearly with the number of employees. No extra data structures are required, which keeps memory usage constant. This pattern appears frequently in array problems where you filter elements based on a condition.
This approach is typically what interviewers expect for an easy counting problem. It demonstrates that you can recognize when a straightforward linear scan is sufficient instead of overengineering a solution.
Approach 2: Functional Filtering (O(n) time, O(n) space)
Languages like Python and JavaScript support functional-style operations such as filter. Instead of manually managing a counter, you filter the array to keep only elements that satisfy hours[i] >= target, then compute the length of the filtered result.
The logic is identical to the iterative solution, but expressed declaratively. For example, Python can use len(list(filter(...))), while JavaScript can use hours.filter(h => h >= target).length. These constructs rely on built-in iteration internally.
The tradeoff is memory usage. The filter operation creates a temporary list containing all qualifying elements, which increases space complexity to O(n). Despite this, many developers prefer the readability and conciseness of functional style. It commonly appears in problems involving array processing and functional programming patterns.
Recommended for interviews: The iterative counting approach. It runs in O(n) time with O(1) extra space and clearly demonstrates control over loops and conditional checks. Showing the functional version afterward can highlight familiarity with modern language features, but the manual iteration proves stronger algorithmic fundamentals.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Iterative Counting | O(n) | O(1) | Best general solution. Minimal memory usage and expected in interviews. |
| Functional Filtering | O(n) | O(n) | When using expressive functional features in Python or JavaScript and readability is preferred. |