forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
49 changed files
with
663 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# Time: O(nlogn) | ||
# Space: O(n) | ||
|
||
SELECT department_salary.pay_month, department_id, | ||
CASE | ||
WHEN department_avg < company_avg THEN 'lower' | ||
WHEN department_avg > company_avg THEN 'higher' | ||
ELSE 'same' | ||
END AS comparison | ||
FROM | ||
( | ||
SELECT department_id, AVG(amount) AS department_avg, date_format(pay_date, '%Y-%m') AS pay_month | ||
FROM salary JOIN employee ON salary.employee_id = employee.employee_id | ||
GROUP BY department_id, pay_month | ||
) AS department_salary | ||
JOIN | ||
( | ||
SELECT AVG(amount) AS company_avg, date_format(pay_date, '%Y-%m') AS pay_month | ||
FROM salary | ||
GROUP BY date_format(pay_date, '%Y-%m') | ||
) AS company_salary | ||
ON department_salary.pay_month = company_salary.pay_month | ||
; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Time: O(n) | ||
# Space: O(1) | ||
|
||
SELECT name, population, area | ||
FROM World | ||
WHERE area > 3000000 OR population > 25000000; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Time: O(n) | ||
# Space: O(n) | ||
|
||
SELECT | ||
IFNULL( | ||
(SELECT | ||
MAX(num) | ||
FROM | ||
(SELECT | ||
num | ||
FROM | ||
number | ||
GROUP BY num | ||
HAVING COUNT(num) = 1 | ||
ORDER BY NULL) AS t | ||
) | ||
, NULL | ||
) AS num | ||
; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Time: O(n) | ||
# Space: O(n) | ||
|
||
SELECT class | ||
FROM courses | ||
GROUP BY class | ||
HAVING COUNT(DISTINCT student) >= 5 | ||
ORDER BY NULL; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# Time: O(n), n is size of Person Table | ||
# Space: O(n) | ||
|
||
SELECT FirstName, LastName, City, State FROM Person LEFT JOIN Address | ||
ON Person.PersonId=Address.PersonId | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# Time: O(nlogn) | ||
# Space: O(n) | ||
|
||
SELECT DISTINCT c1.seat_id | ||
FROM cinema c1 JOIN cinema c2 | ||
ON ((c1.seat_id = c2.seat_id - 1) OR (c1.seat_id = c2.seat_id + 1)) | ||
AND c1.free = true AND c2.free = true | ||
ORDER BY c1.seat_id | ||
; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Time: O(n) | ||
# Space: O(n) | ||
|
||
SELECT DISTINCT(Num) AS ConsecutiveNums | ||
FROM ( | ||
SELECT | ||
Num, | ||
@counter := IF(@prev = Num, @counter + 1, 1) AS how_many_cnt_in_a_row, | ||
@prev := Num | ||
FROM Logs y, (SELECT @counter:=1, @prev:=NULL) vars | ||
) sq | ||
WHERE how_many_cnt_in_a_row >= 3 | ||
|
||
SELECT DISTINCT l1.Num as ConsecutiveNums | ||
FROM Logs l1, Logs l2, Logs l3 | ||
WHERE l1.Id + 1 = l2.Id AND l2.Id + 1 = l3.Id AND l1.Num = l2.Num AND l2.Num = l3.Num | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Time: O(s+dlogd) | ||
# Space: O(d+s) | ||
|
||
SELECT | ||
dept_name, COUNT(student_id) AS student_number | ||
FROM | ||
department | ||
LEFT JOIN | ||
student ON department.dept_id = student.dept_id | ||
GROUP BY department.dept_name | ||
ORDER BY student_number DESC , department.dept_name | ||
; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Time: O(n) | ||
# Space: O(n) | ||
|
||
SELECT customer_number | ||
FROM orders | ||
GROUP BY customer_number | ||
HAVING COUNT(order_number) = | ||
(SELECT MAX(*) | ||
FROM | ||
(SELECT COUNT(*) AS cnt | ||
FROM | ||
orders | ||
GROUP BY customer_number | ||
ORDER BY NULL | ||
) AS cnt_tbl | ||
) | ||
ORDER BY NULL | ||
LIMIT 1 | ||
; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Time: O(n^2) | ||
# Space: O(1) | ||
|
||
SELECT Name AS Customers FROM Customers WHERE Id NOT IN (SELECT CustomerId FROM Orders) | ||
|
||
SELECT Customers.Name AS Customers FROM (Customers LEFT JOIN Orders ON Customers.Id = Orders.CustomerId) WHERE Orders.CustomerId IS NULL | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Time: O(n^2) | ||
# Space: O(n) | ||
|
||
DELETE p1 | ||
FROM Person p1, Person p2 | ||
WHERE p1.Email = p2.Email AND p1.Id > p2.Id | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# Time: O(n^2) | ||
# Space: O(n) | ||
|
||
SELECT d.Department AS Department, e.Name AS Employee, d.Salary AS Salary | ||
FROM (SELECT Department.Id AS DepartmentId, Department.Name AS Department, emp.Salary AS Salary | ||
FROM Department JOIN (SELECT DepartmentId, MAX(Salary) AS Salary FROM Employee GROUP BY DepartmentId) emp | ||
ON Department.Id = emp.DepartmentId) d | ||
JOIN Employee e | ||
ON e.DepartmentId = d.DepartmentId and e.Salary = d.Salary | ||
|
||
SELECT Department.Name AS Department, Employee.Name AS Employee, Employee.Salary AS Salary | ||
FROM Department JOIN Employee ON Employee.DepartmentId = Department.Id | ||
WHERE Employee.Salary IN (SELECT MAX(e.Salary) FROM Employee e WHERE e.DepartmentId = Employee.DepartmentId) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Time: O(n^2) | ||
# Space: O(n) | ||
|
||
SELECT D.Name AS Department, E.Name AS Employee, E.Salary AS Salary | ||
FROM Employee E INNER JOIN Department D ON E.DepartmentId = D.Id | ||
WHERE (SELECT COUNT(DISTINCT(Salary)) FROM Employee | ||
WHERE DepartmentId = E.DepartmentId AND Salary > E.Salary) < 3 | ||
ORDER by E.DepartmentId, E.Salary DESC; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Time: O(n^2) | ||
# Space: O(n) | ||
|
||
SELECT Email FROM Person GROUP BY Email HAVING COUNT(*) > 1 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Time: O(n) if hash join, O(nlogn) if merge join | ||
# Space: O(n) | ||
|
||
SELECT | ||
Employee.name, Bonus.bonus | ||
FROM | ||
Employee | ||
LEFT JOIN | ||
Bonus ON Employee.empid = Bonus.empid | ||
WHERE | ||
Bonus.bonus < 1000 OR Bonus.bonus IS NULL | ||
; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Time: O(n^2) | ||
# Space: O(1) | ||
|
||
SELECT e.Name AS Employee FROM Employee e LEFT JOIN Employee b | ||
ON e.ManagerId=b.Id | ||
WHERE e.Salary > b.Salary | ||
|
||
SELECT Name AS Employee | ||
FROM Employee e | ||
WHERE e.ManagerId IS NOT NULL AND e.Salary > (SELECT Salary | ||
FROM Employee | ||
WHERE e.ManagerId = Id) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# Time: O(nlogn) | ||
# Space: O(n) | ||
|
||
SELECT | ||
s1.id, COALESCE(s2.student, s1.student) AS student | ||
FROM | ||
seat s1 | ||
LEFT JOIN | ||
seat s2 ON ((s1.id + 1) ^ 1) - 1 = s2.id | ||
ORDER BY s1.id; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Time: O(n^2) | ||
# Space: O(n) | ||
|
||
SELECT EA.Id, | ||
EA.Month, | ||
SUM(EB.salary) AS Salary | ||
FROM (SELECT E1.* | ||
FROM employee E1 | ||
LEFT JOIN (SELECT id, | ||
MAX(month) AS month | ||
FROM employee | ||
GROUP BY id) E2 | ||
ON E1.id = E2.id | ||
WHERE E1.month < E2.month) EA | ||
LEFT JOIN employee EB | ||
ON EA.id = EB.id | ||
WHERE EA.month - 2 <= EB.month | ||
AND EB.month <= EA.month | ||
GROUP BY EA.id, | ||
EA.month | ||
ORDER BY EA.id, | ||
month DESC | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Time: O(n) | ||
# Space: O(1) | ||
|
||
SELECT name | ||
FROM customer | ||
WHERE referee_id is NULL OR referee_id != 2; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# Time: O(nlogn) | ||
# Space: O(n) | ||
|
||
SELECT AVG(n.Number) AS median | ||
FROM Numbers n LEFT JOIN | ||
( | ||
SELECT Number, @prev := @count AS prevNumber, (@count := @count + Frequency) AS countNumber | ||
FROM Numbers, | ||
(SELECT @count := 0, @prev := 0, @total := (SELECT SUM(Frequency) FROM Numbers)) temp ORDER BY Number | ||
) n2 | ||
ON n.Number = n2.Number | ||
WHERE | ||
(prevNumber < floor((@total+1)/2) AND countNumber >= floor((@total+1)/2)) | ||
OR | ||
(prevNumber < floor((@total+2)/2) AND countNumber >= floor((@total+2)/2)) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# Time: O(rlogr + aloga) | ||
# Space: O(r + a) | ||
|
||
SELECT | ||
ROUND( | ||
IFNULL( | ||
(SELECT COUNT(*) FROM (SELECT DISTINCT requester_id, accepter_id FROM request_accepted) AS r) | ||
/ | ||
(SELECT COUNT(*) FROM (SELECT DISTINCT sender_id, send_to_id FROM friend_request) AS a), | ||
0) | ||
, 2) AS accept_rate; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# Time: O(nlogn) | ||
# Space: O(n) | ||
|
||
SELECT ids as id, COUNT(*) AS num | ||
FROM | ||
( | ||
SELECT requester_id AS ids FROM request_accepted | ||
UNION ALL | ||
SELECT accepter_id FROM request_accepted | ||
) AS tmp | ||
GROUP BY ids | ||
ORDER BY num DESC | ||
LIMIT 1 | ||
; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# Time: O(nlogn) | ||
# Space: O(n) | ||
|
||
SELECT | ||
question_id AS 'survey_log' | ||
FROM | ||
survey_log | ||
GROUP BY question_id | ||
ORDER BY COUNT(answer_id) / COUNT(IF(action = 'show', 1, 0)) DESC | ||
LIMIT 1; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Time: O(n^3) | ||
# Space: O(n^3) | ||
|
||
SELECT DISTINCT s1.* | ||
FROM stadium AS s1, stadium AS s2, stadium AS s3 | ||
WHERE s1.people >= 100 AND s2.people >= 100 AND s3.people >= 100 | ||
AND | ||
( | ||
(s2.id = s1.id + 1 AND s3.id = s2.id + 1 AND s3.id = s1.id + 2) -- s1, s2, s3 | ||
OR | ||
(s1.id = s2.id + 1 AND s3.id = s1.id + 1 AND s3.id = s2.id + 2) -- s2, s1, s3 | ||
OR | ||
(s3.id = s2.id + 1 AND s1.id = s3.id + 1 AND s1.id = s2.id + 2) -- s2, s3, s1 | ||
) | ||
ORDER BY s1.id | ||
; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# Time: O(n^2) | ||
# Space: O(n) | ||
|
||
SELECT | ||
SUM(insurance.TIV_2016) AS TIV_2016 | ||
FROM | ||
insurance | ||
WHERE | ||
insurance.TIV_2015 IN | ||
( | ||
SELECT | ||
TIV_2015 | ||
FROM | ||
insurance | ||
GROUP BY TIV_2015 | ||
HAVING COUNT(*) > 1 | ||
ORDER BY NULL | ||
) | ||
AND CONCAT(LAT, LON) IN | ||
( | ||
SELECT | ||
CONCAT(LAT, LON) | ||
FROM | ||
insurance | ||
GROUP BY LAT , LON | ||
HAVING COUNT(*) = 1 | ||
ORDER BY NULL | ||
) | ||
; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Time: O(n) | ||
# Space: O(n) | ||
|
||
SELECT | ||
Name | ||
FROM | ||
Employee AS t1 INNER JOIN | ||
(SELECT | ||
ManagerId | ||
FROM | ||
Employee | ||
GROUP BY ManagerId | ||
HAVING COUNT(ManagerId) >= 5 | ||
ORDER BY NULL) AS t2 | ||
ON t1.Id = t2.ManagerId | ||
; | ||
|
Oops, something went wrong.