Problem 2798

Abstract / Summary

Count employees whose monthly hours worked meet or exceed a given target.

Note

The full problem description, including helpful examples, can be found on LeetCode.

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)
3 lines | 161 bytes | submitted December 10, 2023 | GitHub   tab size 4 | low complexity

Difficulty

This problem is ranked as
easy medium hard

and has an acceptance rate of about 88% (200k+ submissions).

Typical patterns to solve this problem:
  • Array
I solved 663 easy, 1250 medium and 309 hard problems

AI Disclaimer

Every single line of code was written by me, 100% from scratch, between August 2023 and August 2024.
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.
LeetCode profile | GitHub repo

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 hours is empty, the generator yields no values, and sum returns 0. This correctly indicates that no employee met the target.
  • Target Greater Than All Hours - If every h is less than target, each ternary evaluation yields 0, and the final sum is 0.
  • Target Less Than or Equal to All Hours - When every h meets the condition, each evaluation yields 1, and the sum equals the length of hours.
  • Negative Hours or Target - The comparison h >= target works 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 - The sum function 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 1 or 0 eliminates the need for a separate counting variable or conditional increment, reducing interpreter overhead.
  • Short-Circuit Evaluation - The comparison h >= target stops 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.

written by openai gpt

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.

written by google gemini

Code Quality

Several LLMs ranked my code quality on a scale from 1 (very poor) to 10 (outstanding):
n = 27 | median = 8 | μ = 8.33 | σ = 0.77

Complexity Analysis

Time: linear
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
generated automatically

Other Solutions

I wrote a basic crawler to find other solutions to that problem.
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
22 links | 10 authors | 7 languages

Videos

4 videos   did my crawler come up with invalid videos ? drop me a line.

Similar Problems

1 similar problem

Solved On The Same Day

6 solutions   December 10, 2023

Extra

There are a few extra pages giving you some insights into my setup:
  • 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