Skip to content

Commit e30ad20

Browse files
committed
Upload 177
1 parent 6f46d2a commit e30ad20

File tree

2 files changed

+77
-1
lines changed

2 files changed

+77
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<!-- markdownlint-disable MD041 -->
22
[![eevveerryyddaayy release](https://img.shields.io/badge/eevveerryyddaayy-v1.3.0-blue.svg)](https://github.com/ggeerraarrdd/eevveerryyddaayy/)
3-
[![Solved](https://img.shields.io/badge/solved-176-green.svg)](#index)
3+
[![Solved](https://img.shields.io/badge/solved-177-green.svg)](#index)
44
<!-- markdownlint-enable MD041 -->
55

66
# SQL Everyday
@@ -210,6 +210,7 @@ Because this project necessitated a framework to enable consistent daily practic
210210
| 174 | [Highest Salaries Difference](https://leetcode.com/problems/highest-salaries-difference/description/) | [Solution](solutions/174_highest_salaries_difference.md) | LeetCode | Easy | |
211211
| 175 | [Form a Chemical Bond](https://leetcode.com/problems/form-a-chemical-bond/description/) | [Solution](solutions/175_form_a_chemical_bond.md) | LeetCode | Easy | `CROSS JOIN` |
212212
| 176 | [Concatenate the Name and the Profession](https://leetcode.com/problems/concatenate-the-name-and-the-profession/description/) | [Solution](solutions/176_concatenate_the_name_and_the_profession.md) | LeetCode | Easy | `SUBSTRING` vs `LEFT` |
213+
| 177 | [Consecutive Available Seats](https://leetcode.com/problems/consecutive-available-seats/description/) | [Solution](solutions/177_consecutive_available_seats.md) | LeetCode | Easy | |
213214
<!-- Index End - WARNING: Do not delete or modify this markdown comment. -->
214215
<!--- cSpell:enable --->
215216

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# SQL Everyday \#177
2+
3+
## Consecutive Available Seats
4+
5+
Site: LeetCode\
6+
Difficulty per Site: Easy
7+
8+
## Problem
9+
10+
Find all the consecutive available seats in the cinema.
11+
12+
Return the result table **ordered** by `seat_id` **in ascending order**. [[Full Description](https://leetcode.com/problems/consecutive-available-seats/description/)]
13+
14+
## Submitted Solution
15+
16+
```sql
17+
-- Submitted Solution
18+
WITH cte1 AS (
19+
SELECT
20+
seat_id
21+
,free
22+
,LEAD(free) OVER (ORDER BY seat_id ASC) AS leadcol
23+
,LAG(free) OVER (ORDER BY seat_id ASC) AS lagcol
24+
FROM Cinema
25+
),
26+
cte2 AS (
27+
SELECT
28+
*
29+
,CASE WHEN free = 1 AND leadcol = 1 AND lagcol IS NULL THEN seat_id
30+
WHEN free = 1 AND leadcol = 1 AND lagcol = 0 THEN seat_id
31+
WHEN free = 1 AND leadcol = 1 AND lagcol = 1 THEN seat_id
32+
WHEN free = 1 AND leadcol IS NULL AND lagcol = 1 THEN seat_id
33+
WHEN free =1 AND leadcol = 0 AND lagcol = 1 THEN seat_id
34+
END AS consec
35+
FROM cte1
36+
)
37+
SELECT
38+
consec AS seat_id
39+
FROM cte2
40+
WHERE consec IS NOT NULL
41+
;
42+
```
43+
44+
## Site Solution
45+
46+
```sql
47+
-- LeetCode Solution
48+
select distinct a.seat_id
49+
from cinema a join cinema b
50+
on abs(a.seat_id - b.seat_id) = 1
51+
and a.free = true and b.free = true
52+
order by a.seat_id
53+
;
54+
55+
-- Code Author: Clayton Wong
56+
-- https://leetcode.com/problems/consecutive-available-seats/solutions/597149/mysql-solution
57+
SELECT seat_id
58+
FROM cinema
59+
WHERE free = 1 AND (
60+
seat_id - 1 IN (SELECT seat_id FROM cinema WHERE free = 1)
61+
OR
62+
seat_id + 1 IN (SELECT seat_id FROM cinema WHERE free = 1)
63+
);
64+
```
65+
66+
## Notes
67+
68+
TBD
69+
70+
## NB
71+
72+
TBD
73+
74+
Go to [Index](../?tab=readme-ov-file#index)\
75+
Go to [Overview](../?tab=readme-ov-file)

0 commit comments

Comments
 (0)