Skip to content

Commit ea40f85

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

File tree

14 files changed

+415
-0
lines changed

14 files changed

+415
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# 13. Roman to Integer
2+
3+
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.
4+
5+
Symbol Value
6+
I 1
7+
V 5
8+
X 10
9+
L 50
10+
C 100
11+
D 500
12+
M 1000
13+
14+
For example, 2 is written as II in Roman numeral, just two ones added together. 12 is written as XII, which is simply X + II. The number 27 is written as XXVII, which is XX + V + II.
15+
16+
Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used:
17+
18+
I can be placed before V (5) and X (10) to make 4 and 9.
19+
X can be placed before L (50) and C (100) to make 40 and 90.
20+
C can be placed before D (500) and M (1000) to make 400 and 900.
21+
22+
Given a roman numeral, convert it to an integer.
23+
24+
Example 1:
25+
26+
Input: s = "III"
27+
Output: 3
28+
Explanation: III = 3.
29+
30+
Example 2:
31+
32+
Input: s = "LVIII"
33+
Output: 58
34+
Explanation: L = 50, V= 5, III = 3.
35+
36+
Example 3:
37+
38+
Input: s = "MCMXCIV"
39+
Output: 1994
40+
Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution:
2+
def romanToInt(self, s: str) -> int:
3+
numerals = {
4+
"I": 1,
5+
"V": 5,
6+
"X": 10,
7+
"L": 50,
8+
"C": 100,
9+
"D": 500,
10+
"M": 1000,
11+
}
12+
13+
summ = 0
14+
prev_val = 0
15+
for num in s[::]:
16+
val = numerals.get(num)
17+
if prev_val < val:
18+
summ += val - prev_val * 2
19+
else:
20+
summ += val
21+
prev_val = val
22+
23+
return summ
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# 180. Consecutive Numbers
2+
3+
Table: Logs
4+
5+
+-------------+---------+
6+
| Column Name | Type |
7+
+-------------+---------+
8+
| id | int |
9+
| num | varchar |
10+
+-------------+---------+
11+
In SQL, id is the primary key for this table.
12+
id is an autoincrement column starting from 1.
13+
14+
15+
Find all numbers that appear at least three times consecutively.
16+
17+
Return the result table in any order.
18+
19+
The result format is in the following example.
20+
21+
22+
Example 1:
23+
24+
Input:
25+
26+
Logs table:
27+
28+
+----+-----+
29+
| id | num |
30+
+----+-----+
31+
| 1 | 1 |
32+
| 2 | 1 |
33+
| 3 | 1 |
34+
| 4 | 2 |
35+
| 5 | 1 |
36+
| 6 | 2 |
37+
| 7 | 2 |
38+
+----+-----+
39+
40+
Output:
41+
42+
+-----------------+
43+
| ConsecutiveNums |
44+
+-----------------+
45+
| 1 |
46+
+-----------------+
47+
48+
Explanation: 1 is the only number that appears consecutively for at least three times.
49+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
SELECT DISTINCT l1.num AS "ConsecutiveNums"
2+
FROM Logs l1
3+
JOIN Logs l2 ON l2.id = l1.id + 1 AND l2.num = l1.num
4+
JOIN Logs l3 ON l3.id = l2.id + 1 AND l3.num = l1.num;
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# 181. Employees Earning More Than Their Managers
2+
3+
Table: Employee
4+
5+
+-------------+---------+
6+
| Column Name | Type |
7+
+-------------+---------+
8+
| id | int |
9+
| name | varchar |
10+
| salary | int |
11+
| managerId | int |
12+
+-------------+---------+
13+
id is the primary key (column with unique values) for this table.
14+
Each row of this table indicates the ID of an employee, their name, salary, and the ID of their manager.
15+
16+
17+
Write a solution to find the employees who earn more than their managers.
18+
19+
Return the result table in any order.
20+
21+
The result format is in the following example.
22+
23+
Example 1:
24+
25+
Input:
26+
27+
Employee table:
28+
29+
+----+-------+--------+-----------+
30+
| id | name | salary | managerId |
31+
+----+-------+--------+-----------+
32+
| 1 | Joe | 70000 | 3 |
33+
| 2 | Henry | 80000 | 4 |
34+
| 3 | Sam | 60000 | Null |
35+
| 4 | Max | 90000 | Null |
36+
+----+-------+--------+-----------+
37+
38+
Output:
39+
40+
+----------+
41+
| Employee |
42+
+----------+
43+
| Joe |
44+
+----------+
45+
46+
Explanation: Joe is the only employee who earns more than his manager.
47+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
SELECT e.name AS "Employee"
2+
FROM Employee e
3+
JOIN Employee e2 ON e.managerId = e2.id
4+
WHERE e.salary > e2.salary
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# 182. Duplicate Emails
2+
3+
Table: Person
4+
5+
+-------------+---------+
6+
| Column Name | Type |
7+
+-------------+---------+
8+
| id | int |
9+
| email | varchar |
10+
+-------------+---------+
11+
id is the primary key (column with unique values) for this table.
12+
Each row of this table contains an email. The emails will not contain uppercase letters.
13+
14+
15+
Write a solution to report all the duplicate emails. Note that it's guaranteed that the email field is not NULL.
16+
17+
Return the result table in any order.
18+
19+
The result format is in the following example.
20+
21+
Example 1:
22+
23+
Input:
24+
25+
Person table:
26+
27+
+----+---------+
28+
| id | email |
29+
+----+---------+
30+
| 1 | a@b.com |
31+
| 2 | c@d.com |
32+
| 3 | a@b.com |
33+
+----+---------+
34+
35+
Output:
36+
37+
+---------+
38+
| Email |
39+
+---------+
40+
| a@b.com |
41+
+---------+
42+
43+
Explanation: a@b.com is repeated two times.
44+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
SELECT email AS "Email"
2+
FROM Person
3+
GROUP BY email
4+
HAVING COUNT(*) > 1;
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# 183. Customers Who Never Order
2+
3+
Table: Customers
4+
5+
+-------------+---------+
6+
| Column Name | Type |
7+
+-------------+---------+
8+
| id | int |
9+
| name | varchar |
10+
+-------------+---------+
11+
12+
id is the primary key (column with unique values) for this table.
13+
Each row of this table indicates the ID and name of a customer.
14+
15+
16+
17+
Table: Orders
18+
19+
+-------------+------+
20+
| Column Name | Type |
21+
+-------------+------+
22+
| id | int |
23+
| customerId | int |
24+
+-------------+------+
25+
26+
id is the primary key (column with unique values) for this table.
27+
customerId is a foreign key (reference columns) of the ID from the Customers table.
28+
Each row of this table indicates the ID of an order and the ID of the customer who ordered it.
29+
30+
31+
32+
Write a solution to find all customers who never order anything.
33+
34+
Return the result table in any order.
35+
36+
The result format is in the following example.
37+
38+
39+
40+
Example 1:
41+
42+
Input:
43+
44+
Customers table:
45+
46+
+----+-------+
47+
| id | name |
48+
+----+-------+
49+
| 1 | Joe |
50+
| 2 | Henry |
51+
| 3 | Sam |
52+
| 4 | Max |
53+
+----+-------+
54+
55+
Orders table:
56+
57+
+----+------------+
58+
| id | customerId |
59+
+----+------------+
60+
| 1 | 3 |
61+
| 2 | 1 |
62+
+----+------------+
63+
64+
Output:
65+
66+
+-----------+
67+
| Customers |
68+
+-----------+
69+
| Henry |
70+
| Max |
71+
+-----------+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
SELECT c.name AS "Customers"
2+
FROM Customers c
3+
LEFT JOIN Orders o ON c.id = o.customerId
4+
WHERE o.customerId IS NULL

0 commit comments

Comments
 (0)