Skip to content

Commit 7fa70f1

Browse files
authored
feat: add solutions to lc problems: No.2798~2801 (#1342)
* No.2798.Number of Employees Who Met the Target * No.2799.Count Complete Subarrays in an Array * No.2800.Shortest String That Contains Three Strings * No.2801.Count Stepping Numbers in Range
1 parent faa7685 commit 7fa70f1

35 files changed

+2426
-0
lines changed

solution/2700-2799/2719.Count of Integers/README.md

+4
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@
5858

5959
时间复杂度 $O(10 \times n \times max\_sum)$,空间复杂度 $O(n \times max\_sum)$。其中 $n$ 表示 $num$ 的长度。
6060

61+
相似题目:
62+
63+
- [2801. 统计范围内的步进数字数目](/solution/2800-2899/2801.Count%20Stepping%20Numbers%20in%20Range/README.md)
64+
6165
<!-- tabs:start -->
6266

6367
### **Python3**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
# [2798. 满足目标工作时长的员工数目](https://leetcode.cn/problems/number-of-employees-who-met-the-target)
2+
3+
[English Version](/solution/2700-2799/2798.Number%20of%20Employees%20Who%20Met%20the%20Target/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>公司里共有 <code>n</code> 名员工,按从 <code>0</code> 到 <code>n - 1</code> 编号。每个员工 <code>i</code> 已经在公司工作了 <code>hours[i]</code> 小时。</p>
10+
11+
<p>公司要求每位员工工作&nbsp;<strong>至少</strong> <code>target</code> 小时。</p>
12+
13+
<p>给你一个下标从 <strong>0</strong> 开始、长度为 <code>n</code> 的非负整数数组 <code>hours</code> 和一个非负整数 <code>target</code> 。</p>
14+
15+
<p>请你用整数表示并返回工作至少 <code>target</code> 小时的员工数。</p>
16+
17+
<p>&nbsp;</p>
18+
19+
<p><strong>示例 1:</strong></p>
20+
21+
<pre><strong>输入:</strong>hours = [0,1,2,3,4], target = 2
22+
<strong>输出:</strong>3
23+
<strong>解释:</strong>公司要求每位员工工作至少 2 小时。
24+
- 员工 0 工作 0 小时,不满足要求。
25+
- 员工 1 工作 1 小时,不满足要求。
26+
- 员工 2 工作 2 小时,满足要求。
27+
- 员工 3 工作 3 小时,满足要求。
28+
- 员工 4 工作 4 小时,满足要求。
29+
共有 3 位满足要求的员工。
30+
</pre>
31+
32+
<p><strong>示例 2:</strong></p>
33+
34+
<pre><strong>输入:</strong>hours = [5,1,4,2,2], target = 6
35+
<strong>输出:</strong>0
36+
<strong>解释:</strong>公司要求每位员工工作至少 6 小时。
37+
共有 0 位满足要求的员工。
38+
</pre>
39+
40+
<p>&nbsp;</p>
41+
42+
<p><strong>提示:</strong></p>
43+
44+
<ul>
45+
<li><code>1 &lt;= n == hours.length &lt;= 50</code></li>
46+
<li><code>0 &lt;=&nbsp;hours[i], target &lt;= 10<sup>5</sup></code></li>
47+
</ul>
48+
49+
## 解法
50+
51+
<!-- 这里可写通用的实现逻辑 -->
52+
53+
**方法一:遍历计数**
54+
55+
我们可以遍历数组 $hours$,对于每个员工,如果其工作时长 $x$ 大于等于 $target$,则将计数器 $ans$ 加一。
56+
57+
遍历结束后,返回答案即可。
58+
59+
时间复杂度 $O(n)$,其中 $n$ 是数组 $hours$ 的长度。空间复杂度 $O(1)$。
60+
61+
<!-- tabs:start -->
62+
63+
### **Python3**
64+
65+
<!-- 这里可写当前语言的特殊实现逻辑 -->
66+
67+
```python
68+
class Solution:
69+
def numberOfEmployeesWhoMetTarget(self, hours: List[int], target: int) -> int:
70+
return sum(x >= target for x in hours)
71+
```
72+
73+
### **Java**
74+
75+
<!-- 这里可写当前语言的特殊实现逻辑 -->
76+
77+
```java
78+
class Solution {
79+
public int numberOfEmployeesWhoMetTarget(int[] hours, int target) {
80+
int ans = 0;
81+
for (int x : hours) {
82+
if (x >= target) {
83+
++ans;
84+
}
85+
}
86+
return ans;
87+
}
88+
}
89+
```
90+
91+
### **C++**
92+
93+
```cpp
94+
class Solution {
95+
public:
96+
int numberOfEmployeesWhoMetTarget(vector<int>& hours, int target) {
97+
int ans = 0;
98+
for (int x : hours) {
99+
ans += x >= target;
100+
}
101+
return ans;
102+
}
103+
};
104+
```
105+
106+
### **Go**
107+
108+
```go
109+
func numberOfEmployeesWhoMetTarget(hours []int, target int) (ans int) {
110+
for _, x := range hours {
111+
if x >= target {
112+
ans++
113+
}
114+
}
115+
return
116+
}
117+
```
118+
119+
### **TypeScript**
120+
121+
```ts
122+
function numberOfEmployeesWhoMetTarget(
123+
hours: number[],
124+
target: number,
125+
): number {
126+
let ans = 0;
127+
for (const x of hours) {
128+
if (x >= target) {
129+
++ans;
130+
}
131+
}
132+
return ans;
133+
}
134+
```
135+
136+
### **...**
137+
138+
```
139+
140+
```
141+
142+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
# [2798. Number of Employees Who Met the Target](https://leetcode.com/problems/number-of-employees-who-met-the-target)
2+
3+
[中文文档](/solution/2700-2799/2798.Number%20of%20Employees%20Who%20Met%20the%20Target/README.md)
4+
5+
## Description
6+
7+
<p>There are <code>n</code> employees in a company, numbered from <code>0</code> to <code>n - 1</code>. Each employee <code>i</code> has worked for <code>hours[i]</code> hours in the company.</p>
8+
9+
<p>The company requires each employee to work for <strong>at least</strong> <code>target</code> hours.</p>
10+
11+
<p>You are given a <strong>0-indexed</strong> array of non-negative integers <code>hours</code> of length <code>n</code> and a non-negative integer <code>target</code>.</p>
12+
13+
<p>Return <em>the integer denoting the number of employees who worked at least</em> <code>target</code> <em>hours</em>.</p>
14+
15+
<p>&nbsp;</p>
16+
<p><strong class="example">Example 1:</strong></p>
17+
18+
<pre>
19+
<strong>Input:</strong> hours = [0,1,2,3,4], target = 2
20+
<strong>Output:</strong> 3
21+
<strong>Explanation:</strong> The company wants each employee to work for at least 2 hours.
22+
- Employee 0 worked for 0 hours and didn&#39;t meet the target.
23+
- Employee 1 worked for 1 hours and didn&#39;t meet the target.
24+
- Employee 2 worked for 2 hours and met the target.
25+
- Employee 3 worked for 3 hours and met the target.
26+
- Employee 4 worked for 4 hours and met the target.
27+
There are 3 employees who met the target.
28+
</pre>
29+
30+
<p><strong class="example">Example 2:</strong></p>
31+
32+
<pre>
33+
<strong>Input:</strong> hours = [5,1,4,2,2], target = 6
34+
<strong>Output:</strong> 0
35+
<strong>Explanation:</strong> The company wants each employee to work for at least 6 hours.
36+
There are 0 employees who met the target.
37+
</pre>
38+
39+
<p>&nbsp;</p>
40+
<p><strong>Constraints:</strong></p>
41+
42+
<ul>
43+
<li><code>1 &lt;= n == hours.length &lt;= 50</code></li>
44+
<li><code>0 &lt;=&nbsp;hours[i], target &lt;= 10<sup>5</sup></code></li>
45+
</ul>
46+
47+
## Solutions
48+
49+
<!-- tabs:start -->
50+
51+
### **Python3**
52+
53+
```python
54+
class Solution:
55+
def numberOfEmployeesWhoMetTarget(self, hours: List[int], target: int) -> int:
56+
return sum(x >= target for x in hours)
57+
```
58+
59+
### **Java**
60+
61+
```java
62+
class Solution {
63+
public int numberOfEmployeesWhoMetTarget(int[] hours, int target) {
64+
int ans = 0;
65+
for (int x : hours) {
66+
if (x >= target) {
67+
++ans;
68+
}
69+
}
70+
return ans;
71+
}
72+
}
73+
```
74+
75+
### **C++**
76+
77+
```cpp
78+
class Solution {
79+
public:
80+
int numberOfEmployeesWhoMetTarget(vector<int>& hours, int target) {
81+
int ans = 0;
82+
for (int x : hours) {
83+
ans += x >= target;
84+
}
85+
return ans;
86+
}
87+
};
88+
```
89+
90+
### **Go**
91+
92+
```go
93+
func numberOfEmployeesWhoMetTarget(hours []int, target int) (ans int) {
94+
for _, x := range hours {
95+
if x >= target {
96+
ans++
97+
}
98+
}
99+
return
100+
}
101+
```
102+
103+
### **TypeScript**
104+
105+
```ts
106+
function numberOfEmployeesWhoMetTarget(
107+
hours: number[],
108+
target: number,
109+
): number {
110+
let ans = 0;
111+
for (const x of hours) {
112+
if (x >= target) {
113+
++ans;
114+
}
115+
}
116+
return ans;
117+
}
118+
```
119+
120+
### **...**
121+
122+
```
123+
124+
```
125+
126+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution {
2+
public:
3+
int numberOfEmployeesWhoMetTarget(vector<int>& hours, int target) {
4+
int ans = 0;
5+
for (int x : hours) {
6+
ans += x >= target;
7+
}
8+
return ans;
9+
}
10+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
func numberOfEmployeesWhoMetTarget(hours []int, target int) (ans int) {
2+
for _, x := range hours {
3+
if x >= target {
4+
ans++
5+
}
6+
}
7+
return
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution {
2+
public int numberOfEmployeesWhoMetTarget(int[] hours, int target) {
3+
int ans = 0;
4+
for (int x : hours) {
5+
if (x >= target) {
6+
++ans;
7+
}
8+
}
9+
return ans;
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Solution:
2+
def numberOfEmployeesWhoMetTarget(self, hours: List[int], target: int) -> int:
3+
return sum(x >= target for x in hours)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
function numberOfEmployeesWhoMetTarget(
2+
hours: number[],
3+
target: number,
4+
): number {
5+
let ans = 0;
6+
for (const x of hours) {
7+
if (x >= target) {
8+
++ans;
9+
}
10+
}
11+
return ans;
12+
}

0 commit comments

Comments
 (0)