Python 2798.py
class Solution:
def numberOfEmployeesWhoMetTarget(self, hours: List[int], target: int) -> int:
return sum(1 if h >= target else 0 for h in hours)
Difficulty
and has an acceptance rate of about 88% (200k+ submissions).
Typical patterns to solve this problem:
- Array
AI Disclaimer
However, two years later I set up this website and used various AI tools to generate most of the walkthrough / explanations below.
See this dedicated page for more details.
Code Walkthrough
Overview
The provided implementation defines a single-method class that counts how many elements in a list of work hours meet or exceed a given target. The solution is expressed in a single expression that leverages Python’s built-in sum together with a generator expression. This design keeps the logic compact while still being clear about the intent: iterate over the collection, evaluate a condition for each element, and aggregate the true results.
Key Steps
Step 1 - Receive Input
The method signature declares two parameters: hours, a list of integers representing individual employee work hours, and target, an integer threshold. No additional validation is performed inside the method, which means the function assumes that callers provide a list of numeric types and a comparable integer target.
def numberOfEmployeesWhoMetTarget(self, hours: List[int], target: int) -> int:
Step 2 - Evaluate Condition for Each Element
A generator expression iterates over hours. For each hour value h, the expression evaluates the Boolean condition h >= target. The ternary operator converts the Boolean outcome into an integer: 1 when the condition is true, 0 otherwise. This conversion enables direct summation of the results.
(1 if h >= target else 0 for h in hours)
Step 3 - Aggregate Results
The built-in sum function consumes the generator, adding together the 1s and 0s produced for each element. The final integer returned by sum equals the count of employees whose hours satisfy the target condition.
return sum(1 if h >= target else 0 for h in hours)
Edge Cases Handled
The implementation naturally accommodates several boundary situations without extra code:
- Empty Input List - When
hoursis empty, the generator yields no values, andsumreturns0. This correctly indicates that no employee met the target. - Target Greater Than All Hours - If every
his less thantarget, each ternary evaluation yields0, and the final sum is0. - Target Less Than or Equal to All Hours - When every
hmeets the condition, each evaluation yields1, and the sum equals the length ofhours. - Negative Hours or Target - The comparison
h >= targetworks for negative integers as well, so the function correctly counts entries that satisfy the relational condition regardless of sign. - Large Lists - The generator expression processes elements lazily, so memory consumption remains constant even for very large input lists.
Performance Optimizations
The code exhibits several implicit optimizations:
- Generator Expression - By using a generator rather than constructing an intermediate list, the solution avoids allocating additional memory proportional to the input size. Each element is processed on-the-fly.
- Built-in
sum- Thesumfunction is implemented in C and operates at native speed, providing faster aggregation than a manual Python loop. - Ternary to Integer Mapping - Converting the Boolean result directly to
1or0eliminates the need for a separate counting variable or conditional increment, reducing interpreter overhead. - Short-Circuit Evaluation - The comparison
h >= targetstops evaluating as soon as the relational result is known for each element, which is the most efficient way to decide the outcome for a single integer comparison.
These choices collectively ensure that the method runs with minimal overhead while maintaining readability.
ELI5 - Simplified Explanation
Imagine a group of hard-working bees who fly around gathering nectar. Some bees work for many hours while others take shorter trips. There is a special goal that everyone tries to reach. The code looks at the list of how many hours each bee worked. It checks every single bee one by one to see if they worked as long as the goal number. If a bee worked enough, they get a gold star. The code counts up all the gold stars at the very end to show how many bees met the goal.
Code Quality
Complexity Analysis
As a rule of thumb, the maximum input size for one second execution time is
- factorial: up to 10 elements
- exponential: up to 20 elements
- polynomial: a few thousand elements
- loglinear: up to 100,000 elements
- linear: a few million elements
- logarithmic: a billion elements
- constant: practically unlimited
Other Solutions
Obviously, I did that after I solved the problem myself.
In general, AlgoMonster and WalkCCC solutions are high-quality and always worth a look.
I haven't checked most of the other solutions, but they might be helpful as well.
And of course, the LeetCode forum is always a good place (or even the best) to look for alternative approaches.
(click on a column header to sort the table)
| author | ||||||||
|---|---|---|---|---|---|---|---|---|
| author | solutions | |||||||
| www | AlgoMonster | Java | C++ | Python | TS | |||
| WalkCCC | Java | C++ | Python | |||||
| leetcode.ca | Java | Go | C++ | Python | Rust | TS | ||
| Progiez | Java | C++ | Python | |||||
| github | charles-wangkai | Java | ||||||
| everthis | JS | |||||||
| fsq | C++ | |||||||
| JoshCrozier | JS | |||||||
| muhammadfarhankt | Go | |||||||
| varunu28 | Java | |||||||
| AlgoMonster | C++, Python, Java, TypeScript | |||||||
| WalkCCC | C++, Python, Java | |||||||
| leetcode.ca | C++, Go, Java, Python, Rust, TypeScript | |||||||
| Progiez | C++, Java, Python | |||||||
| charles-wangkai | Java | |||||||
| everthis | JavaScript | |||||||
| fsq | C++ | |||||||
| JoshCrozier | JavaScript | |||||||
| muhammadfarhankt | Go | |||||||
| varunu28 | Java |
Videos
Similar Problems
- problem 3065: Minimum Operations to Exceed Threshold Value I
Solved On The Same Day
- problem 1128: Number of Equivalent Domino Pairs
- problem 1796: Second Largest Digit in a String
- problem 2413: Smallest Even Multiple
- problem 2609: Find the Longest Balanced Substring of a Binary String
- problem 2748: Number of Beautiful Pairs
Extra
- Tech Stack - my development environment and tools
- AI - how AI helped me create this website (NOT solving the problems)
- Progress - progression over time
- Quality - code quality