Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
kamyu104 committed Oct 12, 2018
1 parent e8ad0e8 commit d39f1ac
Show file tree
Hide file tree
Showing 49 changed files with 663 additions and 28 deletions.
2 changes: 1 addition & 1 deletion LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2018 https://github.com/kamyu104/LeetCode-Solution
Copyright (c) 2018 https://github.com/kamyu104/LeetCode-Solutions

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
24 changes: 24 additions & 0 deletions MySQL/average-salary-departments-vs-company.sql
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
;

7 changes: 7 additions & 0 deletions MySQL/big-countries.sql
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;

20 changes: 20 additions & 0 deletions MySQL/biggest-single-number.sql
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
;

9 changes: 9 additions & 0 deletions MySQL/classes-more-than-5-students.sql
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;

6 changes: 6 additions & 0 deletions MySQL/combine-two-tables.sql
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

10 changes: 10 additions & 0 deletions MySQL/consecutive-available-seats.sql
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
;

17 changes: 17 additions & 0 deletions MySQL/consecutive-numbers.sql
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

13 changes: 13 additions & 0 deletions MySQL/count-student-number-in-department.sql
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
;

20 changes: 20 additions & 0 deletions MySQL/customer-placing-the-largest-number-of-orders.sql
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
;

7 changes: 7 additions & 0 deletions MySQL/customers-who-never-order.sql
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

7 changes: 7 additions & 0 deletions MySQL/delete-duplicate-emails.sql
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

14 changes: 14 additions & 0 deletions MySQL/department-highest-salary.sql
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)

9 changes: 9 additions & 0 deletions MySQL/department-top-three-salaries.sql
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;

5 changes: 5 additions & 0 deletions MySQL/duplicate-emails.sql
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

13 changes: 13 additions & 0 deletions MySQL/employee-bonus.sql
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
;

13 changes: 13 additions & 0 deletions MySQL/employees-earning-more-than-their-managers.sql
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)

11 changes: 11 additions & 0 deletions MySQL/exchange-seats.sql
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;

23 changes: 23 additions & 0 deletions MySQL/find-cumulative-salary-of-an-employee.sql
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

7 changes: 7 additions & 0 deletions MySQL/find-customer-referee.sql
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;

16 changes: 16 additions & 0 deletions MySQL/find-median-given-frequency-of-numbers.sql
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))

12 changes: 12 additions & 0 deletions MySQL/friend-requests-i-overall-acceptance-rate.sql
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;

15 changes: 15 additions & 0 deletions MySQL/friend-requests-ii-who-has-the-most-friends.sql
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
;

11 changes: 11 additions & 0 deletions MySQL/get-highest-answer-rate-question.sql
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;

17 changes: 17 additions & 0 deletions MySQL/human-traffic-of-stadium.sql
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
;

30 changes: 30 additions & 0 deletions MySQL/investments-in-2016.sql
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
)
;

17 changes: 17 additions & 0 deletions MySQL/managers-with-at-least-5-direct-reports.sql
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
;

Loading

0 comments on commit d39f1ac

Please sign in to comment.