Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 05880c3

Browse files
feat: add sql solution to lc problem: No.2153 (doocs#1167)
No.2153.The Number of Passengers in Each Bus II
1 parent b614297 commit 05880c3

File tree

7 files changed

+66
-7
lines changed

7 files changed

+66
-7
lines changed

‎.prettierignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,5 @@ node_modules/
2626
/solution/1600-1699/1651.Hopper Company Queries III/Solution.sql
2727
/solution/1700-1799/1767.Find the Subtasks That Did Not Execute/Solution.sql
2828
/solution/2100-2199/2118.Build the Equation/Solution.sql
29+
/solution/2100-2199/2153.The Number of Passengers in Each Bus II/Solution.sql
2930
/solution/2200-2299/2205.The Number of Users That Are Eligible for Discount/Solution.sql

‎solution/0100-0199/0167.Two Sum II - Input Array Is Sorted/README.md‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,15 @@
5555

5656
**方法一:二分查找**
5757

58-
注意到数组按照非递减顺序排列,因此对于每个 `numbers[i]`,可以通过二分查找的方式找到 `target - numbers[i]` 的位置,如果存在,那么返回 $[i + 1, j + 1]$ 即可。
58+
我们注意到数组按照非递减顺序排列,因此对于每个 $numbers[i]$,可以通过二分查找的方式找到 $target - numbers[i]$ 的位置,如果存在,那么返回 $[i + 1, j + 1]$ 即可。
5959

60-
时间复杂度 $O(n \times \log n),ドル其中 $n$ 为数组 `numbers` 的长度。空间复杂度 $O(1)$。
60+
时间复杂度 $O(n \times \log n),ドル其中 $n$ 为数组 $numbers$ 的长度。空间复杂度 $O(1)$。
6161

6262
**方法二:双指针**
6363

6464
我们定义两个指针 $i$ 和 $j,ドル分别指向数组的第一个元素和最后一个元素。每次计算 $numbers[i] + numbers[j],ドル如果和等于目标值,那么返回 $[i + 1, j + 1]$ 即可。如果和小于目标值,那么将 $i$ 右移一位,如果和大于目标值,那么将 $j$ 左移一位。
6565

66-
时间复杂度 $O(n),ドル其中 $n$ 为数组 `numbers` 的长度。空间复杂度 $O(1)$。
66+
时间复杂度 $O(n),ドル其中 $n$ 为数组 $numbers$ 的长度。空间复杂度 $O(1)$。
6767

6868
<!-- tabs:start -->
6969

‎solution/2100-2199/2153.The Number of Passengers in Each Bus II/README.md‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,26 @@ Passengers 表:
101101
<!-- 这里可写当前语言的特殊实现逻辑 -->
102102

103103
```sql
104-
104+
# Write your MySQL query statement below
105+
WITH
106+
T AS (
107+
SELECT
108+
*,
109+
sum(cnt) OVER (ORDER BY dt, bus_id) AS cur,
110+
if(@t > 0, @t := cnt, @t := @t + cnt) AS cur_sum
111+
FROM
112+
(
113+
SELECT bus_id, arrival_time AS dt, capacity AS cnt FROM Buses
114+
UNION ALL
115+
SELECT -1, arrival_time AS dt, -1 FROM Passengers
116+
) AS a JOIN (SELECT @t := 0 x) AS b
117+
)
118+
SELECT
119+
bus_id,
120+
if(cur_sum > 0, cnt - cur_sum, cnt) AS passengers_cnt
121+
FROM T
122+
WHERE bus_id > 0
123+
ORDER BY bus_id;
105124
```
106125

107126
<!-- tabs:end -->

‎solution/2100-2199/2153.The Number of Passengers in Each Bus II/README_EN.md‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,26 @@ Passengers table:
9595
### **SQL**
9696

9797
```sql
98-
98+
# Write your MySQL query statement below
99+
WITH
100+
T AS (
101+
SELECT
102+
*,
103+
sum(cnt) OVER (ORDER BY dt, bus_id) AS cur,
104+
if(@t > 0, @t := cnt, @t := @t + cnt) AS cur_sum
105+
FROM
106+
(
107+
SELECT bus_id, arrival_time AS dt, capacity AS cnt FROM Buses
108+
UNION ALL
109+
SELECT -1, arrival_time AS dt, -1 FROM Passengers
110+
) AS a JOIN (SELECT @t := 0 x) AS b
111+
)
112+
SELECT
113+
bus_id,
114+
if(cur_sum > 0, cnt - cur_sum, cnt) AS passengers_cnt
115+
FROM T
116+
WHERE bus_id > 0
117+
ORDER BY bus_id;
99118
```
100119

101120
<!-- tabs:end -->
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Write your MySQL query statement below
2+
WITH
3+
T AS (
4+
SELECT
5+
*,
6+
sum(cnt) OVER (ORDER BY dt, bus_id) AS cur,
7+
if(@t > 0, @t := cnt, @t := @t + cnt) AS cur_sum
8+
FROM
9+
(
10+
SELECT bus_id, arrival_time AS dt, capacity AS cnt FROM Buses
11+
UNION ALL
12+
SELECT -1, arrival_time AS dt, -1 FROM Passengers
13+
) AS a JOIN (SELECT @t := 0 x) AS b
14+
)
15+
SELECT
16+
bus_id,
17+
if(cur_sum > 0, cnt - cur_sum, cnt) AS passengers_cnt
18+
FROM T
19+
WHERE bus_id > 0
20+
ORDER BY bus_id;

‎solution/2600-2699/2609.Find the Longest Balanced Substring of a Binary String/README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ impl Solution {
361361
if j - i + 1 < ans {
362362
break;
363363
}
364-
364+
365365
if check(i, j) {
366366
ans = std::cmp::max(ans, j - i + 1);
367367
break;

‎solution/2600-2699/2609.Find the Longest Balanced Substring of a Binary String/README_EN.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ impl Solution {
351351
if j - i + 1 < ans {
352352
break;
353353
}
354-
354+
355355
if check(i, j) {
356356
ans = std::cmp::max(ans, j - i + 1);
357357
break;

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /