You are given a list of integers, hours
, where each integer represents the number of hours an employee has worked in a week. You are also given an integer target
which represents the target number of hours an employee needs to meet or exceed. Your task is to write a function that returns the number of employees who have met or exceeded the target number of hours.
Function Signature:
def number_of_employees_met_target(hours: List[int], target: int) -> int:
hours
list contain negative numbers?
hours
list?
target
be zero?
target
can be zero, indicating that any number of hours worked would meet the target.hours
list.hours
that are greater than or equal to target
.This approach is straightforward and leverages simple iteration and comparison, ensuring an efficient solution.
hours
list. This is because we need to iterate through the list once.from typing import List
def number_of_employees_met_target(hours: List[int], target: int) -> int:
count = 0
for hour in hours:
if hour >= target:
count += 1
return count
# Example usage
hours = [40, 35, 50, 45, 30]
target = 40
print(number_of_employees_met_target(hours, target)) # Output should be 3
This function number_of_employees_met_target
will iterate through the hours
list, count every hour value that meets or exceeds the target
, and return this count. Here, we assume the hours
list and target
are given as input parameters.
For hours = [40, 35, 50, 45, 30]
and target = 40
:
hours
.40 >= 40
-> count += 1 (count = 1)35 < 40
-> count unchanged50 >= 40
-> count += 1 (count = 2)45 >= 40
-> count += 1 (count = 3)30 < 40
-> count unchangedThe function returns 3, indicating 3 employees have met or exceeded the target of 40 hours.
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?