LeetCode 2798: Number of Employees Who Met the Target (Single Pass Counting)
LeetCode 2798ArrayCountingSimulationToday we solve LeetCode 2798 - Number of Employees Who Met the Target.
Source: https://leetcode.com/problems/number-of-employees-who-met-the-target/
English
Problem Summary
Given an integer array hours and an integer target, count how many employees worked at least target hours.
Key Insight
This is a direct counting problem. For each value in hours, if h >= target, increment the answer by 1.
Algorithm
1) Initialize count = 0.
2) Traverse each h in hours.
3) If h >= target, do count++.
4) Return count.
Complexity Analysis
Time: O(n), where n is the length of hours.
Space: O(1).
Reference Implementations (Java / Go / C++ / Python / JavaScript)
class Solution {
public int numberOfEmployeesWhoMetTarget(int[] hours, int target) {
int count = 0;
for (int h : hours) {
if (h >= target) {
count++;
}
}
return count;
}
}func numberOfEmployeesWhoMetTarget(hours []int, target int) int {
count := 0
for _, h := range hours {
if h >= target {
count++
}
}
return count
}class Solution {
public:
int numberOfEmployeesWhoMetTarget(vector<int>& hours, int target) {
int count = 0;
for (int h : hours) {
if (h >= target) {
++count;
}
}
return count;
}
};class Solution:
def numberOfEmployeesWhoMetTarget(self, hours: list[int], target: int) -> int:
count = 0
for h in hours:
if h >= target:
count += 1
return countvar numberOfEmployeesWhoMetTarget = function(hours, target) {
let count = 0;
for (const h of hours) {
if (h >= target) {
count++;
}
}
return count;
};中文
题目概述
给定整数数组 hours 和整数 target,统计工作时长至少为 target 的员工数量。
核心思路
这是标准计数题。遍历 hours,只要某个值 h >= target,答案加一。
算法步骤
1)初始化 count = 0。
2)遍历数组中的每个工时 h。
3)若 h >= target,则 count++。
4)返回 count。
复杂度分析
时间复杂度:O(n),n 为数组长度。
空间复杂度:O(1)。
多语言参考实现(Java / Go / C++ / Python / JavaScript)
class Solution {
public int numberOfEmployeesWhoMetTarget(int[] hours, int target) {
int count = 0;
for (int h : hours) {
if (h >= target) {
count++;
}
}
return count;
}
}func numberOfEmployeesWhoMetTarget(hours []int, target int) int {
count := 0
for _, h := range hours {
if h >= target {
count++
}
}
return count
}class Solution {
public:
int numberOfEmployeesWhoMetTarget(vector<int>& hours, int target) {
int count = 0;
for (int h : hours) {
if (h >= target) {
++count;
}
}
return count;
}
};class Solution:
def numberOfEmployeesWhoMetTarget(self, hours: list[int], target: int) -> int:
count = 0
for h in hours:
if h >= target:
count += 1
return countvar numberOfEmployeesWhoMetTarget = function(hours, target) {
let count = 0;
for (const h of hours) {
if (h >= target) {
count++;
}
}
return count;
};
Comments