Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Max area of island #43

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 4 commits into from
Nov 29, 2020
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
57 changes: 57 additions & 0 deletions LeetcodeProblems/Databases/Human_Traffic_of_Stadium.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
Human Traffic of Stadium
https://leetcode.com/problems/human-traffic-of-stadium/

Table: Stadium

+---------------+---------+
| Column Name | Type |
+---------------+---------+
| id | int |
| visit_date | date |
| people | int |
+---------------+---------+
visit_date is the primary key for this table.
Each row of this table contains the visit date and visit id to the stadium with the number of people during the visit.
No two rows will have the same visit_date, and as the id increases, the dates increase as well.

Write an SQL query to display the records with three or more rows with consecutive id's, and the number of people is greater than or equal to 100 for each.

Return the result table ordered by visit_date in ascending order.

The query result format is in the following example.

Stadium table:
+------+------------+-----------+
| id | visit_date | people |
+------+------------+-----------+
| 1 | 2017-01-01 | 10 |
| 2 | 2017-01-02 | 109 |
| 3 | 2017-01-03 | 150 |
| 4 | 2017-01-04 | 99 |
| 5 | 2017-01-05 | 145 |
| 6 | 2017-01-06 | 1455 |
| 7 | 2017-01-07 | 199 |
| 8 | 2017-01-09 | 188 |
+------+------------+-----------+

Result table:
+------+------------+-----------+
| id | visit_date | people |
+------+------------+-----------+
| 5 | 2017-01-05 | 145 |
| 6 | 2017-01-06 | 1455 |
| 7 | 2017-01-07 | 199 |
| 8 | 2017-01-09 | 188 |
+------+------------+-----------+
The four rows with ids 5, 6, 7, and 8 have consecutive ids and each of them has >= 100 people attended. Note that row 8 was included even though the visit_date was not the next day after row 7.
The rows with ids 2 and 3 are not included because we need at least three consecutive ids.
*/

SELECT DISTINCT(s1.id) as id, s1.visit_date, s1.people
FROM Stadium AS s1, Stadium AS s2, Stadium AS s3
WHERE
(s1.people >= 100 AND s2.people >= 100 AND s3.people >= 100 AND s1.id = s2.id - 1 AND s2.id = s3.id - 1 AND s1.id = s3.id - 2) OR -- S1, S2, S3
(s1.people >= 100 AND s2.people >= 100 AND s3.people >= 100 AND s1.id = s2.id + 1 AND s2.id = s3.id - 2 AND s1.id = s3.id - 1) OR -- S2. S1. S3
(s1.people >= 100 AND s2.people >= 100 AND s3.people >= 100 AND s1.id = s2.id + 2 AND s2.id = s3.id - 1 AND s1.id = s3.id + 1) -- S2 S3 S1
ORDER BY s1.id ASC
56 changes: 56 additions & 0 deletions LeetcodeProblems/Databases/Rank_Scores.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
Rank Scores
https://leetcode.com/problems/rank-scores/

Write a SQL query to rank scores. If there is a tie between two scores, both should have the same ranking. Note that after a tie, the next ranking number should be the next consecutive integer value. In other words, there should be no "holes" between ranks.

+----+-------+
| Id | Score |
+----+-------+
| 1 | 3.50 |
| 2 | 3.65 |
| 3 | 4.00 |
| 4 | 3.85 |
| 5 | 4.00 |
| 6 | 3.65 |
+----+-------+
For example, given the above Scores table, your query should generate the following report (order by highest score):

+-------+---------+
| score | Rank |
+-------+---------+
| 4.00 | 1 |
| 4.00 | 1 |
| 3.85 | 2 |
| 3.65 | 3 |
| 3.65 | 3 |
| 3.50 | 4 |
+-------+---------+
Important Note: For MySQL solutions, to escape reserved words used as column names, you can use an apostrophe before and after the keyword. For example `Rank`.
*/

-- Solution 1
SELECT Scores.Score, t3.Rank
FROM Scores JOIN (
select Score, (@inc := @inc + 1) as "Rank"
FROM (
SELECT Score as Score
FROM Scores
GROUP BY Score
ORDER BY Score DESC
) AS t1 CROSS JOIN (SELECT @inc := 0 ) as t2
) as t3 ON Scores.Score = t3.Score
ORDER BY t3.Rank ASC


-- Solution 2
-- Set two variables, one to keep the previous Score and other to keep the current Rank
SELECT Score,
@curr := CASE WHEN @previous = (@previous := Score) THEN
CAST(@curr AS SIGNED INTEGER)
ELSE
CAST(@curr + 1 AS SIGNED INTEGER)
END AS "Rank"
FROM Scores, (SELECT @curr := 0, @previous := -1) as t1
ORDER BY SCORE DESC

71 changes: 71 additions & 0 deletions LeetcodeProblems/Databases/Trips_and_Users.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
Trips and Users
https://leetcode.com/problems/trips-and-users/

SQL Schema
The Trips table holds all taxi trips. Each trip has a unique Id, while Client_Id and Driver_Id are both foreign keys to the Users_Id at the Users table. Status is an ENUM type of (‘completed’, ‘cancelled_by_driver’, ‘cancelled_by_client’).

