Skip to content

Commit 3690b9d

Browse files
authored
Added tasks 1045, 1046, 1047, 1048
1 parent ddc89ce commit 3690b9d

File tree

13 files changed

+418
-0
lines changed

13 files changed

+418
-0
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.12'
152152

153153
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
154154
|-|-|-|-|-|-
155+
| 1046 |[Last Stone Weight](src/main/kotlin/g1001_1100/s1046_last_stone_weight/Solution.kt)| Easy | Array, Heap_Priority_Queue | 123 | 100.00
155156
| 0692 |[Top K Frequent Words](src/main/kotlin/g0601_0700/s0692_top_k_frequent_words/Solution.kt)| Medium | String, Hash_Table, Sorting, Heap_Priority_Queue, Counting, Trie, Bucket_Sort | 239 | 81.10
156157

157158
### Level 2
@@ -1779,6 +1780,10 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.12'
17791780
| 1155 |[Number of Dice Rolls With Target Sum](src/main/kotlin/g1101_1200/s1155_number_of_dice_rolls_with_target_sum/Solution.kt)| Medium | Dynamic_Programming | 158 | 80.95
17801781
| 1154 |[Day of the Year](src/main/kotlin/g1101_1200/s1154_day_of_the_year/Solution.kt)| Easy | String, Math | 317 | 70.00
17811782
| 1143 |[Longest Common Subsequence](src/main/kotlin/g1101_1200/s1143_longest_common_subsequence/Solution.kt)| Medium | Top_100_Liked_Questions, String, Dynamic_Programming, Algorithm_II_Day_17_Dynamic_Programming, Dynamic_Programming_I_Day_19, Udemy_Dynamic_Programming | 307 | 38.36
1783+
| 1048 |[Longest String Chain](src/main/kotlin/g1001_1100/s1048_longest_string_chain/Solution.kt)| Medium | Array, String, Hash_Table, Dynamic_Programming, Two_Pointers | 273 | 75.00
1784+
| 1047 |[Remove All Adjacent Duplicates In String](src/main/kotlin/g1001_1100/s1047_remove_all_adjacent_duplicates_in_string/Solution.kt)| Easy | String, Stack | 228 | 94.52
1785+
| 1046 |[Last Stone Weight](src/main/kotlin/g1001_1100/s1046_last_stone_weight/Solution.kt)| Easy | Array, Heap_Priority_Queue, Level_1_Day_15_Heap | 123 | 100.00
1786+
| 1045 |[Customers Who Bought All Products](src/main/kotlin/g1001_1100/s1045_customers_who_bought_all_products/script.sql)| Medium | Database | 881 | 90.47
17821787
| 1044 |[Longest Duplicate Substring](src/main/kotlin/g1001_1100/s1044_longest_duplicate_substring/Solution.kt)| Hard | String, Binary_Search, Sliding_Window, Hash_Function, Rolling_Hash, Suffix_Array | 592 | 100.00
17831788
| 1043 |[Partition Array for Maximum Sum](src/main/kotlin/g1001_1100/s1043_partition_array_for_maximum_sum/Solution.kt)| Medium | Array, Dynamic_Programming | 194 | 71.43
17841789
| 1042 |[Flower Planting With No Adjacent](src/main/kotlin/g1001_1100/s1042_flower_planting_with_no_adjacent/Solution.kt)| Medium | Depth_First_Search, Breadth_First_Search, Graph | 396 | 85.71
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
1045\. Customers Who Bought All Products
2+
3+
Medium
4+
5+
SQL Schema
6+
7+
Table: `Customer`
8+
9+
+-------------+---------+
10+
| Column Name | Type |
11+
+-------------+---------+
12+
| customer_id | int |
13+
| product_key | int |
14+
+-------------+---------+
15+
16+
There is no primary key for this table. It may contain duplicates. `customer_id` is not NULL`.` product\_key is a foreign key to `Product` table.
17+
18+
Table: `Product`
19+
20+
+-------------+---------+
21+
| Column Name | Type |
22+
+-------------+---------+
23+
| product_key | int |
24+
+-------------+---------+
25+
26+
product_key is the primary key column for this table.
27+
28+
Write an SQL query to report the customer ids from the `Customer` table that bought all the products in the `Product` table.
29+
30+
Return the result table in **any order**.
31+
32+
The query result format is in the following example.
33+
34+
**Example 1:**
35+
36+
**Input:** Customer table:
37+
38+
+-------------+-------------+
39+
| customer_id | product_key |
40+
+-------------+-------------+
41+
| 1 | 5 |
42+
| 2 | 6 |
43+
| 3 | 5 |
44+
| 3 | 6 |
45+
| 1 | 6 |
46+
+-------------+-------------+
47+
48+
Product table:
49+
50+
+-------------+
51+
| product_key |
52+
+-------------+
53+
| 5 |
54+
| 6 |
55+
+-------------+
56+
57+
**Output:**
58+
59+
+-------------+
60+
| customer_id |
61+
+-------------+
62+
| 1 |
63+
| 3 |
64+
+-------------+
65+
66+
**Explanation:** The customers who bought all the products (5 and 6) are customers with IDs 1 and 3.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Write your MySQL query statement below
2+
# #Medium #Database #2023_05_28_Time_881_ms_(90.47%)_Space_0B_(100.00%)
3+
select customer_id
4+
from customer
5+
group by customer_id
6+
having count(distinct product_key)=(select count(*) from product);
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package g1001_1100.s1046_last_stone_weight
2+
3+
// #Easy #Array #Heap_Priority_Queue #Level_1_Day_15_Heap
4+
// #2023_05_28_Time_123_ms_(100.00%)_Space_35_MB_(39.50%)
5+
6+
import java.util.PriorityQueue
7+
8+
class Solution {
9+
fun lastStoneWeight(stones: IntArray): Int {
10+
val heap = PriorityQueue { a: Int, b: Int -> b - a }
11+
for (stone in stones) {
12+
heap.offer(stone)
13+
}
14+
while (heap.isNotEmpty()) {
15+
if (heap.size >= 2) {
16+
val one = heap.poll()
17+
val two = heap.poll()
18+
val diff = one - two
19+
heap.offer(diff)
20+
} else {
21+
return heap.poll()
22+
}
23+
}
24+
return -1
25+
}
26+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
1046\. Last Stone Weight
2+
3+
Easy
4+
5+
You are given an array of integers `stones` where `stones[i]` is the weight of the <code>i<sup>th</sup></code> stone.
6+
7+
We are playing a game with the stones. On each turn, we choose the **heaviest two stones** and smash them together. Suppose the heaviest two stones have weights `x` and `y` with `x <= y`. The result of this smash is:
8+
9+
* If `x == y`, both stones are destroyed, and
10+
* If `x != y`, the stone of weight `x` is destroyed, and the stone of weight `y` has new weight `y - x`.
11+
12+
At the end of the game, there is **at most one** stone left.
13+
14+
Return _the weight of the last remaining stone_. If there are no stones left, return `0`.
15+
16+
**Example 1:**
17+
18+
**Input:** stones = [2,7,4,1,8,1]
19+
20+
**Output:** 1
21+
22+
**Explanation:**
23+
24+
We combine 7 and 8 to get 1 so the array converts to [2,4,1,1,1] then,
25+
26+
we combine 2 and 4 to get 2 so the array converts to [2,1,1,1] then,
27+
28+
we combine 2 and 1 to get 1 so the array converts to [1,1,1] then,
29+
30+
we combine 1 and 1 to get 0 so the array converts to [1] then that's the value of the last stone.
31+
32+
**Example 2:**
33+
34+
**Input:** stones = [1]
35+
36+
**Output:** 1
37+
38+
**Constraints:**
39+
40+
* `1 <= stones.length <= 30`
41+
* `1 <= stones[i] <= 1000`
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package g1001_1100.s1047_remove_all_adjacent_duplicates_in_string
2+
3+
// #Easy #String #Stack #2023_05_28_Time_228_ms_(94.52%)_Space_50.5_MB_(54.79%)
4+
5+
class Solution {
6+
fun removeDuplicates(s: String): String {
7+
if (s.length == 1) {
8+
return s
9+
}
10+
val array = s.toCharArray()
11+
val length = array.size
12+
var fast = 0
13+
var slow = 0
14+
while (fast < length) {
15+
if (slow == 0 || array[fast] != array[slow - 1]) {
16+
array[slow++] = array[fast++]
17+
} else {
18+
if (array[fast] == array[slow - 1]) {
19+
fast++
20+
}
21+
slow--
22+
}
23+
}
24+
return String(array, 0, slow)
25+
}
26+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
1047\. Remove All Adjacent Duplicates In String
2+
3+
Easy
4+
5+
You are given a string `s` consisting of lowercase English letters. A **duplicate removal** consists of choosing two **adjacent** and **equal** letters and removing them.
6+
7+
We repeatedly make **duplicate removals** on `s` until we no longer can.
8+
9+
Return _the final string after all such duplicate removals have been made_. It can be proven that the answer is **unique**.
10+
11+
**Example 1:**
12+
13+
**Input:** s = "abbaca"
14+
15+
**Output:** "ca"
16+
17+
**Explanation:** For example, in "abbaca" we could remove "bb" since the letters are adjacent and equal, and this is the only possible move. The result of this move is that the string is "aaca", of which only "aa" is possible, so the final string is "ca".
18+
19+
**Example 2:**
20+
21+
**Input:** s = "azxxzy"
22+
23+
**Output:** "ay"
24+
25+
**Constraints:**
26+
27+
* <code>1 <= s.length <= 10<sup>5</sup></code>
28+
* `s` consists of lowercase English letters.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package g1001_1100.s1048_longest_string_chain
2+
3+
// #Medium #Array #String #Hash_Table #Dynamic_Programming #Two_Pointers
4+
// #2023_05_28_Time_273_ms_(75.00%)_Space_46.5_MB_(50.00%)
5+
6+
class Solution {
7+
fun longestStrChain(words: Array<String>): Int {
8+
val lenStr = arrayOfNulls<MutableList<String>?>(20)
9+
for (word in words) {
10+
val len = word.length
11+
if (lenStr[len] == null) {
12+
lenStr[len] = ArrayList()
13+
}
14+
lenStr[len]!!.add(word)
15+
}
16+
val longest: MutableMap<String?, Int?> = HashMap()
17+
var max = 0
18+
for (s in words) {
19+
max = findLongest(s, lenStr, longest).coerceAtLeast(max)
20+
}
21+
return max
22+
}
23+
24+
private fun findLongest(
25+
word: String,
26+
lenStr: Array<MutableList<String>?>,
27+
longest: MutableMap<String?, Int?>
28+
): Int {
29+
if (longest.containsKey(word)) {
30+
return longest[word]!!
31+
}
32+
val len = word.length
33+
val words: List<String>? = lenStr[len + 1]
34+
if (words == null) {
35+
longest[word] = 1
36+
return 1
37+
}
38+
var max = 0
39+
var i: Int
40+
var j: Int
41+
for (w in words) {
42+
i = 0
43+
j = 0
44+
while (i < len && j - i <= 1) {
45+
if (word[i] == w[j++]) {
46+
++i
47+
}
48+
}
49+
if (j - i <= 1) {
50+
max = findLongest(w, lenStr, longest).coerceAtLeast(max)
51+
}
52+
}
53+
++max
54+
longest[word] = max
55+
return max
56+
}
57+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
1048\. Longest String Chain
2+
3+
Medium
4+
5+
You are given an array of `words` where each word consists of lowercase English letters.
6+
7+
<code>word<sub>A</sub></code> is a **predecessor** of <code>word<sub>B</sub></code> if and only if we can insert **exactly one** letter anywhere in <code>word<sub>A</sub></code> **without changing the order of the other characters** to make it equal to <code>word<sub>B</sub></code>.
8+
9+
* For example, `"abc"` is a **predecessor** of <code>"ab<ins>a</ins>c"</code>, while `"cba"` is not a **predecessor** of `"bcad"`.
10+
11+
A **word chain** is a sequence of words <code>[word<sub>1</sub>, word<sub>2</sub>, ..., word<sub>k</sub>]</code> with `k >= 1`, where <code>word<sub>1</sub></code> is a **predecessor** of <code>word<sub>2</sub></code>, <code>word<sub>2</sub></code> is a **predecessor** of <code>word<sub>3</sub></code>, and so on. A single word is trivially a **word chain** with `k == 1`.
12+
13+
Return _the **length** of the **longest possible word chain** with words chosen from the given list of_ `words`.
14+
15+
**Example 1:**
16+
17+
**Input:** words = ["a","b","ba","bca","bda","bdca"]
18+
19+
**Output:** 4
20+
21+
**Explanation:**: One of the longest word chains is ["a","<ins>b</ins>a","b<ins>d</ins>a","bd<ins>c</ins>a"].
22+
23+
**Example 2:**
24+
25+
**Input:** words = ["xbc","pcxbcf","xb","cxbc","pcxbc"]
26+
27+
**Output:** 5
28+
29+
**Explanation:** All the words can be put in a word chain ["xb", "xb<ins>c</ins>", "<ins>c</ins>xbc", "<ins>p</ins>cxbc", "pcxbc<ins>f</ins>"].
30+
31+
**Example 3:**
32+
33+
**Input:** words = ["abcd","dbqca"]
34+
35+
**Output:** 1
36+
37+
**Explanation:** The trivial word chain ["abcd"] is one of the longest word chains. ["abcd","dbqca"] is not a valid word chain because the ordering of the letters is changed.
38+
39+
**Constraints:**
40+
41+
* `1 <= words.length <= 1000`
42+
* `1 <= words[i].length <= 16`
43+
* `words[i]` only consists of lowercase English letters.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package g1001_1100.s1045_customers_who_bought_all_products
2+
3+
import org.hamcrest.CoreMatchers.equalTo
4+
import org.hamcrest.MatcherAssert.assertThat
5+
import org.junit.jupiter.api.Test
6+
import org.zapodot.junit.db.annotations.EmbeddedDatabase
7+
import org.zapodot.junit.db.annotations.EmbeddedDatabaseTest
8+
import org.zapodot.junit.db.common.CompatibilityMode
9+
import java.io.BufferedReader
10+
import java.io.FileNotFoundException
11+
import java.io.FileReader
12+
import java.sql.SQLException
13+
import java.util.stream.Collectors
14+
import javax.sql.DataSource
15+
16+
@EmbeddedDatabaseTest(
17+
compatibilityMode = CompatibilityMode.MySQL,
18+
initialSqls = [
19+
"CREATE TABLE Customer(customer_id INTEGER, product_key INTEGER); " +
20+
" INSERT INTO Customer(customer_id, product_key) VALUES (1, 5); " +
21+
" INSERT INTO Customer(customer_id, product_key) VALUES (2, 6); " +
22+
" INSERT INTO Customer(customer_id, product_key) VALUES (3, 5); " +
23+
" INSERT INTO Customer(customer_id, product_key) VALUES (3, 6); " +
24+
" INSERT INTO Customer(customer_id, product_key) VALUES (1, 6); " +
25+
"CREATE TABLE Product(product_key INTEGER); " +
26+
" INSERT INTO Product(product_key) VALUES (5); " +
27+
" INSERT INTO Product(product_key) VALUES (6); "
28+
]
29+
)
30+
internal class MysqlTest {
31+
@Test
32+
@Throws(SQLException::class, FileNotFoundException::class)
33+
fun testScript(@EmbeddedDatabase dataSource: DataSource) {
34+
dataSource.connection.use { connection ->
35+
connection.createStatement().use { statement ->
36+
statement.executeQuery(
37+
BufferedReader(
38+
FileReader(
39+
"src/main/kotlin/g1001_1100/" +
40+
"s1045_customers_who_bought_all_products" +
41+
"/script.sql"
42+
)
43+
)
44+
.lines()
45+
.collect(Collectors.joining("\n"))
46+
.replace("#.*?\\r?\\n".toRegex(), "")
47+
).use { resultSet ->
48+
assertThat(resultSet.next(), equalTo(true))
49+
assertThat(resultSet.getInt(1), equalTo(1))
50+
assertThat(resultSet.next(), equalTo(true))
51+
assertThat(resultSet.getInt(1), equalTo(3))
52+
assertThat(resultSet.next(), equalTo(false))
53+
}
54+
}
55+
}
56+
}
57+
}

0 commit comments

Comments
 (0)