Count employees who worked at least the target hours
Given a list hours where hours[i] is the hours worked by employee i, and an integer target, return how many employees worked at least target hours (hours[i] >= target).
Examples: hours=[0,1,2,3,4], target=2 → 3; hours=[5,1,4,2,2], target=6 → 0.
def count_target(hours: list[int], target: int) -> int:
# your code here
Write the implementation.
Count the elements meeting the threshold: sum(1 for h in hours if h >= target), or sum(h >= target for h in hours) since booleans count as 0/1. The comparison must be >= — exactly meeting the target counts.
- ✗Using > instead of >= and dropping exact matches
- ✗Assuming the input list is sorted
- ✗Dividing total hours by target instead of counting
- →Why does
>=matter for hitting the target exactly? - →How would you also return who met it, not just how many?
Count the elements that meet the threshold in one pass.
def count_target(hours: list[int], target: int) -> int:
return sum(1 for h in hours if h >= target)
The >= is essential: an employee at exactly target hours has met the norm. For [0,1,2,3,4] with target 2, the values 2, 3, 4 qualify → 3. For [5,1,4,2,2] with target 6, none reach 6 → 0. Booleans are integers in Python, so sum(h >= target for h in hours) is an equivalent one-liner. O(n) time.