Skip to content

Commit 1125f60

Browse files
authored
Added tasks 1978, 2001, 2002, 2003, 2006, 2007, 2008, 2009
1 parent 9ca088a commit 1125f60

File tree

25 files changed

+997
-0
lines changed

25 files changed

+997
-0
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1847,6 +1847,13 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.13'
18471847

18481848
| # | Title | Difficulty | Tag | Time, ms | Time, %
18491849
|------|----------------|-------------|-------------|----------|---------
1850+
| 2009 |[Minimum Number of Operations to Make Array Continuous](src/main/kotlin/g2001_2100/s2009_minimum_number_of_operations_to_make_array_continuous/Solution.kt)| Hard | Array, Binary_Search | 603 | 100.00
1851+
| 2008 |[Maximum Earnings From Taxi](src/main/kotlin/g2001_2100/s2008_maximum_earnings_from_taxi/Solution.kt)| Medium | Array, Dynamic_Programming, Sorting, Binary_Search | 1008 | 100.00
1852+
| 2007 |[Find Original Array From Doubled Array](src/main/kotlin/g2001_2100/s2007_find_original_array_from_doubled_array/Solution.kt)| Medium | Array, Hash_Table, Sorting, Greedy | 753 | 100.00
1853+
| 2006 |[Count Number of Pairs With Absolute Difference K](src/main/kotlin/g2001_2100/s2006_count_number_of_pairs_with_absolute_difference_k/Solution.kt)| Easy | Array, Hash_Table, Counting | 186 | 100.00
1854+
| 2003 |[Smallest Missing Genetic Value in Each Subtree](src/main/kotlin/g2001_2100/s2003_smallest_missing_genetic_value_in_each_subtree/Solution.kt)| Hard | Dynamic_Programming, Depth_First_Search, Tree, Union_Find | 984 | 100.00
1855+
| 2002 |[Maximum Product of the Length of Two Palindromic Subsequences](src/main/kotlin/g2001_2100/s2002_maximum_product_of_the_length_of_two_palindromic_subsequences/Solution.kt)| Medium | String, Dynamic_Programming, Bit_Manipulation, Backtracking, Bitmask | 389 | 100.00
1856+
| 2001 |[Number of Pairs of Interchangeable Rectangles](src/main/kotlin/g2001_2100/s2001_number_of_pairs_of_interchangeable_rectangles/Solution.kt)| Medium | Array, Hash_Table, Math, Counting, Number_Theory | 797 | 100.00
18501857
| 2000 |[Reverse Prefix of Word](src/main/kotlin/g1901_2000/s2000_reverse_prefix_of_word/Solution.kt)| Easy | String, Two_Pointers | 164 | 31.25
18511858
| 1998 |[GCD Sort of an Array](src/main/kotlin/g1901_2000/s1998_gcd_sort_of_an_array/Solution.kt)| Hard | Array, Math, Sorting, Union_Find | 437 | 100.00
18521859
| 1997 |[First Day Where You Have Been in All the Rooms](src/main/kotlin/g1901_2000/s1997_first_day_where_you_have_been_in_all_the_rooms/Solution.kt)| Medium | Array, Dynamic_Programming | 572 | 100.00
@@ -1864,6 +1871,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.13'
18641871
| 1981 |[Minimize the Difference Between Target and Chosen Elements](src/main/kotlin/g1901_2000/s1981_minimize_the_difference_between_target_and_chosen_elements/Solution.kt)| Medium | Array, Dynamic_Programming, Matrix | 588 | 100.00
18651872
| 1980 |[Find Unique Binary String](src/main/kotlin/g1901_2000/s1980_find_unique_binary_string/Solution.kt)| Medium | Array, String, Backtracking | 186 | 50.00
18661873
| 1979 |[Find Greatest Common Divisor of Array](src/main/kotlin/g1901_2000/s1979_find_greatest_common_divisor_of_array/Solution.kt)| Easy | Array, Math, Number_Theory | 172 | 100.00
1874+
| 1978 |[Employees Whose Manager Left the Company](src/main/kotlin/g1901_2000/s1978_employees_whose_manager_left_the_company/script.sql)| Easy | Database | 686 | 64.74
18671875
| 1977 |[Number of Ways to Separate Numbers](src/main/kotlin/g1901_2000/s1977_number_of_ways_to_separate_numbers/Solution.kt)| Hard | String, Dynamic_Programming, Suffix_Array | 199 | 100.00
18681876
| 1976 |[Number of Ways to Arrive at Destination](src/main/kotlin/g1901_2000/s1976_number_of_ways_to_arrive_at_destination/Solution.kt)| Medium | Dynamic_Programming, Graph, Topological_Sort, Shortest_Path | 282 | 100.00
18691877
| 1975 |[Maximum Matrix Sum](src/main/kotlin/g1901_2000/s1975_maximum_matrix_sum/Solution.kt)| Medium | Array, Greedy, Matrix | 535 | 100.00
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
1978\. Employees Whose Manager Left the Company
2+
3+
Easy
4+
5+
SQL Schema
6+
7+
Table: `Employees`
8+
9+
+-------------+----------+
10+
| Column Name | Type |
11+
+-------------+----------+
12+
| employee_id | int |
13+
| name | varchar |
14+
| manager_id | int |
15+
| salary | int |
16+
+-------------+----------+
17+
18+
employee_id is the primary key for this table. This table contains information about the employees, their salary, and the ID of their manager. Some employees do not have a manager (manager_id is null).
19+
20+
Write an SQL query to report the IDs of the employees whose salary is strictly less than `$30000` and whose manager left the company. When a manager leaves the company, their information is deleted from the `Employees` table, but the reports still have their `manager_id` set to the manager that left.
21+
22+
Return the result table ordered by `employee_id`.
23+
24+
The query result format is in the following example.
25+
26+
**Example 1:**
27+
28+
**Input: ** Employees table:
29+
30+
+-------------+-----------+------------+--------+
31+
| employee_id | name | manager_id | salary |
32+
+-------------+-----------+------------+--------+
33+
| 3 | Mila | 9 | 60301 |
34+
| 12 | Antonella | null | 31000 |
35+
| 13 | Emery | null | 67084 |
36+
| 1 | Kalel | 11 | 21241 |
37+
| 9 | Mikaela | null | 50937 |
38+
| 11 | Joziah | 6 | 28485 |
39+
+-------------+-----------+------------+--------+
40+
41+
**Output:**
42+
43+
+-------------+
44+
| employee_id |
45+
+-------------+
46+
| 11 |
47+
+-------------+
48+
49+
**Explanation:**
50+
51+
The employees with a salary less than $30000 are 1 (Kalel) and 11 (Joziah).
52+
53+
Kalel's manager is employee 11, who is still in the company (Joziah).
54+
55+
Joziah's manager is employee 6, who left the company because there is no row for employee 6 as it was deleted.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Write your MySQL query statement below
2+
# #Easy #Database #2023_06_23_Time_686_ms_(64.74%)_Space_0B_(100.00%)
3+
select employee_id from employees where manager_id not in (select distinct (employee_id) from employees)
4+
AND salary < 30000 order by employee_id
5+
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package g2001_2100.s2001_number_of_pairs_of_interchangeable_rectangles
2+
3+
// #Medium #Array #Hash_Table #Math #Counting #Number_Theory
4+
// #2023_06_23_Time_797_ms_(100.00%)_Space_78.6_MB_(100.00%)
5+
6+
@Suppress("NAME_SHADOWING")
7+
class Solution {
8+
private fun factorial(n: Long): Long {
9+
var n = n
10+
var m: Long = 0
11+
while (n > 0) {
12+
m += n
13+
n -= 1
14+
}
15+
return m
16+
}
17+
18+
fun interchangeableRectangles(rec: Array<IntArray>): Long {
19+
val ratio = DoubleArray(rec.size)
20+
for (i in rec.indices) {
21+
ratio[i] = rec[i][0].toDouble() / rec[i][1]
22+
}
23+
ratio.sort()
24+
var res: Long = 0
25+
var k = 0
26+
for (j in 0 until ratio.size - 1) {
27+
if (ratio[j] == ratio[j + 1]) {
28+
k++
29+
}
30+
if (ratio[j] != ratio[j + 1] || j + 2 == ratio.size) {
31+
res += factorial(k.toLong())
32+
k = 0
33+
}
34+
}
35+
return res
36+
}
37+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2001\. Number of Pairs of Interchangeable Rectangles
2+
3+
Medium
4+
5+
You are given `n` rectangles represented by a **0-indexed** 2D integer array `rectangles`, where <code>rectangles[i] = [width<sub>i</sub>, height<sub>i</sub>]</code> denotes the width and height of the <code>i<sup>th</sup></code> rectangle.
6+
7+
Two rectangles `i` and `j` (`i < j`) are considered **interchangeable** if they have the **same** width-to-height ratio. More formally, two rectangles are **interchangeable** if <code>width<sub>i</sub>/height<sub>i</sub> == width<sub>j</sub>/height<sub>j</sub></code> (using decimal division, not integer division).
8+
9+
Return _the **number** of pairs of **interchangeable** rectangles in_ `rectangles`.
10+
11+
**Example 1:**
12+
13+
**Input:** rectangles = [[4,8],[3,6],[10,20],[15,30]]
14+
15+
**Output:** 6
16+
17+
**Explanation:** The following are the interchangeable pairs of rectangles by index (0-indexed):
18+
19+
- Rectangle 0 with rectangle 1: 4/8 == 3/6.
20+
21+
- Rectangle 0 with rectangle 2: 4/8 == 10/20.
22+
23+
- Rectangle 0 with rectangle 3: 4/8 == 15/30.
24+
25+
- Rectangle 1 with rectangle 2: 3/6 == 10/20.
26+
27+
- Rectangle 1 with rectangle 3: 3/6 == 15/30.
28+
29+
- Rectangle 2 with rectangle 3: 10/20 == 15/30.
30+
31+
**Example 2:**
32+
33+
**Input:** rectangles = [[4,5],[7,8]]
34+
35+
**Output:** 0
36+
37+
**Explanation:** There are no interchangeable pairs of rectangles.
38+
39+
**Constraints:**
40+
41+
* `n == rectangles.length`
42+
* <code>1 <= n <= 10<sup>5</sup></code>
43+
* `rectangles[i].length == 2`
44+
* <code>1 <= width<sub>i</sub>, height<sub>i</sub> <= 10<sup>5</sup></code>
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package g2001_2100.s2002_maximum_product_of_the_length_of_two_palindromic_subsequences
2+
3+
// #Medium #String #Dynamic_Programming #Bit_Manipulation #Backtracking #Bitmask
4+
// #2023_06_23_Time_389_ms_(100.00%)_Space_43.3_MB_(100.00%)
5+
6+
class Solution {
7+
fun maxProduct(s: String): Int {
8+
if (s.length == 2) {
9+
return 1
10+
}
11+
val list: MutableList<State> = ArrayList()
12+
val chars = s.toCharArray()
13+
val visited: MutableSet<State> = HashSet()
14+
for (i in chars.indices) {
15+
val mask = 1 shl i
16+
recur(chars, State(i, i, 0, mask), list, visited)
17+
recur(chars, State(i, i + 1, 0, mask), list, visited)
18+
}
19+
list.sortWith { a: State, b: State -> b.cnt - a.cnt }
20+
var res = 1
21+
val explored: MutableSet<Int> = HashSet()
22+
for (i in 0 until list.size - 1) {
23+
if (explored.contains(i)) {
24+
continue
25+
}
26+
val cur = list[i]
27+
if (cur.cnt == 1) {
28+
break
29+
}
30+
for (j in i + 1 until list.size) {
31+
val cand = list[j]
32+
if (cur.mask and cand.mask < 1) {
33+
if (explored.add(j)) {
34+
res = res.coerceAtLeast(cur.cnt * cand.cnt)
35+
}
36+
break
37+
}
38+
}
39+
}
40+
return res
41+
}
42+
43+
private fun recur(chars: CharArray, s: State, list: MutableList<State>, visited: MutableSet<State>) {
44+
if (s.i < 0 || s.j >= chars.size) {
45+
return
46+
}
47+
if (!visited.add(s)) {
48+
return
49+
}
50+
if (chars[s.i] == chars[s.j]) {
51+
val m = s.mask or (1 shl s.i) or (1 shl s.j)
52+
val nextCnt = s.cnt + if (s.i < s.j) 2 else 1
53+
list.add(State(s.i, s.j, nextCnt, m))
54+
recur(chars, State(s.i - 1, s.j + 1, nextCnt, m), list, visited)
55+
}
56+
recur(chars, State(s.i - 1, s.j, s.cnt, s.mask), list, visited)
57+
recur(chars, State(s.i, s.j + 1, s.cnt, s.mask), list, visited)
58+
}
59+
60+
private class State(var i: Int, var j: Int, var cnt: Int, var mask: Int) {
61+
override fun equals(other: Any?): Boolean {
62+
if (other == null || other.javaClass != this.javaClass) {
63+
return false
64+
}
65+
val s = other as State
66+
return i == s.i && j == s.j && mask == s.mask
67+
}
68+
69+
override fun hashCode(): Int {
70+
return (i * 31 + j) * 31 + mask
71+
}
72+
}
73+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2002\. Maximum Product of the Length of Two Palindromic Subsequences
2+
3+
Medium
4+
5+
Given a string `s`, find two **disjoint palindromic subsequences** of `s` such that the **product** of their lengths is **maximized**. The two subsequences are **disjoint** if they do not both pick a character at the same index.
6+
7+
Return _the **maximum** possible **product** of the lengths of the two palindromic subsequences_.
8+
9+
A **subsequence** is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters. A string is **palindromic** if it reads the same forward and backward.
10+
11+
**Example 1:**
12+
13+
![example-1](https://assets.leetcode.com/uploads/2021/08/24/two-palindromic-subsequences.png)
14+
15+
**Input:** s = "leetcodecom"
16+
17+
**Output:** 9
18+
19+
**Explanation:** An optimal solution is to choose "ete" for the 1<sup>st</sup> subsequence and "cdc" for the 2<sup>nd</sup> subsequence.
20+
21+
The product of their lengths is: 3 \* 3 = 9.
22+
23+
**Example 2:**
24+
25+
**Input:** s = "bb"
26+
27+
**Output:** 1
28+
29+
**Explanation:** An optimal solution is to choose "b" (the first character) for the 1<sup>st</sup> subsequence and "b" (the second character) for the 2<sup>nd</sup> subsequence.
30+
31+
The product of their lengths is: 1 \* 1 = 1.
32+
33+
**Example 3:**
34+
35+
**Input:** s = "accbcaxxcxx"
36+
37+
**Output:** 25
38+
39+
**Explanation:** An optimal solution is to choose "accca" for the 1<sup>st</sup> subsequence and "xxcxx" for the 2<sup>nd</sup> subsequence.
40+
41+
The product of their lengths is: 5 \* 5 = 25.
42+
43+
**Constraints:**
44+
45+
* `2 <= s.length <= 12`
46+
* `s` consists of lowercase English letters only.
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package g2001_2100.s2003_smallest_missing_genetic_value_in_each_subtree
2+
3+
// #Hard #Dynamic_Programming #Depth_First_Search #Tree #Union_Find
4+
// #2023_06_23_Time_984_ms_(100.00%)_Space_78.9_MB_(100.00%)
5+
6+
@Suppress("NAME_SHADOWING")
7+
class Solution {
8+
fun smallestMissingValueSubtree(parents: IntArray, nums: IntArray): IntArray {
9+
val ans = IntArray(parents.size)
10+
val all = arrayOfNulls<Node>(parents.size)
11+
var max = 0
12+
for (i in nums.indices) {
13+
all[i] = Node(i, nums[i])
14+
max = max.coerceAtLeast(nums[i])
15+
}
16+
for (i in 1 until parents.size) {
17+
all[parents[i]]!!.nodes.add(all[i])
18+
}
19+
solve(all[0], ans, UF(++max, nums))
20+
return ans
21+
}
22+
23+
private fun solve(root: Node?, ans: IntArray, uf: UF) {
24+
var max = 1
25+
for (child in root!!.nodes) {
26+
solve(child, ans, uf)
27+
uf.union(root.`val`, child!!.`val`)
28+
max = ans[child.idx].coerceAtLeast(max)
29+
}
30+
while (max <= ans.size && uf.isConnected(max, root.`val`)) {
31+
++max
32+
}
33+
ans[root.idx] = max
34+
}
35+
36+
private class Node internal constructor(var idx: Int, var `val`: Int) {
37+
var nodes: MutableList<Node?> = ArrayList()
38+
}
39+
40+
private class UF internal constructor(n: Int, nums: IntArray) {
41+
var rank: IntArray
42+
var parent: IntArray
43+
44+
init {
45+
rank = IntArray(n)
46+
parent = IntArray(n)
47+
for (m in nums) {
48+
parent[m] = m
49+
}
50+
}
51+
52+
private fun find(x: Int): Int {
53+
if (x == parent[x]) {
54+
return x
55+
}
56+
parent[x] = find(parent[x])
57+
return parent[x]
58+
}
59+
60+
fun union(x: Int, y: Int) {
61+
var x = x
62+
var y = y
63+
x = find(x)
64+
y = find(y)
65+
if (rank[x] > rank[y]) {
66+
parent[y] = x
67+
} else {
68+
parent[x] = y
69+
if (rank[x] == rank[y]) {
70+
rank[y]++
71+
}
72+
}
73+
}
74+
75+
fun isConnected(x: Int, y: Int): Boolean {
76+
return find(x) == find(y)
77+
}
78+
}
79+
}

0 commit comments

Comments
 (0)