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 82a12d3

Browse files
committed
Adding new Solutions
1 parent d3c51b4 commit 82a12d3

File tree

5 files changed

+84
-14
lines changed

5 files changed

+84
-14
lines changed

‎dump_file/leetcodedb.sql

460 Bytes
Binary file not shown.

‎easy/603. Consecutive Available Seats.sql

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ FROM cinema_603 c1
33
INNER JOIN cinema_603 c2 ON ABS(c1.seat_id-c2.seat_id)=1
44
WHERE c1.free AND c2.free;
55

6-
7-
(OR)
8-
6+
-------------------------------------------------------------------------------------------------------------------------------
97

108
WITH cte AS(
119
SELECT seat_id,free,
@@ -17,3 +15,20 @@ WITH cte AS(
1715
SELECT DISTINCT seat_id
1816
FROM cte
1917
WHERE (free=1 AND next_seat=1) OR (free=1 AND prev_seat=1);
18+
19+
-------------------------------------------------------------------------------------------------------------------------------
20+
21+
WITH ranked AS (
22+
SELECT *,
23+
seat_id-ROW_NUMBER () OVER (ORDER BY seat_id) AS diff
24+
FROM cinema_603
25+
WHERE free = 1
26+
),
27+
consecutive_free_seats AS (
28+
SELECT *,
29+
COUNT(seat_id) OVER (PARTITION BY diff) AS cnt
30+
FROM ranked
31+
)
32+
SELECT seat_id
33+
FROM consecutive_free_seats
34+
WHERE cnt >= 2;

‎hard/1225. Report Contiguous Dates.sql

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,27 @@ cte4 AS (
2525
SELECT MAX(status) AS period_state, MIN(dt) AS start_date, MAX(dt) AS end_date
2626
FROM cte4
2727
GROUP BY rolling_sum;
28+
29+
---------------------------------------------------------------------------------------------------------------------------------------------
30+
--Simplified Query
31+
---------------------------------------------------------------------------------------------------------------------------------------------
32+
33+
WITH tasks AS (
34+
SELECT fail_date AS dt,'failed' AS status
35+
FROM failed_1225
36+
UNION
37+
SELECT success_date AS dt,'succeeded' AS status
38+
FROM succeeded_1225
39+
),
40+
ranked AS (
41+
SELECT *,
42+
ROW_NUMBER() OVER (ORDER BY dt)-ROW_NUMBER() OVER (PARTITION BY status ORDER BY dt) AS diff
43+
FROM tasks
44+
WHERE dt BETWEEN '01-01-2019' AND '31-12-2019'
45+
ORDER BY dt
46+
)
47+
SELECT status,
48+
MIN(dt) AS start_date,MAX(dt) AS end_date
49+
FROM ranked
50+
GROUP BY status,diff
51+
ORDER BY start_date;

‎hard/601. Human Traffic of Stadium.sql

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ ON ((s1.id = s2.id-1 AND s1.id = s3.id-2) OR (s1.id = s2.id+1 AND s1.id = s3.id-
66
WHERE s1.people >= 100 AND s2.people >= 100 AND s3.people>=100
77
ORDER BY visit_date;
88

9-
10-
(OR)
11-
9+
------------------------------------------------------------------------------------------------------------------------------------
1210

1311
WITH ranked AS(
1412
SELECT *,
@@ -29,3 +27,21 @@ FROM ranked r
2927
LEFT JOIN consecutive c ON r.diff = c.diff
3028
WHERE c.count >=3
3129
ORDER BY visit_date;
30+
31+
------------------------------------------------------------------------------------------------------------------------------------
32+
33+
WITH ranked AS (
34+
SELECT *,
35+
id-ROW_NUMBER() OVER (ORDER BY id) AS diff
36+
FROM stadium_601
37+
WHERE people >= 100
38+
),
39+
consecutives AS (
40+
SELECT *,
41+
COUNT(id) OVER (PARTITION BY diff) AS cnt
42+
FROM ranked
43+
)
44+
SELECT id,visit_date,people
45+
FROM consecutives
46+
WHERE cnt >= 3
47+
ORDER BY visit_date;

‎medium/180. Consecutive Numbers.sql

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,51 @@
11
-----------------------------------------------------------
2-
Solution 1 :
2+
--Solution 1 :
33
-----------------------------------------------------------
44
WITH cte AS(
55
SELECT id,Num,
66
LEAD(Num,1) OVER() as Next1,
77
LEAD(Num,2) OVER() as Next2
8-
FROM Logs
8+
FROM logs_180
99
)
1010
SELECT DISTINCT Num AS ConsecutiveNums
1111
FROM cte
1212
WHERE Num = Next1 AND Num = Next2;
1313

1414

1515
-----------------------------------------------------------
16-
Solution 2 :
16+
--Solution 2 :
1717
-----------------------------------------------------------
1818

1919
WITH cte AS(
2020
SELECT id,Num,
2121
LAG(Num) OVER() as Prev,
2222
LEAD(Num) OVER() as Next
23-
FROM Logs
23+
FROM logs_180
2424
)
2525
SELECT DISTINCT Num AS ConsecutiveNums
2626
FROM cte
2727
WHERE Num = Prev AND Num = Next;
2828

2929

3030
-----------------------------------------------------------
31-
Solution 3 :
31+
--Solution 3 :
3232
-----------------------------------------------------------
3333
SELECT DISTINCT l1.Num AS ConsecutiveNums
34-
FROM Logs l1
35-
JOIN Logs l2 ON l1.id=l2.id-1 AND l1.Num=l2.Num
36-
JOIN Logs l3 ON l1.id=l3.id-2 AND l2.Num=l3.Num
34+
FROM logs_180 l1
35+
JOIN logs_180 l2 ON l1.id=l2.id-1 AND l1.Num=l2.Num
36+
JOIN logs_180 l3 ON l1.id=l3.id-2 AND l2.Num=l3.Num
37+
38+
39+
-----------------------------------------------------------
40+
--Exensible Solution (Best) :
41+
-----------------------------------------------------------
42+
43+
WITH ranked AS (
44+
SELECT *,
45+
(id-ROW_NUMBER() OVER (PARTITION BY num ORDER BY id)) AS diff
46+
FROM logs_180
47+
)
48+
SELECT DISTINCT num AS "ConsecutiveNums"
49+
FROM ranked
50+
GROUP BY diff,num
51+
HAVING COUNT(id) >= 3;

0 commit comments

Comments
(0)

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