Skip to content

feat: add solutions to lc problems #1190

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions solution/0600-0699/0601.Human Traffic of Stadium/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,19 +73,18 @@ id</strong> 为 5、6、7、8 的四行 id 连续,并且每行都有 &gt;= 100
```sql
# Write your MySQL query statement below
WITH
s AS (
SELECT *, id - row_number() OVER (ORDER BY id) AS rk
S AS (
SELECT
*,
id - (row_number() OVER (ORDER BY id)) AS rk
FROM Stadium
WHERE people >= 100
),
t AS (
SELECT *, count(*) OVER (PARTITION BY rk) AS cnt
FROM s
)
T AS (SELECT *, count(1) OVER (PARTITION BY rk) AS cnt FROM S)
SELECT id, visit_date, people
FROM t
FROM T
WHERE cnt >= 3
ORDER BY visit_date;
ORDER BY 1;
```

<!-- tabs:end -->
15 changes: 7 additions & 8 deletions solution/0600-0699/0601.Human Traffic of Stadium/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,19 +68,18 @@ The rows with ids 2 and 3 are not included because we need at least three consec
```sql
# Write your MySQL query statement below
WITH
s AS (
SELECT *, id - row_number() OVER (ORDER BY id) AS rk
S AS (
SELECT
*,
id - (row_number() OVER (ORDER BY id)) AS rk
FROM Stadium
WHERE people >= 100
),
t AS (
SELECT *, count(*) OVER (PARTITION BY rk) AS cnt
FROM s
)
T AS (SELECT *, count(1) OVER (PARTITION BY rk) AS cnt FROM S)
SELECT id, visit_date, people
FROM t
FROM T
WHERE cnt >= 3
ORDER BY visit_date;
ORDER BY 1;
```

