Number of Employees Who Met the Target solution and explanation
9 more videos on this problem
Leetcode | 2798. Number of Employees Who Met the Target | Easy | Java SolutionDeveloper Docs · 3:30 · 695 views
2798. Number of Employees Who Met the Target (Leetcode Easy)Programming Live with Larry · 1:24 · 507 views
2798. Number of Employees Who Met the Target | LEETCODE EASYcode Explainer · 2:05 · 463 views
Leetcode 2798 Number of Employees Who Met the TargetTech Diet · 1:31 · 428 views
Leetcode Problem 2798. Number of Employees Who Met the Target. Leetcode Solution 2798 EASY. JavaCodingForBeginners · 3:39 · 408 views
Number of Employees Who Met the Target| LeetCode 2798 | CppCodeClips with Abhishek Ranjan · 2:01 · 329 views
Leetcode Weekly contest 356 - Easy - Number of Employees Who Met the TargetPrakhar Agrawal · 2:17 · 279 views
Leetcode Poblem 2798. Number of Employees Who Met the Target.Solution with python.Codium · 2:50 · 142 views
C++ Beginner Walkthrough - LeetCode #2798 Number of Employees Who Met The TargetJoshProgramin · 1:56 · 130 views
Coding Interview With LeetCode 2798. Number of Employees Who Met the Target in PythonvanAmsen · 3:41 · 118 views
The problem
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
Approach overview
Problem 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 1: Iterative Approach
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.
def count_employees(hours, target):
count = 0
for hour in hours:
if hour >= target:
count += 1
return count
hours = [0, 1, 2, 3, 4]
target = 2
print(count_employees(hours, target))public class Main {
public static int countEmployees(int[] hours, int target) {
int count = 0;
for (int hour : hours) {
if (hour >= target) {
count++;
}
}
return count;
}
public static void main(String[] args) {
int[] hours = {0, 1, 2, 3, 4};
int target = 2;
System.out.println(countEmployees(hours, target));
}
}#include <iostream>
#include <vector>
int countEmployees(const std::vector<int>& hours, int target) {
int count = 0;
for (int hour : hours) {
if (hour >= target) {
count++;
}
}
return count;
}
int main() {
std::vector<int> hours = {0, 1, 2, 3, 4};
int target = 2;
std::cout << countEmployees(hours, target);
return 0;
}function countEmployees(hours, target) {
let count = 0;
for (let hour of hours) {
if (hour >= target) {
count++;
}
}
return count;
}
const hours = [0, 1, 2, 3, 4];
const target = 2;
console.log(countEmployees(hours, target));#include <stdio.h>
int countEmployees(int* hours, int n, int target) {
int count = 0;
for (int i = 0; i < n; i++) {
if (hours[i] >= target) {
count++;
}
}
return count;
}
int main() {
int hours[] = {0, 1, 2, 3, 4};
int target = 2;
int n = sizeof(hours) / sizeof(hours[0]);
printf("%d", countEmployees(hours, n, target));
return 0;
}using System;
class Program {
static int CountEmployees(int[] hours, int target) {
int count = 0;
foreach (int hour in hours) {
if (hour >= target) {
count++;
}
}
return count;
}
static void Main() {
int[] hours = {0, 1, 2, 3, 4};
int target = 2;
Console.WriteLine(CountEmployees(hours, target));
}
}This Python function count_employees iterates through the list of hours. If an hour value meets or exceeds target, count is incremented and returned. The result is printed.
Time Complexity: O(n), as we loop through the list of hours.
Space Complexity: O(1), using only a constant amount of additional memory.
Approach 2: Functional Programming Approach with Filtering
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.
def count_employees(hours, target):
return sum(1 for hour in hours if hour >= target)
hours = [0, 1, 2, 3, 4]
target = 2
print(count_employees(hours, target))function countEmployees(hours, target) {
return hours.filter(hour => hour >= target).length;
}
const hours = [0, 1, 2, 3, 4];
const target = 2;
console.log(countEmployees(hours, target));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.
Time Complexity: O(n), iterating over the list of employees.
Space Complexity: O(1), with only a temporary generator expression used.
Approach 3: Iteration and Counting
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).
function numberOfEmployeesWhoMetTarget(hours: number[], target: number): number {
return hours.filter(x => x >= target).length;
}func numberOfEmployeesWhoMetTarget(hours []int, target int) (ans int) {
for _, x := range hours {
if x >= target {
ans++
}
}
return
}impl Solution {
pub fn number_of_employees_who_met_target(hours: Vec<i32>, target: i32) -> i32 {
hours.iter().filter(|&x| *x >= target).count() as i32
}
}Complexity comparison
| Approach | Complexity |
|---|---|
| Iterative Approach | Time Complexity: O(n), as we loop through the list of hours. |
| Functional Programming Approach with Filtering | Time Complexity: O(n), iterating over the list of employees. |
| Iteration and Counting | – |
Complexity analysis
| 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. |
Frequently asked questions
Is Number of Employees Who Met the Target easy or hard?
Number of Employees Who Met the Target is categorized as an Easy problem. It focuses on basic array traversal and conditional checks, making it suitable for beginners practicing simple counting patterns in algorithms.
Number of Employees Who Met the Target Python/Java solution
Both Python and Java implementations follow the same idea: iterate through the array and count values greater than or equal to the target. Python may also use a filter expression, while Java typically uses a loop with a counter variable. Both approaches run in O(n) time.
How to solve Number of Employees Who Met the Target in O(n)?
Iterate through the hours array and compare each value with the target. Maintain a counter that increments whenever hours[i] >= target. After scanning the entire array, return the counter. This requires one linear pass and constant extra memory.
What is the best approach for Number of Employees Who Met the Target?
The best approach is a single-pass iterative count. Traverse the hours array and increment a counter whenever hours[i] >= target. This solution runs in O(n) time and O(1) space, which is optimal because every element must be inspected at least once.
Is Number of Employees Who Met the Target asked at Google/Amazon/Meta?
Problems of this style frequently appear in screening rounds at large tech companies because they test basic array traversal and conditional logic. While the exact problem may not always appear, similar counting and filtering array problems are common in interviews at companies like Amazon and Google.
What data structure is used in Number of Employees Who Met the Target?
The primary data structure is an array (or list). The algorithm simply scans the array and counts elements that satisfy a condition, so no additional structures like hash maps or stacks are required.
What is the time complexity of Number of Employees Who Met the Target?
The time complexity is O(n), where n is the number of employees in the hours array. Each element is checked exactly once against the target value. The optimal iterative solution also uses O(1) additional space.
