Skip to content

Commit ed7d4ac

Browse files
committed
feat(leetcode): new easy/medium solutions
1 parent e7a8753 commit ed7d4ac

File tree

12 files changed

+195
-0
lines changed

12 files changed

+195
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# 175. Combine Two Tables
2+
3+
Table: Person
4+
5+
+-------------+---------+
6+
| Column Name | Type |
7+
+-------------+---------+
8+
| personId | int |
9+
| lastName | varchar |
10+
| firstName | varchar |
11+
+-------------+---------+
12+
personId is the primary key (column with unique values) for this table.
13+
This table contains information about the ID of some persons and their first and last names.
14+
15+
16+
17+
Table: Address
18+
19+
+-------------+---------+
20+
| Column Name | Type |
21+
+-------------+---------+
22+
| addressId | int |
23+
| personId | int |
24+
| city | varchar |
25+
| state | varchar |
26+
+-------------+---------+
27+
addressId is the primary key (column with unique values) for this table.
28+
Each row of this table contains information about the city and state of one person with ID = PersonId.
29+
30+
31+
32+
Write a solution to report the first name, last name, city, and state of each person in the Person table. If the address of a personId is not present in the Address table, report null instead.
33+
34+
Return the result table in any order.
35+
36+
The result format is in the following example.
37+
38+
[Link to problem](https://leetcode.com/problems/combine-two-tables/)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
SELECT p.firstName, p.lastName, a.city, a.state
2+
FROM Person as p
3+
LEFT JOIN Address as a ON a.personId = p.personId
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# 176. Second Highest Salary
2+
3+
Table: Employee
4+
5+
+-------------+------+
6+
| Column Name | Type |
7+
+-------------+------+
8+
| id | int |
9+
| salary | int |
10+
+-------------+------+
11+
id is the primary key (column with unique values) for this table.
12+
Each row of this table contains information about the salary of an employee.
13+
14+
15+
16+
Write a solution to find the second highest distinct salary from the Employee table. If there is no second highest salary, return null (return None in Pandas).
17+
18+
The result format is in the following example.
19+
20+
[Link to problem](https://leetcode.com/problems/second-highest-salary/)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
SELECT (
2+
SELECT DISTINCT salary
3+
FROM Employee
4+
ORDER BY salary DESC
5+
OFFSET 1 LIMIT 1
6+
) AS "SecondHighestSalary";
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# 177. Nth Highest Salary
2+
3+
Table: Employee
4+
5+
+-------------+------+
6+
| Column Name | Type |
7+
+-------------+------+
8+
| id | int |
9+
| salary | int |
10+
+-------------+------+
11+
id is the primary key (column with unique values) for this table.
12+
Each row of this table contains information about the salary of an employee.
13+
14+
15+
16+
Write a solution to find the nth highest distinct salary from the Employee table. If there are less than n distinct salaries, return null.
17+
18+
The result format is in the following example.
19+
20+
[Link to problem](https://leetcode.com/problems/nth-highest-salary/)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
CREATE OR REPLACE FUNCTION NthHighestSalary(N INT) RETURNS TABLE (Salary INT) AS $$
2+
BEGIN
3+
IF n <= 0 THEN
4+
RETURN;
5+
END IF;
6+
7+
RETURN QUERY
8+
SELECT DISTINCT e.salary
9+
FROM employee e
10+
ORDER BY salary DESC
11+
OFFSET n-1 LIMIT 1;
12+
END;
13+
$$ LANGUAGE plpgsql;
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# 178. Rank Scores
2+
3+
Table: Scores
4+
5+
+-------------+---------+
6+
| Column Name | Type |
7+
+-------------+---------+
8+
| id | int |
9+
| score | decimal |
10+
+-------------+---------+
11+
id is the primary key (column with unique values) for this table.
12+
Each row of this table contains the score of a game. Score is a floating point value with two decimal places.
13+
14+
15+
16+
Write a solution to find the rank of the scores. The ranking should be calculated according to the following rules:
17+
18+
The scores should be ranked from the highest to the lowest.
19+
If there is a tie between two scores, both should have the same ranking.
20+
After a tie, the next ranking number should be the next consecutive integer value. In other words, there should be no holes between ranks.
21+
22+
Return the result table ordered by score in descending order.
23+
24+
The result format is in the following example.
25+
26+
[Link to problem](https://leetcode.com/problems/rank-scores/)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
SELECT score, DENSE_RANK() OVER (ORDER BY score DESC) rank
2+
FROM Scores
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# 1935. Maximum Number of Words You Can Type
2+
3+
There is a malfunctioning keyboard where some letter keys do not work. All other keys on the keyboard work properly.
4+
5+
Given a string text of words separated by a single space (no leading or trailing spaces) and a string brokenLetters of all distinct letter keys that are broken, return the number of words in text you can fully type using this keyboard.
6+
7+
[Link to problem](https://leetcode.com/problems/maximum-number-of-words-you-can-types)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def canBeTypedWords(self, text: str, brokenLetters: str) -> int:
3+
count = 0
4+
for word in text.split(" "):
5+
for broken_letter in brokenLetters:
6+
if word.find(broken_letter) != -1:
7+
break
8+
else:
9+
count += 1
10+
11+
return count

0 commit comments

Comments
 (0)