<!-- tabs:end -->
15 changes: 7 additions & 8 deletions solution/0600-0699/0601.Human Traffic of Stadium/Solution.sql
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
# Write your MySQL query statement below
WITH
s AS (
SELECT *, id - row_number() OVER (ORDER BY id) AS rk
S AS (
SELECT
*,
id - (row_number() OVER (ORDER BY id)) AS rk
FROM Stadium
WHERE people >= 100
),
t AS (
SELECT *, count(*) OVER (PARTITION BY rk) AS cnt
FROM s
)
T AS (SELECT *, count(1) OVER (PARTITION BY rk) AS cnt FROM S)
SELECT id, visit_date, people
FROM t
FROM T
WHERE cnt >= 3
ORDER BY visit_date;
ORDER BY 1;
Original file line number Diff line number Diff line change
Expand Up @@ -71,21 +71,17 @@ RequestAccepted 表:
### **SQL**

```sql
SELECT
ids AS id,
COUNT(*) AS num
FROM
(
SELECT
requester_id AS ids
FROM RequestAccepted
UNION ALL
SELECT
accepter_id
FROM RequestAccepted
) AS t
GROUP BY ids
ORDER BY num DESC
# Write your MySQL query statement below
WITH
T AS (
SELECT requester_id, accepter_id FROM RequestAccepted
UNION
SELECT accepter_id, requester_id FROM RequestAccepted
)
SELECT requester_id AS id, count(accepter_id) AS num
FROM T
GROUP BY 1
ORDER BY 2 DESC
LIMIT 1;
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,21 +60,17 @@ The person with id 3 is a friend of people 1, 2, and 4, so he has three friends
### **SQL**

```sql
SELECT
ids AS id,
COUNT(*) AS num
FROM
(
SELECT
requester_id AS ids
FROM RequestAccepted
UNION ALL
SELECT
accepter_id
FROM RequestAccepted
) AS t
GROUP BY ids
ORDER BY num DESC
# Write your MySQL query statement below
WITH
T AS (
SELECT requester_id, accepter_id FROM RequestAccepted
UNION
SELECT accepter_id, requester_id FROM RequestAccepted
)
SELECT requester_id AS id, count(accepter_id) AS num
FROM T
GROUP BY 1
ORDER BY 2 DESC
LIMIT 1;
```

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
SELECT
ids AS id,
COUNT(*) AS num
FROM
(
SELECT
requester_id AS ids
FROM RequestAccepted
UNION ALL
SELECT
accepter_id
FROM RequestAccepted
) AS t
GROUP BY ids
ORDER BY num DESC
# Write your MySQL query statement below
WITH
T AS (
SELECT requester_id, accepter_id FROM RequestAccepted
UNION
SELECT accepter_id, requester_id FROM RequestAccepted
)
SELECT requester_id AS id, count(accepter_id) AS num
FROM T
GROUP BY 1
ORDER BY 2 DESC
LIMIT 1;
20 changes: 17 additions & 3 deletions solution/0600-0699/0603.Consecutive Available Seats/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,23 @@ Cinema 表:
SELECT DISTINCT a.seat_id
FROM
Cinema AS a
JOIN Cinema AS b
ON ABS(a.seat_id - b.seat_id) = 1 AND a.free = TRUE AND b.free = TRUE
ORDER BY a.seat_id;
JOIN Cinema AS b ON abs(a.seat_id - b.seat_id) = 1 AND a.free AND b.free
ORDER BY 1;
```

```sql
# Write your MySQL query statement below
WITH
T AS (
SELECT
seat_id,
(free + (lag(free) OVER (ORDER BY seat_id))) AS a,
(free + (lead(free) OVER (ORDER BY seat_id))) AS b
FROM Cinema
)
SELECT seat_id
FROM T
WHERE a = 2 OR b = 2;
```

<!-- tabs:end -->
20 changes: 17 additions & 3 deletions solution/0600-0699/0603.Consecutive Available Seats/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,23 @@ Cinema table:
SELECT DISTINCT a.seat_id
FROM
Cinema AS a
JOIN Cinema AS b
ON ABS(a.seat_id - b.seat_id) = 1 AND a.free = TRUE AND b.free = TRUE
ORDER BY a.seat_id;
JOIN Cinema AS b ON abs(a.seat_id - b.seat_id) = 1 AND a.free AND b.free
ORDER BY 1;
```

```sql
# Write your MySQL query statement below
WITH
T AS (
SELECT
seat_id,
(free + (lag(free) OVER (ORDER BY seat_id))) AS a,
(free + (lead(free) OVER (ORDER BY seat_id))) AS b
FROM Cinema
)
SELECT seat_id
FROM T
WHERE a = 2 OR b = 2;
```

<!-- tabs:end -->
17 changes: 11 additions & 6 deletions solution/0600-0699/0603.Consecutive Available Seats/Solution.sql
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
# Write your MySQL query statement below
SELECT DISTINCT a.seat_id
FROM
Cinema AS a
JOIN Cinema AS b
ON ABS(a.seat_id - b.seat_id) = 1 AND a.free = TRUE AND b.free = TRUE
ORDER BY a.seat_id;
WITH
T AS (
SELECT
seat_id,
(free + (lag(free) OVER (ORDER BY seat_id))) AS a,
(free + (lead(free) OVER (ORDER BY seat_id))) AS b
FROM Cinema
)
SELECT seat_id
FROM T
WHERE a = 2 OR b = 2;
15 changes: 15 additions & 0 deletions solution/0600-0699/0607.Sales Person/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,19 @@ WHERE
);
```

```sql
# Write your MySQL query statement below
SELECT name
FROM SalesPerson
WHERE
sales_id NOT IN (
SELECT sales_id
FROM
Company AS c
JOIN Orders AS o USING (com_id)
GROUP BY sales_id
HAVING sum(name = 'RED') > 0
);
```

<!-- tabs:end -->
15 changes: 15 additions & 0 deletions solution/0600-0699/0607.Sales Person/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,19 @@ WHERE
);
```

```sql
# Write your MySQL query statement below
SELECT name
FROM SalesPerson
WHERE
sales_id NOT IN (
SELECT sales_id
FROM
Company AS c
JOIN Orders AS o USING (com_id)
GROUP BY sales_id
HAVING sum(name = 'RED') > 0
);
```

<!-- tabs:end -->
26 changes: 3 additions & 23 deletions solution/0600-0699/0608.Tree Node/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,36 +79,16 @@

### **SQL**

```sql
SELECT
id,
(
CASE
WHEN p_id IS NULL THEN 'Root'
WHEN id IN (
SELECT p_id
FROM tree
) THEN 'Inner'
ELSE 'Leaf'
END
) AS type
FROM tree;
```

```sql
# Write your MySQL query statement below
SELECT
id,
CASE
WHEN p_id IS NULL THEN 'Root'
WHEN id IN (
SELECT
p_id
FROM tree
) THEN 'Inner'
WHEN id IN (SELECT p_id FROM Tree) THEN 'Inner'
ELSE 'Leaf'
END AS Type
FROM tree;
END AS type
FROM Tree;
```

<!-- tabs:end -->
26 changes: 3 additions & 23 deletions solution/0600-0699/0608.Tree Node/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,36 +90,16 @@ Tree table:

### **SQL**

```sql
SELECT
id,
(
CASE
WHEN p_id IS NULL THEN 'Root'
WHEN id IN (
SELECT p_id
FROM tree
) THEN 'Inner'
ELSE 'Leaf'
END
) AS type
FROM tree;
```

```sql
# Write your MySQL query statement below
SELECT
id,
CASE
WHEN p_id IS NULL THEN 'Root'
WHEN id IN (
SELECT
p_id
FROM tree
) THEN 'Inner'
WHEN id IN (SELECT p_id FROM Tree) THEN 'Inner'
ELSE 'Leaf'
END AS Type
FROM tree;
END AS type
FROM Tree;
```

<!-- tabs:end -->
18 changes: 7 additions & 11 deletions solution/0600-0699/0608.Tree Node/Solution.sql
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
# Write your MySQL query statement below
SELECT
id,
(
CASE
WHEN p_id IS NULL THEN 'Root'
WHEN id IN (
SELECT p_id
FROM tree
) THEN 'Inner'
ELSE 'Leaf'
END
) AS type
FROM tree;
CASE
WHEN p_id IS NULL THEN 'Root'
WHEN id IN (SELECT p_id FROM Tree) THEN 'Inner'
ELSE 'Leaf'
END AS type
FROM Tree;
8 changes: 8 additions & 0 deletions solution/0600-0699/0613.Shortest Distance in a Line/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,12 @@ FROM
JOIN Point AS p2 ON p1.x != p2.x;
```

```sql
# Write your MySQL query statement below
SELECT x - lag(x) OVER (ORDER BY x) AS shortest
FROM Point
ORDER BY 1
LIMIT 1, 1;
```

<!-- tabs:end -->
Loading