From d858e6511d254ae01c8ab734a315f789b08ce4c3 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Sun, 1 Sep 2024 09:48:12 +0800 Subject: [PATCH] feat: update solutions to lc problem: No.1450 No.1450.Number of Students Doing Homework at a Given Time --- .../README.md | 143 +++--------------- .../README_EN.md | 121 +++------------ .../Solution.c | 8 +- .../Solution.go | 12 +- .../Solution.py | 2 +- .../Solution.rs | 6 +- .../Solution.ts | 8 +- .../Solution2.cpp | 15 -- .../Solution2.go | 13 -- .../Solution2.java | 14 -- .../Solution2.py | 9 -- 11 files changed, 59 insertions(+), 292 deletions(-) delete mode 100644 solution/1400-1499/1450.Number of Students Doing Homework at a Given Time/Solution2.cpp delete mode 100644 solution/1400-1499/1450.Number of Students Doing Homework at a Given Time/Solution2.go delete mode 100644 solution/1400-1499/1450.Number of Students Doing Homework at a Given Time/Solution2.java delete mode 100644 solution/1400-1499/1450.Number of Students Doing Homework at a Given Time/Solution2.py diff --git a/solution/1400-1499/1450.Number of Students Doing Homework at a Given Time/README.md b/solution/1400-1499/1450.Number of Students Doing Homework at a Given Time/README.md index d462d297cb93e..46ae03faf3ff5 100644 --- a/solution/1400-1499/1450.Number of Students Doing Homework at a Given Time/README.md +++ b/solution/1400-1499/1450.Number of Students Doing Homework at a Given Time/README.md @@ -78,11 +78,13 @@ tags: -### 方法一:遍历计数 +### 方法一:直接遍历 -同时遍历 $startTime$ 和 $endTime$,统计正在做作业的学生人数。 +我们可以直接遍历两个数组,对于每个学生,判断 $\textit{queryTime}$ 是否在他们的作业时间区间内,若是,答案加一。 -时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 是 $startTime$ 和 $endTime$ 的长度。 +遍历结束后,返回答案即可。 + +时间复杂度 $O(n)$,其中 $n$ 为学生数量。空间复杂度 $O(1)$。 @@ -93,7 +95,7 @@ class Solution: def busyStudent( self, startTime: List[int], endTime: List[int], queryTime: int ) -> int: - return sum(a <= queryTime <= b for a, b in zip(startTime, endTime)) + return sum(x <= queryTime <= y for x, y in zip(startTime, endTime)) ``` #### Java @@ -130,15 +132,13 @@ public: #### Go ```go -func busyStudent(startTime []int, endTime []int, queryTime int) int { - ans := 0 - for i, a := range startTime { - b := endTime[i] - if a <= queryTime && queryTime <= b { +func busyStudent(startTime []int, endTime []int, queryTime int) (ans int) { + for i, x := range startTime { + if x <= queryTime && queryTime <= endTime[i] { ans++ } } - return ans + return } ``` @@ -147,13 +147,13 @@ func busyStudent(startTime []int, endTime []int, queryTime int) int { ```ts function busyStudent(startTime: number[], endTime: number[], queryTime: number): number { const n = startTime.length; - let res = 0; + let ans = 0; for (let i = 0; i < n; i++) { - if (startTime[i] <= queryTime && endTime[i] >= queryTime) { - res++; + if (startTime[i] <= queryTime && queryTime <= endTime[i]) { + ans++; } } - return res; + return ans; } ``` @@ -162,13 +162,13 @@ function busyStudent(startTime: number[], endTime: number[], queryTime: number): ```rust impl Solution { pub fn busy_student(start_time: Vec, end_time: Vec, query_time: i32) -> i32 { - let mut res = 0; + let mut ans = 0; for i in 0..start_time.len() { if start_time[i] <= query_time && end_time[i] >= query_time { - res += 1; + ans += 1; } } - res + ans } } ``` @@ -177,116 +177,13 @@ impl Solution { ```c int busyStudent(int* startTime, int startTimeSize, int* endTime, int endTimeSize, int queryTime) { - int res = 0; + int ans = 0; for (int i = 0; i < startTimeSize; i++) { if (startTime[i] <= queryTime && endTime[i] >= queryTime) { - res++; - } - } - return res; -} -``` - - - - - - - -### 方法二:差分数组 - -差分数组可以 $O(1)$ 时间处理区间加减操作。例如,对区间 $[l, r]$ 中的每个数加上 $c$。 - -假设数组 $a$ 的所有元素分别为 $a[1], a[2], ... a[n]$,则差分数组 $b$ 的元素 $b[i]=a[i]-a[i-1]$。 - -$$ -\begin{cases} -b[1]=a[1]\\ -b[2]=a[2]-a[1]\\ -b[3]=a[3]-a[2]\\ -...\\ -b[i]=a[i]-a[i-1]\\ -\end{cases} -$$ - -那么 $a[i]=b[1]+b[2]+...+b[i]$,原数组 $a$ 是差分数组 $b$ 的前缀和。 - -在这道题中,我们定义差分数组 $c$,然后遍历两个数组中对应位置的两个数 $a$, $b$,则 $c[a]+=1$, $c[b+1]-=1$。 - -遍历结束后,对差分数组 $c$ 进行求前缀和操作,即可得到 $queryTime$ 时刻正在做作业的学生人数。 - -时间复杂度 $O(n+queryTime)$,空间复杂度 $O(1010)$。 - - - -#### Python3 - -```python -class Solution: - def busyStudent( - self, startTime: List[int], endTime: List[int], queryTime: int - ) -> int: - c = [0] * 1010 - for a, b in zip(startTime, endTime): - c[a] += 1 - c[b + 1] -= 1 - return sum(c[: queryTime + 1]) -``` - -#### Java - -```java -class Solution { - public int busyStudent(int[] startTime, int[] endTime, int queryTime) { - int[] c = new int[1010]; - for (int i = 0; i < startTime.length; ++i) { - c[startTime[i]]++; - c[endTime[i] + 1]--; - } - int ans = 0; - for (int i = 0; i <= queryTime; ++i) { - ans += c[i]; - } - return ans; - } -} -``` - -#### C++ - -```cpp -class Solution { -public: - int busyStudent(vector& startTime, vector& endTime, int queryTime) { - vector c(1010); - for (int i = 0; i < startTime.size(); ++i) { - c[startTime[i]]++; - c[endTime[i] + 1]--; - } - int ans = 0; - for (int i = 0; i <= queryTime; ++i) { - ans += c[i]; + ans++; } - return ans; } -}; -``` - -#### Go - -```go -func busyStudent(startTime []int, endTime []int, queryTime int) int { - c := make([]int, 1010) - for i, a := range startTime { - b := endTime[i] - c[a]++ - c[b+1]-- - } - ans := 0 - for i := 0; i <= queryTime; i++ { - ans += c[i] - } - return ans + return ans; } ``` diff --git a/solution/1400-1499/1450.Number of Students Doing Homework at a Given Time/README_EN.md b/solution/1400-1499/1450.Number of Students Doing Homework at a Given Time/README_EN.md index c6124f38bf493..2203446c59639 100644 --- a/solution/1400-1499/1450.Number of Students Doing Homework at a Given Time/README_EN.md +++ b/solution/1400-1499/1450.Number of Students Doing Homework at a Given Time/README_EN.md @@ -60,7 +60,13 @@ The third student started doing homework at time 3 and finished at time 7 and wa -### Solution 1 +### Solution 1: Direct Traversal + +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. + +After the traversal, we return the answer. + +The time complexity is $O(n)$, where $n$ is the number of students. The space complexity is $O(1)$. @@ -71,7 +77,7 @@ class Solution: def busyStudent( self, startTime: List[int], endTime: List[int], queryTime: int ) -> int: - return sum(a <= queryTime <= b for a, b in zip(startTime, endTime)) + return sum(x <= queryTime <= y for x, y in zip(startTime, endTime)) ``` #### Java @@ -108,15 +114,13 @@ public: #### Go ```go -func busyStudent(startTime []int, endTime []int, queryTime int) int { - ans := 0 - for i, a := range startTime { - b := endTime[i] - if a <= queryTime && queryTime <= b { +func busyStudent(startTime []int, endTime []int, queryTime int) (ans int) { + for i, x := range startTime { + if x <= queryTime && queryTime <= endTime[i] { ans++ } } - return ans + return } ``` @@ -125,13 +129,13 @@ func busyStudent(startTime []int, endTime []int, queryTime int) int { ```ts function busyStudent(startTime: number[], endTime: number[], queryTime: number): number { const n = startTime.length; - let res = 0; + let ans = 0; for (let i = 0; i < n; i++) { - if (startTime[i] <= queryTime && endTime[i] >= queryTime) { - res++; + if (startTime[i] <= queryTime && queryTime <= endTime[i]) { + ans++; } } - return res; + return ans; } ``` @@ -140,13 +144,13 @@ function busyStudent(startTime: number[], endTime: number[], queryTime: number): ```rust impl Solution { pub fn busy_student(start_time: Vec, end_time: Vec, query_time: i32) -> i32 { - let mut res = 0; + let mut ans = 0; for i in 0..start_time.len() { if start_time[i] <= query_time && end_time[i] >= query_time { - res += 1; + ans += 1; } } - res + ans } } ``` @@ -155,94 +159,13 @@ impl Solution { ```c int busyStudent(int* startTime, int startTimeSize, int* endTime, int endTimeSize, int queryTime) { - int res = 0; + int ans = 0; for (int i = 0; i < startTimeSize; i++) { if (startTime[i] <= queryTime && endTime[i] >= queryTime) { - res++; + ans++; } } - return res; -} -``` - - - - - - - -### Solution 2 - - - -#### Python3 - -```python -class Solution: - def busyStudent( - self, startTime: List[int], endTime: List[int], queryTime: int - ) -> int: - c = [0] * 1010 - for a, b in zip(startTime, endTime): - c[a] += 1 - c[b + 1] -= 1 - return sum(c[: queryTime + 1]) -``` - -#### Java - -```java -class Solution { - public int busyStudent(int[] startTime, int[] endTime, int queryTime) { - int[] c = new int[1010]; - for (int i = 0; i < startTime.length; ++i) { - c[startTime[i]]++; - c[endTime[i] + 1]--; - } - int ans = 0; - for (int i = 0; i <= queryTime; ++i) { - ans += c[i]; - } - return ans; - } -} -``` - -#### C++ - -```cpp -class Solution { -public: - int busyStudent(vector& startTime, vector& endTime, int queryTime) { - vector c(1010); - for (int i = 0; i < startTime.size(); ++i) { - c[startTime[i]]++; - c[endTime[i] + 1]--; - } - int ans = 0; - for (int i = 0; i <= queryTime; ++i) { - ans += c[i]; - } - return ans; - } -}; -``` - -#### Go - -```go -func busyStudent(startTime []int, endTime []int, queryTime int) int { - c := make([]int, 1010) - for i, a := range startTime { - b := endTime[i] - c[a]++ - c[b+1]-- - } - ans := 0 - for i := 0; i <= queryTime; i++ { - ans += c[i] - } - return ans + return ans; } ``` diff --git a/solution/1400-1499/1450.Number of Students Doing Homework at a Given Time/Solution.c b/solution/1400-1499/1450.Number of Students Doing Homework at a Given Time/Solution.c index 2dd59ef5a3103..619b1aea7893e 100644 --- a/solution/1400-1499/1450.Number of Students Doing Homework at a Given Time/Solution.c +++ b/solution/1400-1499/1450.Number of Students Doing Homework at a Given Time/Solution.c @@ -1,9 +1,9 @@ int busyStudent(int* startTime, int startTimeSize, int* endTime, int endTimeSize, int queryTime) { - int res = 0; + int ans = 0; for (int i = 0; i < startTimeSize; i++) { if (startTime[i] <= queryTime && endTime[i] >= queryTime) { - res++; + ans++; } } - return res; -} \ No newline at end of file + return ans; +} diff --git a/solution/1400-1499/1450.Number of Students Doing Homework at a Given Time/Solution.go b/solution/1400-1499/1450.Number of Students Doing Homework at a Given Time/Solution.go index c289e85b2d770..0a92acd79824b 100644 --- a/solution/1400-1499/1450.Number of Students Doing Homework at a Given Time/Solution.go +++ b/solution/1400-1499/1450.Number of Students Doing Homework at a Given Time/Solution.go @@ -1,10 +1,8 @@ -func busyStudent(startTime []int, endTime []int, queryTime int) int { - ans := 0 - for i, a := range startTime { - b := endTime[i] - if a <= queryTime && queryTime <= b { +func busyStudent(startTime []int, endTime []int, queryTime int) (ans int) { + for i, x := range startTime { + if x <= queryTime && queryTime <= endTime[i] { ans++ } } - return ans -} \ No newline at end of file + return +} diff --git a/solution/1400-1499/1450.Number of Students Doing Homework at a Given Time/Solution.py b/solution/1400-1499/1450.Number of Students Doing Homework at a Given Time/Solution.py index b70553ec0545f..c5acd2cdd707c 100644 --- a/solution/1400-1499/1450.Number of Students Doing Homework at a Given Time/Solution.py +++ b/solution/1400-1499/1450.Number of Students Doing Homework at a Given Time/Solution.py @@ -2,4 +2,4 @@ class Solution: def busyStudent( self, startTime: List[int], endTime: List[int], queryTime: int ) -> int: - return sum(a <= queryTime <= b for a, b in zip(startTime, endTime)) + return sum(x <= queryTime <= y for x, y in zip(startTime, endTime)) diff --git a/solution/1400-1499/1450.Number of Students Doing Homework at a Given Time/Solution.rs b/solution/1400-1499/1450.Number of Students Doing Homework at a Given Time/Solution.rs index b9da79fd77fd8..b9c5bcc3e4a3b 100644 --- a/solution/1400-1499/1450.Number of Students Doing Homework at a Given Time/Solution.rs +++ b/solution/1400-1499/1450.Number of Students Doing Homework at a Given Time/Solution.rs @@ -1,11 +1,11 @@ impl Solution { pub fn busy_student(start_time: Vec, end_time: Vec, query_time: i32) -> i32 { - let mut res = 0; + let mut ans = 0; for i in 0..start_time.len() { if start_time[i] <= query_time && end_time[i] >= query_time { - res += 1; + ans += 1; } } - res + ans } } diff --git a/solution/1400-1499/1450.Number of Students Doing Homework at a Given Time/Solution.ts b/solution/1400-1499/1450.Number of Students Doing Homework at a Given Time/Solution.ts index b20af1eaf6458..367a03208ff8f 100644 --- a/solution/1400-1499/1450.Number of Students Doing Homework at a Given Time/Solution.ts +++ b/solution/1400-1499/1450.Number of Students Doing Homework at a Given Time/Solution.ts @@ -1,10 +1,10 @@ function busyStudent(startTime: number[], endTime: number[], queryTime: number): number { const n = startTime.length; - let res = 0; + let ans = 0; for (let i = 0; i < n; i++) { - if (startTime[i] <= queryTime && endTime[i] >= queryTime) { - res++; + if (startTime[i] <= queryTime && queryTime <= endTime[i]) { + ans++; } } - return res; + return ans; } diff --git a/solution/1400-1499/1450.Number of Students Doing Homework at a Given Time/Solution2.cpp b/solution/1400-1499/1450.Number of Students Doing Homework at a Given Time/Solution2.cpp deleted file mode 100644 index c9e276a0733ee..0000000000000 --- a/solution/1400-1499/1450.Number of Students Doing Homework at a Given Time/Solution2.cpp +++ /dev/null @@ -1,15 +0,0 @@ -class Solution { -public: - int busyStudent(vector& startTime, vector& endTime, int queryTime) { - vector c(1010); - for (int i = 0; i < startTime.size(); ++i) { - c[startTime[i]]++; - c[endTime[i] + 1]--; - } - int ans = 0; - for (int i = 0; i <= queryTime; ++i) { - ans += c[i]; - } - return ans; - } -}; \ No newline at end of file diff --git a/solution/1400-1499/1450.Number of Students Doing Homework at a Given Time/Solution2.go b/solution/1400-1499/1450.Number of Students Doing Homework at a Given Time/Solution2.go deleted file mode 100644 index 3cb84a13f9147..0000000000000 --- a/solution/1400-1499/1450.Number of Students Doing Homework at a Given Time/Solution2.go +++ /dev/null @@ -1,13 +0,0 @@ -func busyStudent(startTime []int, endTime []int, queryTime int) int { - c := make([]int, 1010) - for i, a := range startTime { - b := endTime[i] - c[a]++ - c[b+1]-- - } - ans := 0 - for i := 0; i <= queryTime; i++ { - ans += c[i] - } - return ans -} \ No newline at end of file diff --git a/solution/1400-1499/1450.Number of Students Doing Homework at a Given Time/Solution2.java b/solution/1400-1499/1450.Number of Students Doing Homework at a Given Time/Solution2.java deleted file mode 100644 index f2c779d094def..0000000000000 --- a/solution/1400-1499/1450.Number of Students Doing Homework at a Given Time/Solution2.java +++ /dev/null @@ -1,14 +0,0 @@ -class Solution { - public int busyStudent(int[] startTime, int[] endTime, int queryTime) { - int[] c = new int[1010]; - for (int i = 0; i < startTime.length; ++i) { - c[startTime[i]]++; - c[endTime[i] + 1]--; - } - int ans = 0; - for (int i = 0; i <= queryTime; ++i) { - ans += c[i]; - } - return ans; - } -} \ No newline at end of file diff --git a/solution/1400-1499/1450.Number of Students Doing Homework at a Given Time/Solution2.py b/solution/1400-1499/1450.Number of Students Doing Homework at a Given Time/Solution2.py deleted file mode 100644 index aa441bc60d933..0000000000000 --- a/solution/1400-1499/1450.Number of Students Doing Homework at a Given Time/Solution2.py +++ /dev/null @@ -1,9 +0,0 @@ -class Solution: - def busyStudent( - self, startTime: List[int], endTime: List[int], queryTime: int - ) -> int: - c = [0] * 1010 - for a, b in zip(startTime, endTime): - c[a] += 1 - c[b + 1] -= 1 - return sum(c[: queryTime + 1])