Skip to content

Commit 2bab3bc

Browse files
authored
Added tasks 1049, 1050, 1051, 1052
1 parent 3690b9d commit 2bab3bc

File tree

13 files changed

+408
-0
lines changed

13 files changed

+408
-0
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1747,6 +1747,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.12'
17471747
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
17481748
|-|-|-|-|-|-
17491749
| 0182 |[Duplicate Emails](src/main/kotlin/g0101_0200/s0182_duplicate_emails/script.sql)| Easy | Database | 396 | 68.40
1750+
| 1050 |[Actors and Directors Who Cooperated At Least Three Times](src/main/kotlin/g1001_1100/s1050_actors_and_directors_who_cooperated_at_least_three_times/script.sql)| Easy | LeetCode_Curated_SQL_70, Database | 629 | 81.02
17501751

17511752
## Algorithms
17521753

@@ -1780,6 +1781,10 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.12'
17801781
| 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
17811782
| 1154 |[Day of the Year](src/main/kotlin/g1101_1200/s1154_day_of_the_year/Solution.kt)| Easy | String, Math | 317 | 70.00
17821783
| 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
1784+
| 1052 |[Grumpy Bookstore Owner](src/main/kotlin/g1001_1100/s1052_grumpy_bookstore_owner/Solution.kt)| Medium | Array, Sliding_Window | 268 | 80.00
1785+
| 1051 |[Height Checker](src/main/kotlin/g1001_1100/s1051_height_checker/Solution.kt)| Easy | Array, Sorting, Counting_Sort | 140 | 94.37
1786+
| 1050 |[Actors and Directors Who Cooperated At Least Three Times](src/main/kotlin/g1001_1100/s1050_actors_and_directors_who_cooperated_at_least_three_times/script.sql)| Easy | LeetCode_Curated_SQL_70, Database, SQL_I_Day_10_Where | 629 | 81.02
1787+
| 1049 |[Last Stone Weight II](src/main/kotlin/g1001_1100/s1049_last_stone_weight_ii/Solution.kt)| Medium | Array, Dynamic_Programming | 150 | 100.00
17831788
| 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
17841789
| 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
17851790
| 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
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g1001_1100.s1049_last_stone_weight_ii
2+
3+
// #Medium #Array #Dynamic_Programming #2023_05_29_Time_150_ms_(100.00%)_Space_35.5_MB_(100.00%)
4+
5+
class Solution {
6+
fun lastStoneWeightII(stones: IntArray): Int {
7+
// dp[i][j] i is the index of stones, j is the current weight
8+
// goal is to find max closest to half and use it to get the diff
9+
// 0-1 knapsack problem
10+
var sum = 0
11+
for (stone in stones) {
12+
sum += stone
13+
}
14+
val half = sum / 2
15+
val dp = IntArray(half + 1)
16+
for (cur in stones) {
17+
for (j in half downTo cur) {
18+
dp[j] = dp[j].coerceAtLeast(dp[j - cur] + cur)
19+
}
20+
}
21+
return sum - dp[half] * 2
22+
}
23+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
1049\. Last Stone Weight II
2+
3+
Medium
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 any two stones and smash them together. Suppose the 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 smallest possible weight of the left 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 can combine 2 and 4 to get 2, so the array converts to [2,7,1,8,1] then,
25+
26+
we can combine 7 and 8 to get 1, so the array converts to [2,1,1,1] then,
27+
28+
we can combine 2 and 1 to get 1, so the array converts to [1,1,1] then,
29+
30+
we can combine 1 and 1 to get 0, so the array converts to [1], then that's the optimal value.
31+
32+
**Example 2:**
33+
34+
**Input:** stones = [31,26,33,21,40]
35+
36+
**Output:** 5
37+
38+
**Constraints:**
39+
40+
* `1 <= stones.length <= 30`
41+
* `1 <= stones[i] <= 100`
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
1050\. Actors and Directors Who Cooperated At Least Three Times
2+
3+
Easy
4+
5+
SQL Schema
6+
7+
Table: `ActorDirector`
8+
9+
+-------------+---------+
10+
| Column Name | Type |
11+
+-------------+---------+
12+
| actor_id | int |
13+
| director_id | int |
14+
| timestamp | int |
15+
+-------------+---------+
16+
timestamp is the primary key column for this table.
17+
18+
Write a SQL query for a report that provides the pairs `(actor_id, director_id)` where the actor has cooperated with the director at least three times.
19+
20+
Return the result table in **any order**.
21+
22+
The query result format is in the following example.
23+
24+
**Example 1:**
25+
26+
**Input:**
27+
28+
ActorDirector table:
29+
+-------------+-------------+-------------+
30+
| actor_id | director_id | timestamp |
31+
+-------------+-------------+-------------+
32+
| 1 | 1 | 0 |
33+
| 1 | 1 | 1 |
34+
| 1 | 1 | 2 |
35+
| 1 | 2 | 3 |
36+
| 1 | 2 | 4 |
37+
| 2 | 1 | 5 |
38+
| 2 | 1 | 6 |
39+
+-------------+-------------+-------------+
40+
41+
**Output:**
42+
43+
+-------------+-------------+
44+
| actor_id | director_id |
45+
+-------------+-------------+
46+
| 1 | 1 |
47+
+-------------+-------------+
48+
49+
**Explanation:** The only pair is (1, 1) where they cooperated exactly 3 times.
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 #LeetCode_Curated_SQL_70 #Database #SQL_I_Day_10_Where
3+
# #2023_05_29_Time_629_ms_(81.02%)_Space_0B_(100.00%)
4+
SELECT actor_id, director_id
5+
FROM ActorDirector
6+
GROUP BY actor_id, director_id
7+
HAVING COUNT(*) > 2
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package g1001_1100.s1051_height_checker
2+
3+
// #Easy #Array #Sorting #Counting_Sort #2023_05_29_Time_140_ms_(94.37%)_Space_35.3_MB_(77.46%)
4+
5+
class Solution {
6+
fun heightChecker(heights: IntArray): Int {
7+
var heightDiff = 0
8+
val count = IntArray(101)
9+
val actualLine = IntArray(heights.size)
10+
for (height in heights) {
11+
count[height]++
12+
}
13+
var heightLength = 0
14+
for (i in count.indices) {
15+
if (count[i] > 0) {
16+
for (j in 0 until count[i]) {
17+
actualLine[heightLength] = i
18+
heightLength++
19+
}
20+
count[i] = 0
21+
}
22+
}
23+
for (i in heights.indices) {
24+
if (actualLine[i] != heights[i]) {
25+
heightDiff++
26+
}
27+
}
28+
return heightDiff
29+
}
30+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
1051\. Height Checker
2+
3+
Easy
4+
5+
A school is trying to take an annual photo of all the students. The students are asked to stand in a single file line in **non-decreasing order** by height. Let this ordering be represented by the integer array `expected` where `expected[i]` is the expected height of the <code>i<sup>th</sup></code> student in line.
6+
7+
You are given an integer array `heights` representing the **current order** that the students are standing in. Each `heights[i]` is the height of the <code>i<sup>th</sup></code> student in line (**0-indexed**).
8+
9+
Return _the **number of indices** where_ `heights[i] != expected[i]`.
10+
11+
**Example 1:**
12+
13+
**Input:** heights = [1,1,4,2,1,3]
14+
15+
**Output:** 3
16+
17+
**Explanation:**
18+
19+
heights: [1,1,4,2,1,3]
20+
21+
expected: [1,1,1,2,3,4]
22+
23+
Indices 2, 4, and 5 do not match.
24+
25+
**Example 2:**
26+
27+
**Input:** heights = [5,1,2,3,4]
28+
29+
**Output:** 5
30+
31+
**Explanation:**
32+
33+
heights: [5,1,2,3,4]
34+
35+
expected: [1,2,3,4,5]
36+
37+
All indices do not match.
38+
39+
**Example 3:**
40+
41+
**Input:** heights = [1,2,3,4,5]
42+
43+
**Output:** 0
44+
45+
**Explanation:**
46+
47+
heights: [1,2,3,4,5]
48+
49+
expected: [1,2,3,4,5]
50+
51+
All indices match.
52+
53+
**Constraints:**
54+
55+
* `1 <= heights.length <= 100`
56+
* `1 <= heights[i] <= 100`
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package g1001_1100.s1052_grumpy_bookstore_owner
2+
3+
// #Medium #Array #Sliding_Window #2023_05_29_Time_268_ms_(80.00%)_Space_64.6_MB_(60.00%)
4+
5+
class Solution {
6+
fun maxSatisfied(customers: IntArray, grumpy: IntArray, minutes: Int): Int {
7+
// storing numbers of customers who faced grumpy owner till ith minute.
8+
val grumpySum = IntArray(grumpy.size)
9+
var ans = 0
10+
if (grumpy[0] == 1) {
11+
grumpySum[0] = customers[0]
12+
} else {
13+
ans += customers[0]
14+
}
15+
for (i in 1 until grumpy.size) {
16+
if (grumpy[i] == 1) {
17+
grumpySum[i] = grumpySum[i - 1] + customers[i]
18+
} else {
19+
grumpySum[i] = grumpySum[i - 1]
20+
ans += customers[i]
21+
}
22+
}
23+
// calculating max number of customers who faced grumpy owner in a window of size 'minutes'.
24+
var max = 0
25+
for (i in 0..customers.size - minutes) {
26+
max = if (i == 0) {
27+
max.coerceAtLeast(grumpySum[i + minutes - 1])
28+
} else {
29+
max.coerceAtLeast(grumpySum[i + minutes - 1] - grumpySum[i - 1])
30+
}
31+
}
32+
// making the owner non-grumpy in that max window and adding the number of customers who do
33+
// not face the grumpy customers.
34+
ans += max
35+
return ans
36+
}
37+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
1052\. Grumpy Bookstore Owner
2+
3+
Medium
4+
5+
There is a bookstore owner that has a store open for `n` minutes. Every minute, some number of customers enter the store. You are given an integer array `customers` of length `n` where `customers[i]` is the number of the customer that enters the store at the start of the <code>i<sup>th</sup></code> minute and all those customers leave after the end of that minute.
6+
7+
On some minutes, the bookstore owner is grumpy. You are given a binary array grumpy where `grumpy[i]` is `1` if the bookstore owner is grumpy during the <code>i<sup>th</sup></code> minute, and is `0` otherwise.
8+
9+
When the bookstore owner is grumpy, the customers of that minute are not satisfied, otherwise, they are satisfied.
10+
11+
The bookstore owner knows a secret technique to keep themselves not grumpy for `minutes` consecutive minutes, but can only use it once.
12+
13+
Return _the maximum number of customers that can be satisfied throughout the day_.
14+
15+
**Example 1:**
16+
17+
**Input:** customers = [1,0,1,2,1,1,7,5], grumpy = [0,1,0,1,0,1,0,1], minutes = 3
18+
19+
**Output:** 16
20+
21+
**Explanation:** The bookstore owner keeps themselves not grumpy for the last 3 minutes. The maximum number of customers that can be satisfied = 1 + 1 + 1 + 1 + 7 + 5 = 16.
22+
23+
**Example 2:**
24+
25+
**Input:** customers = [1], grumpy = [0], minutes = 1
26+
27+
**Output:** 1
28+
29+
**Constraints:**
30+
31+
* `n == customers.length == grumpy.length`
32+
* <code>1 <= minutes <= n <= 2 * 10<sup>4</sup></code>
33+
* `0 <= customers[i] <= 1000`
34+
* `grumpy[i]` is either `0` or `1`.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package g1001_1100.s1049_last_stone_weight_ii
2+
3+
import org.hamcrest.CoreMatchers.equalTo
4+
import org.hamcrest.MatcherAssert.assertThat
5+
import org.junit.jupiter.api.Test
6+
7+
internal class SolutionTest {
8+
@Test
9+
fun lastStoneWeightII() {
10+
assertThat(Solution().lastStoneWeightII(intArrayOf(2, 7, 4, 1, 8, 1)), equalTo(1))
11+
}
12+
13+
@Test
14+
fun lastStoneWeightII2() {
15+
assertThat(Solution().lastStoneWeightII(intArrayOf(31, 26, 33, 21, 40)), equalTo(5))
16+
}
17+
}

0 commit comments

Comments
 (0)