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.
In this approach, we iterate through the list of hours and count how many employees met the target. For each employee, we check if the number of hours they worked is at least the target. If it is, we increment the counter. This straightforward approach gives us the result directly.
In this C solution, we define a function countEmployees() that iterates through the provided hours array, checking each value against target. If the condition hours[i] >= target is met, we increment the count. The total count is returned, representing the number of employees who met or exceeded the target.
Time Complexity: O(n), where n is the number of employees.
Space Complexity: O(1), as no extra space is used beyond a few variables.
This approach leverages the capabilities of functional programming languages by using filtering, which applies a condition to each element of the list and retains only those that satisfy the condition (i.e., hours worked are at least the target). We then return the count of the filtered list as the result.
Using a generator expression, this Python approach filters the list of hours based on whether each hour meets or exceeds the target. The sum function counts the number of true conditions, equating to the number of employees who met the criteria.
Python
JavaScript
Time Complexity: O(n), iterating over the list of employees.
Space Complexity: O(1), with only a temporary generator expression used.
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.
After the iteration, we return the answer.
The time complexity is O(n), where n is the length of the array hours. The space complexity is O(1).
| Approach | Complexity |
|---|---|
| Iterative Approach | Time Complexity: O(n), where n is the number of employees. |
| Functional Programming Approach with Filtering | Time Complexity: O(n), iterating over the list of employees. |
| Iteration and Counting | — |
| 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. |
Leetcode | 2798. Number of Employees Who Met the Target | Easy | Java Solution • Developer Docs • 613 views views
Watch 9 more video solutions →Practice Number of Employees Who Met the Target with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor