Skip to content

Commit 30ca343

Browse files
authoredSep 1, 2024
feat: update solutions to lc problem: No.1450 (#3464)
No.1450.Number of Students Doing Homework at a Given Time
1 parent d207f72 commit 30ca343

File tree

11 files changed

+59
-292
lines changed

11 files changed

+59
-292
lines changed
 

‎solution/1400-1499/1450.Number of Students Doing Homework at a Given Time/README.md

+20-123
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,13 @@ tags:
7878

7979
<!-- solution:start -->
8080

81-
### 方法一:遍历计数
81+
### 方法一:直接遍历
8282

83-
同时遍历 $startTime$ 和 $endTime$,统计正在做作业的学生人数
83+
我们可以直接遍历两个数组,对于每个学生,判断 $\textit{queryTime}$ 是否在他们的作业时间区间内,若是,答案加一
8484

85-
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 是 $startTime$ 和 $endTime$ 的长度。
85+
遍历结束后,返回答案即可。
86+
87+
时间复杂度 $O(n)$,其中 $n$ 为学生数量。空间复杂度 $O(1)$。
8688

8789
<!-- tabs:start -->
8890

@@ -93,7 +95,7 @@ class Solution:
9395
def busyStudent(
9496
self, startTime: List[int], endTime: List[int], queryTime: int
9597
) -> int:
96-
return sum(a <= queryTime <= b for a, b in zip(startTime, endTime))
98+
return sum(x <= queryTime <= y for x, y in zip(startTime, endTime))
9799
```
98100

99101
#### Java
@@ -130,15 +132,13 @@ public:
130132
#### Go
131133
132134
```go
133-
func busyStudent(startTime []int, endTime []int, queryTime int) int {
134-
ans := 0
135-
for i, a := range startTime {
136-
b := endTime[i]
137-
if a <= queryTime && queryTime <= b {
135+
func busyStudent(startTime []int, endTime []int, queryTime int) (ans int) {
136+
for i, x := range startTime {
137+
if x <= queryTime && queryTime <= endTime[i] {
138138
ans++
139139
}
140140
}
141-
return ans
141+
return
142142
}
143143
```
144144

@@ -147,13 +147,13 @@ func busyStudent(startTime []int, endTime []int, queryTime int) int {
147147
```ts
148148
function busyStudent(startTime: number[], endTime: number[], queryTime: number): number {
149149
const n = startTime.length;
150-
let res = 0;
150+
let ans = 0;
151151
for (let i = 0; i < n; i++) {
152-
if (startTime[i] <= queryTime && endTime[i] >= queryTime) {
153-
res++;
152+
if (startTime[i] <= queryTime && queryTime <= endTime[i]) {
153+
ans++;
154154
}
155155
}
156-
return res;
156+
return ans;
157157
}
158158
```
159159

@@ -162,13 +162,13 @@ function busyStudent(startTime: number[], endTime: number[], queryTime: number):
162162
```rust
163163
impl Solution {
164164
pub fn busy_student(start_time: Vec<i32>, end_time: Vec<i32>, query_time: i32) -> i32 {
165-
let mut res = 0;
165+
let mut ans = 0;
166166
for i in 0..start_time.len() {
167167
if start_time[i] <= query_time && end_time[i] >= query_time {
168-
res += 1;
168+
ans += 1;
169169
}
170170
}
171-
res
171+
ans
172172
}
173173
}
174174
```
@@ -177,116 +177,13 @@ impl Solution {
177177

178178
```c
179179
int busyStudent(int* startTime, int startTimeSize, int* endTime, int endTimeSize, int queryTime) {
180-
int res = 0;
180+
int ans = 0;
181181
for (int i = 0; i < startTimeSize; i++) {
182182
if (startTime[i] <= queryTime && endTime[i] >= queryTime) {
183-
res++;
184-
}
185-
}
186-
return res;
187-
}
188-
```
189-
190-
<!-- tabs:end -->
191-
192-
<!-- solution:end -->
193-
194-
<!-- solution:start -->
195-
196-
### 方法二:差分数组
197-
198-
差分数组可以 $O(1)$ 时间处理区间加减操作。例如,对区间 $[l, r]$ 中的每个数加上 $c$。
199-
200-
假设数组 $a$ 的所有元素分别为 $a[1], a[2], ... a[n]$,则差分数组 $b$ 的元素 $b[i]=a[i]-a[i-1]$。
201-
202-
$$
203-
\begin{cases}
204-
b[1]=a[1]\\
205-
b[2]=a[2]-a[1]\\
206-
b[3]=a[3]-a[2]\\
207-
...\\
208-
b[i]=a[i]-a[i-1]\\
209-
\end{cases}
210-
$$
211-
212-
那么 $a[i]=b[1]+b[2]+...+b[i]$,原数组 $a$ 是差分数组 $b$ 的前缀和。
213-
214-
在这道题中,我们定义差分数组 $c$,然后遍历两个数组中对应位置的两个数 $a$, $b$,则 $c[a]+=1$, $c[b+1]-=1$。
215-
216-
遍历结束后,对差分数组 $c$ 进行求前缀和操作,即可得到 $queryTime$ 时刻正在做作业的学生人数。
217-
218-
时间复杂度 $O(n+queryTime)$,空间复杂度 $O(1010)$。
219-
220-
<!-- tabs:start -->
221-
222-
#### Python3
223-
224-
```python
225-
class Solution:
226-
def busyStudent(
227-
self, startTime: List[int], endTime: List[int], queryTime: int
228-
) -> int:
229-
c = [0] * 1010
230-
for a, b in zip(startTime, endTime):
231-
c[a] += 1
232-
c[b + 1] -= 1
233-
return sum(c[: queryTime + 1])
234-
```
235-
236-
#### Java
237-
238-
```java
239-
class Solution {
240-
public int busyStudent(int[] startTime, int[] endTime, int queryTime) {
241-
int[] c = new int[1010];
242-
for (int i = 0; i < startTime.length; ++i) {
243-
c[startTime[i]]++;
244-
c[endTime[i] + 1]--;
245-
}
246-
int ans = 0;
247-
for (int i = 0; i <= queryTime; ++i) {
248-
ans += c[i];
249-
}
250-
return ans;
251-
}
252-
}
253-
```
254-
255-
#### C++
256-
257-
```cpp
258-
class Solution {
259-
public:
260-
int busyStudent(vector<int>& startTime, vector<int>& endTime, int queryTime) {
261-
vector<int> c(1010);
262-
for (int i = 0; i < startTime.size(); ++i) {
263-
c[startTime[i]]++;
264-
c[endTime[i] + 1]--;
265-
}
266-
int ans = 0;
267-
for (int i = 0; i <= queryTime; ++i) {
268-
ans += c[i];
183+
ans++;
269184
}
270-
return ans;
271185
}
272-
};
273-
```
274-
275-
#### Go
276-
277-
```go
278-
func busyStudent(startTime []int, endTime []int, queryTime int) int {
279-
c := make([]int, 1010)
280-
for i, a := range startTime {
281-
b := endTime[i]
282-
c[a]++
283-
c[b+1]--
284-
}
285-
ans := 0
286-
for i := 0; i <= queryTime; i++ {
287-
ans += c[i]
288-
}
289-
return ans
186+
return ans;
290187
}
291188
```
292189

‎solution/1400-1499/1450.Number of Students Doing Homework at a Given Time/README_EN.md

+22-99
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,13 @@ The third student started doing homework at time 3 and finished at time 7 and wa
6060

6161
<!-- solution:start -->
6262

63-
### Solution 1
63+
### Solution 1: Direct Traversal
64+
65+
We can directly traverse the two arrays. For each student, we check if $\textit{queryTime}$ is within their homework time interval. If it is, we increment the answer by one.
66+
67+
After the traversal, we return the answer.
68+
69+
The time complexity is $O(n)$, where $n$ is the number of students. The space complexity is $O(1)$.
6470

6571
<!-- tabs:start -->
6672

@@ -71,7 +77,7 @@ class Solution:
7177
def busyStudent(
7278
self, startTime: List[int], endTime: List[int], queryTime: int
7379
) -> int:
74-
return sum(a <= queryTime <= b for a, b in zip(startTime, endTime))
80+
return sum(x <= queryTime <= y for x, y in zip(startTime, endTime))
7581
```
7682

7783
#### Java
@@ -108,15 +114,13 @@ public:
108114
#### Go
109115
110116
```go
111-
func busyStudent(startTime []int, endTime []int, queryTime int) int {
112-
ans := 0
113-
for i, a := range startTime {
114-
b := endTime[i]
115-
if a <= queryTime && queryTime <= b {
117+
func busyStudent(startTime []int, endTime []int, queryTime int) (ans int) {
118+
for i, x := range startTime {
119+
if x <= queryTime && queryTime <= endTime[i] {
116120
ans++
117121
}
118122
}
119-
return ans
123+
return
120124
}
121125
```
122126

@@ -125,13 +129,13 @@ func busyStudent(startTime []int, endTime []int, queryTime int) int {
125129
```ts
126130
function busyStudent(startTime: number[], endTime: number[], queryTime: number): number {
127131
const n = startTime.length;
128-
let res = 0;
132+
let ans = 0;
129133
for (let i = 0; i < n; i++) {
130-
if (startTime[i] <= queryTime && endTime[i] >= queryTime) {
131-
res++;
134+
if (startTime[i] <= queryTime && queryTime <= endTime[i]) {
135+
ans++;
132136
}
133137
}
134-
return res;
138+
return ans;
135139
}
136140
```
137141

@@ -140,13 +144,13 @@ function busyStudent(startTime: number[], endTime: number[], queryTime: number):
140144
```rust
141145
impl Solution {
142146
pub fn busy_student(start_time: Vec<i32>, end_time: Vec<i32>, query_time: i32) -> i32 {
143-
let mut res = 0;
147+
let mut ans = 0;
144148
for i in 0..start_time.len() {
145149
if start_time[i] <= query_time && end_time[i] >= query_time {
146-
res += 1;
150+
ans += 1;
147151
}
148152
}
149-
res
153+
ans
150154
}
151155
}
152156
```
@@ -155,94 +159,13 @@ impl Solution {
155159

156160
```c
157161
int busyStudent(int* startTime, int startTimeSize, int* endTime, int endTimeSize, int queryTime) {
158-
int res = 0;
162+
int ans = 0;
159163
for (int i = 0; i < startTimeSize; i++) {
160164
if (startTime[i] <= queryTime && endTime[i] >= queryTime) {
161-
res++;
165+
ans++;
162166
}
163167
}
164-
return res;
165-
}
166-
```
167-
168-
<!-- tabs:end -->
169-
170-
<!-- solution:end -->
171-
172-
<!-- solution:start -->
173-
174-
### Solution 2
175-
176-
<!-- tabs:start -->
177-
178-
#### Python3
179-
180-
```python
181-
class Solution:
182-
def busyStudent(
183-
self, startTime: List[int], endTime: List[int], queryTime: int
184-
) -> int:
185-
c = [0] * 1010
186-
for a, b in zip(startTime, endTime):
187-
c[a] += 1
188-
c[b + 1] -= 1
189-
return sum(c[: queryTime + 1])
190-
```
191-
192-
#### Java
193-
194-
```java
195-
class Solution {
196-
public int busyStudent(int[] startTime, int[] endTime, int queryTime) {
197-
int[] c = new int[1010];
198-
for (int i = 0; i < startTime.length; ++i) {
199-
c[startTime[i]]++;
200-
c[endTime[i] + 1]--;
201-
}
202-
int ans = 0;
203-
for (int i = 0; i <= queryTime; ++i) {
204-
ans += c[i];
205-
}
206-
return ans;
207-
}
208-
}
209-
```
210-
211-
#### C++
212-
213-
```cpp
214-
class Solution {
215-
public:
216-
int busyStudent(vector<int>& startTime, vector<int>& endTime, int queryTime) {
217-
vector<int> c(1010);
218-
for (int i = 0; i < startTime.size(); ++i) {
219-
c[startTime[i]]++;
220-
c[endTime[i] + 1]--;
221-
}
222-
int ans = 0;
223-
for (int i = 0; i <= queryTime; ++i) {
224-
ans += c[i];
225-
}
226-
return ans;
227-
}
228-
};
229-
```
230-
231-
#### Go
232-
233-
```go
234-
func busyStudent(startTime []int, endTime []int, queryTime int) int {
235-
c := make([]int, 1010)
236-
for i, a := range startTime {
237-
b := endTime[i]
238-
c[a]++
239-
c[b+1]--
240-
}
241-
ans := 0
242-
for i := 0; i <= queryTime; i++ {
243-
ans += c[i]
244-
}
245-
return ans
168+
return ans;
246169
}
247170
```
248171
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
int busyStudent(int* startTime, int startTimeSize, int* endTime, int endTimeSize, int queryTime) {
2-
int res = 0;
2+
int ans = 0;
33
for (int i = 0; i < startTimeSize; i++) {
44
if (startTime[i] <= queryTime && endTime[i] >= queryTime) {
5-
res++;
5+
ans++;
66
}
77
}
8-
return res;
9-
}
8+
return ans;
9+
}

0 commit comments

Comments
 (0)
Please sign in to comment.