+----+-----------+-----------+---------+--------------------+----------+
| Id | Client_Id | Driver_Id | City_Id | Status |Request_at|
+----+-----------+-----------+---------+--------------------+----------+
| 1 | 1 | 10 | 1 | completed |2013-10-01|
| 2 | 2 | 11 | 1 | cancelled_by_driver|2013-10-01|
| 3 | 3 | 12 | 6 | completed |2013-10-01|
| 4 | 4 | 13 | 6 | cancelled_by_client|2013-10-01|
| 5 | 1 | 10 | 1 | completed |2013-10-02|
| 6 | 2 | 11 | 6 | completed |2013-10-02|
| 7 | 3 | 12 | 6 | completed |2013-10-02|
| 8 | 2 | 12 | 12 | completed |2013-10-03|
| 9 | 3 | 10 | 12 | completed |2013-10-03|
| 10 | 4 | 13 | 12 | cancelled_by_driver|2013-10-03|
+----+-----------+-----------+---------+--------------------+----------+
The Users table holds all users. Each user has an unique Users_Id, and Role is an ENUM type of (‘client’, ‘driver’, ‘partner’).

+----------+--------+--------+
| Users_Id | Banned | Role |
+----------+--------+--------+
| 1 | No | client |
| 2 | Yes | client |
| 3 | No | client |
| 4 | No | client |
| 10 | No | driver |
| 11 | No | driver |
| 12 | No | driver |
| 13 | No | driver |
+----------+--------+--------+
Write a SQL query to find the cancellation rate of requests made by unbanned users (both client and driver must be unbanned) between Oct 1, 2013 and Oct 3, 2013. The cancellation rate is computed by dividing the number of canceled (by client or driver) requests made by unbanned users by the total number of requests made by unbanned users.

For the above tables, your SQL query should return the following rows with the cancellation rate being rounded to two decimal places.

+------------+-------------------+
| Day | Cancellation Rate |
+------------+-------------------+
| 2013-10-01 | 0.33 |
| 2013-10-02 | 0.00 |
| 2013-10-03 | 0.50 |
+------------+-------------------+
*/

-- solution 1
SELECT trips_per_date.Request_at AS "Day", Round(coalesce(cancelled_trips.count_of_cancelled_trips, 0) / trips_per_date.count_of_trips, 2) as "Cancellation Rate"
FROM (
SELECT Trips.Request_at, count(*) as count_of_cancelled_trips
FROM Trips JOIN Users as users_drivers ON Trips.Driver_Id = users_drivers.Users_Id JOIN Users as users_clients ON Trips.Client_Id = users_clients.Users_Id
WHERE users_drivers.Banned = "No" AND users_clients.Banned = "No" AND (Trips.Status = "cancelled_by_driver" OR Trips.Status = "cancelled_by_client")
GROUP BY Trips.Request_at
) as cancelled_trips RIGHT JOIN (
SELECT Request_at, count(*) as count_of_trips
FROM Trips JOIN Users as users_drivers ON Trips.Driver_Id = users_drivers.Users_Id JOIN Users as users_clients ON Trips.Client_Id = users_clients.Users_Id
WHERE users_drivers.Banned = "No" AND users_clients.Banned = "No"
Group by Request_at
) as trips_per_date ON cancelled_trips.Request_at = trips_per_date.Request_at
WHERE trips_per_date.Request_at >= Date("2013-10-01") AND trips_per_date.Request_at <= Date("2013-10-03")
ORDER BY trips_per_date.Request_at

-- solution 2
SELECT Trips.Request_at AS "Day", ROUND(count(IF(Trips.Status = "cancelled_by_driver" OR Trips.Status = "cancelled_by_client",TRUE,null)) / count(*), 2) as "Cancellation Rate"
FROM Trips JOIN Users as users_drivers ON Trips.Driver_Id = users_drivers.Users_Id JOIN Users as users_clients ON Trips.Client_Id = users_clients.Users_Id
WHERE users_drivers.Banned = "No" AND users_clients.Banned = "No" -- AND ()
AND Trips.Request_at >= Date("2013-10-01") AND Trips.Request_at <= Date("2013-10-03")
GROUP BY Trips.Request_at
ORDER BY Trips.Request_at
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,14 @@ To run a specific problem in your console, go to the file test, add the call to
| Algoritmhs |
| - |
| [Heap Sort](/SortingAlgorithms/heapSort.js) |
| [Quick Sort](/SortingAlgorithms/QuickSort.js) |

### Databases
| Problems | Level | Link |
|-|-|-|
| [Trips and Users](/LeetcodeProblems/Databases/Trips_and_Users.sql) | Hard | https://leetcode.com/problems/trips-and-users/ |
| [Human Traffic of Stadium](/LeetcodeProblems/Databases/Human_Traffic_of_Stadium.sql) | Hard | https://leetcode.com/problems/human-traffic-of-stadium |
| [Rank Scores](/LeetcodeProblems/Databases/Rank_Scores.sql) | Medium | https://leetcode.com/problems/rank-scores |
| [Consecutive Numbers](/LeetcodeProblems/Databases/Consecutive_Numbers.sql) | Medium | https://leetcode.com/problems/consecutive-numbers |
| [Department Highest Salary](/LeetcodeProblems/Databases/Department_Highest_Salary.sql) | Medium | https://leetcode.com/problems/department-highest-salary |
| [Exchange Seats](/LeetcodeProblems/Databases/Exchange_Seats.sql) | Medium | https://leetcode.com/problems/exchange-seats |
Expand Down