Number of Employees Who Met the Target
EasyPrompt
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 <= 105
Approaches
2 approaches with complexity analysis and trade-offs.
This approach involves first sorting the hours array. Once sorted, we can efficiently find the number of employees who met the target. After finding the first employee who meets the criteria in the sorted list, all subsequent employees will also meet it, allowing for a quick calculation of the total count.
Algorithm
- Sort the input array
hoursin non-decreasing order. - Iterate through the sorted
hoursarray from the beginning. - Find the first index
iwherehours[i]is greater than or equal to thetarget. - The number of employees who met the target is
n - i, wherenis the total number of employees. Return this value. - If the loop finishes without finding such an employee, return 0.
Walkthrough
The core idea is that if we sort the hours worked by employees in ascending order, all employees who met the target will form a contiguous block at the end of the array. By finding the start of this block, we can determine its size.
Algorithm
- Sort the input array
hoursin non-decreasing order. - Iterate through the sorted
hoursarray from the beginning. - Find the first index
iwherehours[i]is greater than or equal to thetarget. - Once this index
iis found, we know that all elements from this index to the end of the array meet the target. The total count is therefore the total number of employeesnminus the current indexi(i.e.,n - i). - Return this count immediately.
- If the loop completes without finding any employee who met the target, it means no one did, so we return 0.
Code Snippet
import java.util.Arrays; class Solution { public int numberOfEmployeesWhoMetTarget(int[] hours, int target) { Arrays.sort(hours); int n = hours.length; for (int i = 0; i < n; i++) { if (hours[i] >= target) { return n - i; } } return 0; }}Complexity
Time
O(n log n), where n is the number of employees. The dominant operation is sorting the `hours` array. The subsequent linear scan to find the first element meeting the target takes at most O(n) time.
Space
O(log n) to O(n). The space complexity depends on the sorting algorithm used. In Java, `Arrays.sort()` for primitive types uses a dual-pivot quicksort, which has an average space complexity of O(log n) for the recursion stack. The worst-case space complexity can be O(n).
Trade-offs
Pros
After sorting, a binary search could also be used to find the first valid employee, which could be slightly faster in practice for the search part (O(log n)) than a linear scan, though the overall complexity remains O(n log n).
Cons
The time complexity of O(n log n) is suboptimal for this problem.
It modifies the input array in-place. If the original order of the array needs to be preserved, a copy must be made, which would increase the space complexity to O(n).
This is the most straightforward and optimal approach. We can solve the problem by iterating through the hours array just once. We maintain a counter, and for each employee, we check if their hours worked are greater than or equal to the target. If they are, we increment the counter.
Algorithm
- Initialize a counter
countto 0. - Iterate through each
hourin thehoursarray. - If
houris greater than or equal totarget, incrementcount. - After the loop, return
count.
Walkthrough
The problem asks for a simple count of elements that satisfy a condition. There is no need for any pre-processing like sorting. A single pass through the array is sufficient to gather the required information. This approach directly translates the problem statement into code.
Algorithm
- Initialize a counter variable,
count, to 0. - Iterate through each element
hin thehoursarray. - For each
h, check if it is greater than or equal to thetarget. - If the condition
h >= targetis true, increment thecount. - After the loop has processed all elements in the array, return the final
count.
This logic can be implemented using a standard for-each loop for simplicity and readability, or with Java Streams for a more functional style.
Code Snippet (For-Each Loop)
class Solution { public int numberOfEmployeesWhoMetTarget(int[] hours, int target) { int count = 0; for (int h : hours) { if (h >= target) { count++; } } return count; }}Code Snippet (Java Streams)
import java.util.Arrays; class Solution { public int numberOfEmployeesWhoMetTarget(int[] hours, int target) { return (int) Arrays.stream(hours) .filter(h -> h >= target) .count(); }}Complexity
Time
O(n), where n is the number of employees. We must visit each element of the `hours` array exactly once to check if it meets the target.
Space
O(1). We only use a constant amount of extra space for the counter variable, regardless of the input size.
Trade-offs
Pros
Optimal time complexity, as every element must be checked at least once.
Optimal space complexity, using only a single extra variable.
Simple, intuitive, and easy to implement.
Does not modify the input array.
Cons
There are no significant disadvantages to this approach as it is the optimal solution for this problem.
Solutions
Solution
class Solution {public: int numberOfEmployeesWhoMetTarget(vector<int> &hours, int target) { int ans = 0; for (int x : hours) { ans += x >= target; } return ans; }};class Solution: def numberOfEmployeesWhoMetTarget( self, hours: List[int], target: int) -> int: return sum(x >= target for x in hours)class Solution {public int numberOfEmployeesWhoMetTarget(int[] hours, int target) { int ans = 0; for (int x : hours) { if (x >= target) { ++ans; } } return ans; }}Video walkthrough
Newsletter
One sharp idea, every week
System design and interview prep — short enough to finish.
No spam. Unsubscribe anytime.
Practice
Same difficulty — related problems to reinforce the pattern.