Skip to content

Commit 4158748

Browse files
authored
Added tasks 183, 184, 185, 187.
1 parent f42fa79 commit 4158748

File tree

13 files changed

+554
-0
lines changed

13 files changed

+554
-0
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.5'
621621

622622
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
623623
|-|-|-|-|-|-
624+
| 0183 |[Customers Who Never Order](src/main/kotlin/g0101_0200/s0183_customers_who_never_order/script.sql)| Easy | Database | 376 | 98.73
624625

625626
#### Day 2 Select and Order
626627

@@ -910,6 +911,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.5'
910911
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
911912
|-|-|-|-|-|-
912913
| 0014 |[Longest Common Prefix](src/main/kotlin/g0001_0100/s0014_longest_common_prefix/Solution.kt)| Easy | Top_Interview_Questions, String | 209 | 88.86
914+
| 0187 |[Repeated DNA Sequences](src/main/kotlin/g0101_0200/s0187_repeated_dna_sequences/Solution.kt)| Medium | String, Hash_Table, Bit_Manipulation, Sliding_Window, Hash_Function, Rolling_Hash | 319 | 79.03
913915
| 0003 |[Longest Substring Without Repeating Characters](src/main/kotlin/g0001_0100/s0003_longest_substring_without_repeating_characters/Solution.kt)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Sliding_Window | 258 | 91.09
914916
| 0020 |[Valid Parentheses](src/main/kotlin/g0001_0100/s0020_valid_parentheses/Solution.kt)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, String, Stack | 226 | 72.53
915917
| 0005 |[Longest Palindromic Substring](src/main/kotlin/g0001_0100/s0005_longest_palindromic_substring/Solution.kt)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Dynamic_Programming | 323 | 75.48
@@ -1217,6 +1219,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.5'
12171219

12181220
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
12191221
|-|-|-|-|-|-
1222+
| 0187 |[Repeated DNA Sequences](src/main/kotlin/g0101_0200/s0187_repeated_dna_sequences/Solution.kt)| Medium | String, Hash_Table, Bit_Manipulation, Sliding_Window, Hash_Function, Rolling_Hash | 319 | 79.03
12201223
| 0005 |[Longest Palindromic Substring](src/main/kotlin/g0001_0100/s0005_longest_palindromic_substring/Solution.kt)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Dynamic_Programming | 323 | 75.48
12211224

