Skip to content

Commit 90cf3be

Browse files
add 601
1 parent dea9ec4 commit 90cf3be

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,7 @@ Your ideas/fixes/algorithms are more than welcome!
632632
|608|[Tree Node](https://leetcode.com/problems/tree-node/)|[Solution](../master/database/_608.sql) | | | Medium | Union
633633
|607|[Sales Person](https://leetcode.com/problems/sales-person/)|[Solution](../master/database/_607.sql) | | | Easy |
634634
|602|[Friend Requests II: Who Has the Most Friends](https://leetcode.com/problems/friend-requests-ii-who-has-the-most-friends/)|[Solution](../master/database/_602.sql) | | | Medium |
635+
|601|[Human Traffic of Stadium](https://leetcode.com/problems/human-traffic-of-stadium/)|[Solution](../master/database/_601.sql) | | | Hard |
635636
|597|[Friend Requests I: Overall Acceptance Rate](https://leetcode.com/problems/friend-requests-i-overall-acceptance-rate/)|[Solution](../master/database/_597.sql) | | | Easy |
636637
|596|[Classes More Than 5 Students](https://leetcode.com/problems/classes-more-than-5-students/)|[Solution](../master/database/_596.sql) | || Easy |
637638
|595|[Big Countries](https://leetcode.com/problems/big-countries/)|[Solution](../master/database/_595.sql) | O(n) |O(1) | Easy |

database/_601.sql

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
-- 601. Human Traffic of Stadium
2+
--
3+
--X city built a new stadium, each day many people visit it and the stats are saved as these columns: id, date, people
4+
--
5+
--Please write a query to display the records which have 3 or more consecutive rows and the amount of people more than 100(inclusive).
6+
--For example, the table stadium:
7+
--
8+
--+------+------------+-----------+
9+
--| id | date | people |
10+
--+------+------------+-----------+
11+
--| 1 | 2017-01-01 | 10 |
12+
--| 2 | 2017-01-02 | 109 |
13+
--| 3 | 2017-01-03 | 150 |
14+
--| 4 | 2017-01-04 | 99 |
15+
--| 5 | 2017-01-05 | 145 |
16+
--| 6 | 2017-01-06 | 1455 |
17+
--| 7 | 2017-01-07 | 199 |
18+
--| 8 | 2017-01-08 | 188 |
19+
--+------+------------+-----------+
20+
--
21+
--For the sample data above, the output is:
22+
--
23+
--+------+------------+-----------+
24+
--| id | date | people |
25+
--+------+------------+-----------+
26+
--| 5 | 2017-01-05 | 145 |
27+
--| 6 | 2017-01-06 | 1455 |
28+
--| 7 | 2017-01-07 | 199 |
29+
--| 8 | 2017-01-08 | 188 |
30+
--+------+------------+-----------+
31+
--
32+
--Note:
33+
--Each day only have one row record, and the dates are increasing with id increasing.
34+
35+
SELECT s1.* FROM stadium AS s1, stadium AS s2, stadium as s3
36+
WHERE
37+
((s1.id + 1 = s2.id
38+
AND s1.id + 2 = s3.id)
39+
OR
40+
(s1.id - 1 = s2.id
41+
AND s1.id + 1 = s3.id)
42+
OR
43+
(s1.id - 2 = s2.id
44+
AND s1.id - 1 = s3.id)
45+
)
46+
AND s1.people>=100
47+
AND s2.people>=100
48+
AND s3.people>=100
49+
50+
GROUP BY s1.id

0 commit comments

Comments
 (0)