Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
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 summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array
[0,1,2,3,4] 2
[5,1,4,2,2] 6
minimum-operations-to-exceed-threshold-value-i)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2798: Number of Employees Who Met the Target
class Solution {
public int numberOfEmployeesWhoMetTarget(int[] hours, int target) {
int ans = 0;
for (int x : hours) {
if (x >= target) {
++ans;
}
}
return ans;
}
}
// Accepted solution for LeetCode #2798: Number of Employees Who Met the Target
func numberOfEmployeesWhoMetTarget(hours []int, target int) (ans int) {
for _, x := range hours {
if x >= target {
ans++
}
}
return
}
# Accepted solution for LeetCode #2798: Number of Employees Who Met the Target
class Solution:
def numberOfEmployeesWhoMetTarget(self, hours: List[int], target: int) -> int:
return sum(x >= target for x in hours)
// Accepted solution for LeetCode #2798: Number of Employees Who Met the Target
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
}
}
// Accepted solution for LeetCode #2798: Number of Employees Who Met the Target
function numberOfEmployeesWhoMetTarget(hours: number[], target: number): number {
return hours.filter(x => x >= target).length;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.