12221225
#### Day 10 Linked List
@@ -1564,6 +1567,10 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.5'
15641567
| 0199 |[Binary Tree Right Side View](src/main/kotlin/g0101_0200/s0199_binary_tree_right_side_view/Solution.kt)| Medium | Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Data_Structure_II_Day_16_Tree, Level_2_Day_15_Tree | 194 | 92.89
15651568
| 0198 |[House Robber](src/main/kotlin/g0101_0200/s0198_house_robber/Solution.kt)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Algorithm_I_Day_12_Dynamic_Programming, Dynamic_Programming_I_Day_3, Level_2_Day_12_Dynamic_Programming, Udemy_Dynamic_Programming | 156 | 92.24
15661569
| 0189 |[Rotate Array](src/main/kotlin/g0101_0200/s0189_rotate_array/Solution.kt)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Math, Two_Pointers, Algorithm_I_Day_2_Two_Pointers, Udemy_Arrays | 483 | 86.95
1570+
| 0187 |[Repeated DNA Sequences](src/main/kotlin/g0101_0200/s0187_repeated_dna_sequences/Solution.kt)| Medium | String, Hash_Table, Bit_Manipulation, Sliding_Window, Hash_Function, Rolling_Hash, Data_Structure_II_Day_9_String, Udemy_Strings | 319 | 79.03
1571+
| 0185 |[Department Top Three Salaries](src/main/kotlin/g0101_0200/s0185_department_top_three_salaries/script.sql)| Hard | Database | 643 | 95.52
1572+
| 0184 |[Department Highest Salary](src/main/kotlin/g0101_0200/s0184_department_highest_salary/script.sql)| Medium | Database | 590 | 73.49
1573+
| 0183 |[Customers Who Never Order](src/main/kotlin/g0101_0200/s0183_customers_who_never_order/script.sql)| Easy | Database, SQL_I_Day_1_Select | 376 | 98.73
15671574
| 0182 |[Duplicate Emails](src/main/kotlin/g0101_0200/s0182_duplicate_emails/script.sql)| Easy | Database, SQL_I_Day_10_Where | 303 | 92.08
15681575
| 0181 |[Employees Earning More Than Their Managers](src/main/kotlin/g0101_0200/s0181_employees_earning_more_than_their_managers/script.sql)| Easy | Database | 315 | 94.44
15691576
| 0180 |[Consecutive Numbers](src/main/kotlin/g0101_0200/s0180_consecutive_numbers/script.sql)| Medium | Database | 550 | 48.44
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
183\. Customers Who Never Order
2+
3+
Easy
4+
5+
SQL Schema
6+
7+
Table: `Customers`
8+
9+
+-------------+---------+
10+
| Column Name | Type |
11+
+-------------+---------+
12+
| id | int |
13+
| name | varchar |
14+
+-------------+---------+
15+
id is the primary key column for this table.
16+
Each row of this table indicates the ID and name of a customer.
17+
18+
Table: `Orders`
19+
20+
+-------------+------+
21+
| Column Name | Type |
22+
+-------------+------+
23+
| id | int |
24+
| customerId | int |
25+
+-------------+------+
26+
id is the primary key column for this table.
27+
customerId is a foreign key 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+
Write an SQL query to report all customers who never order anything.
31+
32+
Return the result table in **any order**.
33+
34+
The query result format is in the following example.
35+
36+
**Example 1:**
37+
38+
**Input:**
39+
40+
Customers table:
41+
+----+-------+
42+
| id | name |
43+
+----+-------+
44+
| 1 | Joe |
45+
| 2 | Henry |
46+
| 3 | Sam |
47+
| 4 | Max |
48+
+----+-------+
49+
50+
Orders table:
51+
+----+------------+
52+
| id | customerId |
53+
+----+------------+
54+
| 1 | 3 |
55+
| 2 | 1 |
56+
+----+------------+
57+
58+
**Output:**
59+
60+
+-----------+
61+
| Customers |
62+
+-----------+
63+
| Henry |
64+
| Max |
65+
+-----------+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Write your MySQL query statement below
2+
# #Easy #Database #SQL_I_Day_1_Select #2022_06_27_Time_376_ms_(98.73%)_Space_0B_(100.00%)
3+
SELECT c.Name as Customers
4+
FROM Customers as c
5+
LEFT JOIN Orders as o
6+
ON c.Id = o.CustomerId
7+
WHERE o.CustomerId is null
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
184\. Department Highest Salary
2+
3+
Medium
4+
5+
SQL Schema
6+
7+
Table: `Employee`
8+
9+
+--------------+---------+
10+
| Column Name | Type |
11+
+--------------+---------+
12+
| id | int |
13+
| name | varchar |
14+
| salary | int |
15+
| departmentId | int |
16+
+--------------+---------+
17+
id is the primary key column for this table.
18+
departmentId is a foreign key of the ID from the `Department` table.
19+
Each row of this table indicates the ID, name, and salary of an employee. It also contains the ID of their department.
20+
21+
Table: `Department`
22+
23+
+-------------+---------+
24+
| Column Name | Type |
25+
+-------------+---------+
26+
| id | int |
27+
| name | varchar |
28+
+-------------+---------+
29+
id is the primary key column for this table.
30+
Each row of this table indicates the ID of a department and its name.
31+
32+
Write an SQL query to find employees who have the highest salary in each of the departments.
33+
34+
Return the result table in **any order**.
35+
36+
The query result format is in the following example.
37+
38+
**Example 1:**
39+
40+
**Input:**
41+
42+
Employee table:
43+
+----+-------+--------+--------------+
44+
| id | name | salary | departmentId |
45+
+----+-------+--------+--------------+
46+
| 1 | Joe | 70000 | 1 |
47+
| 2 | Jim | 90000 | 1 |
48+
| 3 | Henry | 80000 | 2 |
49+
| 4 | Sam | 60000 | 2 |
50+
| 5 | Max | 90000 | 1 |
51+
+----+-------+--------+--------------+
52+
53+
Department table:
54+
+----+-------+
55+
| id | name |
56+
+----+-------+
57+
| 1 | IT |
58+
| 2 | Sales |
59+
+----+-------+
60+
61+
**Output:**
62+
63+
+------------+----------+--------+
64+
| Department | Employee | Salary |
65+
+------------+----------+--------+
66+
| IT | Jim | 90000 |
67+
| Sales | Henry | 80000 |
68+
| IT | Max | 90000 |
69+
+------------+----------+--------+
70+
71+
**Explanation:** Max and Jim both have the highest salary in the IT department and Henry has the highest salary in the Sales department.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Write your MySQL query statement below
2+
# #Medium #Database #2022_06_27_Time_590_ms_(73.49%)_Space_0B_(100.00%)
3+
SELECT
4+
d.Name AS Department,
5+
Sel.Name AS Employee,
6+
Sel.Salary AS Salary
7+
FROM
8+
(
9+
SELECT
10+
Name,
11+
Salary,
12+
DepartmentId,
13+
DENSE_RANK() OVER (PARTITION BY DepartmentId ORDER BY Salary DESC) AS dr
14+
FROM Employee
15+
) AS Sel
16+
INNER JOIN Department d ON d.Id = Sel.DepartmentId
17+
WHERE Sel.dr = 1
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
185\. Department Top Three Salaries
2+
3+
Hard
4+
5+
SQL Schema
6+
7+
Table: `Employee`
8+
9+
+--------------+---------+
10+
| Column Name | Type |
11+
+--------------+---------+
12+
| id | int |
13+
| name | varchar |
14+
| salary | int |
15+
| departmentId | int |
16+
+--------------+---------+
17+
id is the primary key column for this table.
18+
departmentId is a foreign key of the ID from the `Department` table. Each row of this table indicates the ID, name, and salary of an employee. It also contains the ID of their department.
19+
20+
Table: `Department`
21+
22+
+-------------+---------+
23+
| Column Name | Type |
24+
+-------------+---------+
25+
| id | int |
26+
| name | varchar |
27+
+-------------+---------+
28+
id is the primary key column for this table.
29+
Each row of this table indicates the ID of a department and its name.
30+
31+
A company's executives are interested in seeing who earns the most money in each of the company's departments. A **high earner** in a department is an employee who has a salary in the **top three unique** salaries for that department.
32+
33+
Write an SQL query to find the employees who are **high earners** in each of the departments.
34+
35+
Return the result table **in any order**.
36+
37+
The query result format is in the following example.
38+
39+
**Example 1:**
40+
41+
**Input:**
42+
43+
Employee table:
44+
45+
+----+-------+--------+--------------+
46+
| id | name | salary | departmentId |
47+
+----+-------+--------+--------------+
48+
| 1 | Joe | 85000 | 1 |
49+
| 2 | Henry | 80000 | 2 |
50+
| 3 | Sam | 60000 | 2 |
51+
| 4 | Max | 90000 | 1 |
52+
| 5 | Janet | 69000 | 1 |
53+
| 6 | Randy | 85000 | 1 |
54+
| 7 | Will | 70000 | 1 |
55+
+----+-------+--------+--------------+
56+
57+
Department table:
58+
+----+-------+
59+
| id | name |
60+
+----+-------+
61+
| 1 | IT |
62+
| 2 | Sales |
63+
+----+-------+
64+
65+
**Output:**
66+
67+
+------------+----------+--------+
68+
| Department | Employee | Salary |
69+
+------------+----------+--------+
70+
| IT | Max | 90000 |
71+
| IT | Joe | 85000 |
72+
| IT | Randy | 85000 |
73+
| IT | Will | 70000 |
74+
| Sales | Henry | 80000 |
75+
| Sales | Sam | 60000 |
76+
+------------+----------+--------+
77+
78+
**Explanation:**
79+
80+
In the IT department:
81+
- Max earns the highest unique salary
82+
- Both Randy and Joe earn the second-highest unique salary
83+
- Will earns the third-highest unique salary
84+
85+
In the Sales department:
86+
- Henry earns the highest salary
87+
- Sam earns the second-highest salary
88+
- There is no third-highest salary as there are only two employees
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Write your MySQL query statement below
2+
# #Hard #Database #2022_06_27_Time_643_ms_(95.52%)_Space_0B_(100.00%)
3+
SELECT D.name AS Department,
4+
A.name AS Employee,
5+
A.salary AS Salary
6+
FROM
7+
(SELECT *,
8+
dense_rank() over(PARTITION BY departmentID
9+
ORDER BY salary DESC) AS rn
10+
FROM Employee) A
11+
INNER JOIN Department D ON A.departmentid = D.id
12+
WHERE A.rn < 4
13+
ORDER BY Department, Salary DESC
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package g0101_0200.s0187_repeated_dna_sequences
2+
3+
// #Medium #String #Hash_Table #Bit_Manipulation #Sliding_Window #Hash_Function #Rolling_Hash
4+
// #Data_Structure_II_Day_9_String #Udemy_Strings
5+
// #2022_10_17_Time_319_ms_(79.03%)_Space_66.7_MB_(16.13%)
6+
7+
class Solution {
8+
fun findRepeatedDnaSequences(s: String): List<String> {
9+
if (s.length < 10) {
10+
return emptyList()
11+
}
12+
val seen = BooleanArray(1024 * 1024)
13+
val added = BooleanArray(1024 * 1024)
14+
val chars = s.toCharArray()
15+
var buf = 0
16+
val map = IntArray(128)
17+
map['A'.code] = 0
18+
map['C'.code] = 1
19+
map['G'.code] = 2
20+
map['T'.code] = 3
21+
val ans: MutableList<String> = ArrayList(s.length / 2)
22+
for (i in 0..9) {
23+
buf = (buf shl 2) + map[chars[i].code]
24+
}
25+
seen[buf] = true
26+
for (i in 10 until chars.size) {
27+
buf = (buf shl 2 and 0xFFFFF) + map[chars[i].code]
28+
if (seen[buf]) {
29+
if (!added[buf]) {
30+
ans.add(String(chars, i - 9, 10))
31+
added[buf] = true
32+
}
33+
} else {
34+
seen[buf] = true
35+
}
36+
}
37+
return ans
38+
}
39+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
187\. Repeated DNA Sequences
2+
3+
Medium
4+
5+
The **DNA sequence** is composed of a series of nucleotides abbreviated as `'A'`, `'C'`, `'G'`, and `'T'`.
6+
7+
* For example, `"ACGAATTCCG"` is a **DNA sequence**.
8+
9+
When studying **DNA**, it is useful to identify repeated sequences within the DNA.
10+
11+
Given a string `s` that represents a **DNA sequence**, return all the **`10`\-letter-long** sequences (substrings) that occur more than once in a DNA molecule. You may return the answer in **any order**.
12+
13+
**Example 1:**
14+
15+
**Input:** s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT"
16+
17+
**Output:** ["AAAAACCCCC","CCCCCAAAAA"]
18+
19+
**Example 2:**
20+
21+
**Input:** s = "AAAAAAAAAAAAA"
22+
23+
**Output:** ["AAAAAAAAAA"]
24+
25+
**Constraints:**
26+
27+
* <code>1 <= s.length <= 10<sup>5</sup></code>
28+
* `s[i]` is either `'A'`, `'C'`, `'G'`, or `'T'`.

0 commit comments

Comments
 (0)