Skip to content

Commit 30bd0f7

Browse files
authored
Added tasks 3168-3181
1 parent 220e960 commit 30bd0f7

File tree

15 files changed

+1107
-6
lines changed
  • src/main/kotlin
    • g0201_0300/s0208_implement_trie_prefix_tree
    • g0601_0700/s0620_not_boring_movies
    • g3101_3200
      • s3168_minimum_number_of_chairs_in_a_waiting_room
      • s3169_count_days_without_meetings
      • s3170_lexicographically_minimum_string_after_removing_stars
      • s3171_find_subarray_with_bitwise_and_closest_to_k
      • s3174_clear_digits
      • s3175_find_the_first_player_to_win_k_games_in_a_row
      • s3176_find_the_maximum_length_of_a_good_subsequence_i
      • s3177_find_the_maximum_length_of_a_good_subsequence_ii
      • s3178_find_the_child_who_has_the_ball_after_k_seconds
      • s3179_find_the_n_th_value_after_k_seconds
      • s3180_maximum_total_reward_using_operations_i
      • s3181_maximum_total_reward_using_operations_ii

15 files changed

+1107
-6
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1816,6 +1816,18 @@
18161816

18171817
| # | Title | Difficulty | Tag | Time, ms | Time, %
18181818
|------|----------------|-------------|-------------|----------|--------
1819+
| 3181 |[Maximum Total Reward Using Operations II](src/main/kotlin/g3101_3200/s3181_maximum_total_reward_using_operations_ii)| Hard | Array, Dynamic_Programming, Bit_Manipulation | 376 | 100.00
1820+
| 3180 |[Maximum Total Reward Using Operations I](src/main/kotlin/g3101_3200/s3180_maximum_total_reward_using_operations_i)| Medium | Array, Dynamic_Programming | 183 | 100.00
1821+
| 3179 |[Find the N-th Value After K Seconds](src/main/kotlin/g3101_3200/s3179_find_the_n_th_value_after_k_seconds)| Medium | Array, Math, Simulation, Prefix_Sum, Combinatorics | 175 | 100.00
1822+
| 3178 |[Find the Child Who Has the Ball After K Seconds](src/main/kotlin/g3101_3200/s3178_find_the_child_who_has_the_ball_after_k_seconds)| Easy | Math, Simulation | 136 | 82.35
1823+
| 3177 |[Find the Maximum Length of a Good Subsequence II](src/main/kotlin/g3101_3200/s3177_find_the_maximum_length_of_a_good_subsequence_ii)| Hard | Array, Hash_Table, Dynamic_Programming | 284 | 100.00
1824+
| 3176 |[Find the Maximum Length of a Good Subsequence I](src/main/kotlin/g3101_3200/s3176_find_the_maximum_length_of_a_good_subsequence_i)| Medium | Array, Hash_Table, Dynamic_Programming | 183 | 100.00
1825+
| 3175 |[Find The First Player to win K Games in a Row](src/main/kotlin/g3101_3200/s3175_find_the_first_player_to_win_k_games_in_a_row)| Medium | Array, Simulation | 536 | 100.00
1826+
| 3174 |[Clear Digits](src/main/kotlin/g3101_3200/s3174_clear_digits)| Easy | String, Hash_Table, Simulation | 180 | 70.18
1827+
| 3171 |[Find Subarray With Bitwise AND Closest to K](src/main/kotlin/g3101_3200/s3171_find_subarray_with_bitwise_and_closest_to_k)| Hard | Array, Binary_Search, Bit_Manipulation, Segment_Tree | 520 | 100.00
1828+
| 3170 |[Lexicographically Minimum String After Removing Stars](src/main/kotlin/g3101_3200/s3170_lexicographically_minimum_string_after_removing_stars)| Medium | String, Hash_Table, Greedy, Stack, Heap_Priority_Queue | 316 | 100.00
1829+
| 3169 |[Count Days Without Meetings](src/main/kotlin/g3101_3200/s3169_count_days_without_meetings)| Medium | Array, Sorting | 733 | 97.59
1830+
| 3168 |[Minimum Number of Chairs in a Waiting Room](src/main/kotlin/g3101_3200/s3168_minimum_number_of_chairs_in_a_waiting_room)| Easy | String, Simulation | 148 | 86.52
18191831
| 3165 |[Maximum Sum of Subsequence With Non-adjacent Elements](src/main/kotlin/g3101_3200/s3165_maximum_sum_of_subsequence_with_non_adjacent_elements)| Hard | Array, Dynamic_Programming, Divide_and_Conquer, Segment_Tree | 1301 | 22.22
18201832
| 3164 |[Find the Number of Good Pairs II](src/main/kotlin/g3101_3200/s3164_find_the_number_of_good_pairs_ii)| Medium | Array, Hash_Table | 1175 | 90.00
18211833
| 3163 |[String Compression III](src/main/kotlin/g3101_3200/s3163_string_compression_iii)| Medium | String | 331 | 66.13

src/main/kotlin/g0201_0300/s0208_implement_trie_prefix_tree/readme.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,12 @@ class Trie {
102102
root = TrieNode()
103103
}
104104
}
105+
106+
/*
107+
* Your Trie object will be instantiated and called as such:
108+
* var obj = Trie()
109+
* obj.insert(word)
110+
* var param_2 = obj.search(word)
111+
* var param_3 = obj.startsWith(prefix)
112+
*/
105113
```

src/main/kotlin/g0601_0700/s0620_not_boring_movies/readme.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ The query result format is in the following example.
3232
**Input:**
3333
Cinema table:
3434
+----+------------+-------------+--------+
35-
\| id \| movie \| description \| rating \|
35+
| id | movie | description | rating |
3636
+----+------------+-------------+--------+
37-
\| 1 \| War \| great 3D \| 8.9 \|
38-
\| 2 \| Science \| fiction \| 8.5 \|
39-
\| 3 \| irish \| boring \| 6.2 \|
40-
\| 4 \| Ice song \| Fantacy \| 8.6 \|
41-
\| 5 \| House card \| Interesting \| 9.1 \|
37+
| 1 | War | great 3D | 8.9 |
38+
| 2 | Science | fiction | 8.5 |
39+
| 3 | irish | boring | 6.2 |
40+
| 4 | Ice song | Fantacy | 8.6 |
41+
| 5 | House card | Interesting | 9.1 |
4242
+----+------------+-------------+--------+
4343

4444
**Output:**
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
3+
4+
## 3168\. Minimum Number of Chairs in a Waiting Room
5+
6+
Easy
7+
8+
You are given a string `s`. Simulate events at each second `i`:
9+
10+
* If `s[i] == 'E'`, a person enters the waiting room and takes one of the chairs in it.
11+
* If `s[i] == 'L'`, a person leaves the waiting room, freeing up a chair.
12+
13+
Return the **minimum** number of chairs needed so that a chair is available for every person who enters the waiting room given that it is initially **empty**.
14+
15+
**Example 1:**
16+
17+
**Input:** s = "EEEEEEE"
18+
19+
**Output:** 7
20+
21+
**Explanation:**
22+
23+
After each second, a person enters the waiting room and no person leaves it. Therefore, a minimum of 7 chairs is needed.
24+
25+
**Example 2:**
26+
27+
**Input:** s = "ELELEEL"
28+
29+
**Output:** 2
30+
31+
**Explanation:**
32+
33+
Let's consider that there are 2 chairs in the waiting room. The table below shows the state of the waiting room at each second.
34+
35+
| Second | Event | People in the Waiting Room | Available Chairs |
36+
|--------|-------|----------------------------|------------------|
37+
| 0 | Enter | 1 | 1 |
38+
| 1 | Leave | 0 | 2 |
39+
| 2 | Enter | 1 | 1 |
40+
| 3 | Leave | 0 | 2 |
41+
| 4 | Enter | 1 | 1 |
42+
| 5 | Enter | 2 | 0 |
43+
| 6 | Leave | 1 | 1 |
44+
45+
**Example 3:**
46+
47+
**Input:** s = "ELEELEELLL"
48+
49+
**Output:** 3
50+
51+
**Explanation:**
52+
53+
Let's consider that there are 3 chairs in the waiting room. The table below shows the state of the waiting room at each second.
54+
55+
| Second | Event | People in the Waiting Room | Available Chairs |
56+
|--------|-------|----------------------------|------------------|
57+
| 0 | Enter | 1 | 2 |
58+
| 1 | Leave | 0 | 3 |
59+
| 2 | Enter | 1 | 2 |
60+
| 3 | Enter | 2 | 1 |
61+
| 4 | Leave | 1 | 2 |
62+
| 5 | Enter | 2 | 1 |
63+
| 6 | Enter | 3 | 0 |
64+
| 7 | Leave | 2 | 1 |
65+
| 8 | Leave | 1 | 2 |
66+
| 9 | Leave | 0 | 3 |
67+
68+
**Constraints:**
69+
70+
* `1 <= s.length <= 50`
71+
* `s` consists only of the letters `'E'` and `'L'`.
72+
* `s` represents a valid sequence of entries and exits.
73+
74+
## Solution
75+
76+
```kotlin
77+
import kotlin.math.max
78+
79+
class Solution {
80+
fun minimumChairs(s: String): Int {
81+
var count = 0
82+
var ans = Int.MIN_VALUE
83+
for (ch in s.toCharArray()) {
84+
if (ch == 'E') {
85+
count++
86+
ans = max(ans, count)
87+
} else {
88+
count--
89+
}
90+
}
91+
return ans
92+
}
93+
}
94+
```
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
3+
4+
## 3169\. Count Days Without Meetings
5+
6+
Medium
7+
8+
You are given a positive integer `days` representing the total number of days an employee is available for work (starting from day 1). You are also given a 2D array `meetings` of size `n` where, `meetings[i] = [start_i, end_i]` represents the starting and ending days of meeting `i` (inclusive).
9+
10+
Return the count of days when the employee is available for work but no meetings are scheduled.
11+
12+
**Note:** The meetings may overlap.
13+
14+
**Example 1:**
15+
16+
**Input:** days = 10, meetings = \[\[5,7],[1,3],[9,10]]
17+
18+
**Output:** 2
19+
20+
**Explanation:**
21+
22+
There is no meeting scheduled on the 4<sup>th</sup> and 8<sup>th</sup> days.
23+
24+
**Example 2:**
25+
26+
**Input:** days = 5, meetings = \[\[2,4],[1,3]]
27+
28+
**Output:** 1
29+
30+
**Explanation:**
31+
32+
There is no meeting scheduled on the 5<sup>th</sup> day.
33+
34+
**Example 3:**
35+
36+
**Input:** days = 6, meetings = \[\[1,6]]
37+
38+
**Output:** 0
39+
40+
**Explanation:**
41+
42+
Meetings are scheduled for all working days.
43+
44+
**Constraints:**
45+
46+
* <code>1 <= days <= 10<sup>9</sup></code>
47+
* <code>1 <= meetings.length <= 10<sup>5</sup></code>
48+
* `meetings[i].length == 2`
49+
* `1 <= meetings[i][0] <= meetings[i][1] <= days`
50+
51+
## Solution
52+
53+
```kotlin
54+
class Solution {
55+
fun countDays(days: Int, meetings: Array<IntArray>): Int {
56+
var availableDays: MutableList<IntArray> = ArrayList()
57+
availableDays.add(intArrayOf(1, days))
58+
// Iterate through each meeting
59+
for (meeting in meetings) {
60+
val start = meeting[0]
61+
val end = meeting[1]
62+
val newAvailableDays: MutableList<IntArray> = ArrayList()
63+
// Iterate through available days and split the intervals
64+
for (interval in availableDays) {
65+
if (start > interval[1] || end < interval[0]) {
66+
// No overlap, keep the interval
67+
newAvailableDays.add(interval)
68+
} else {
69+
// Overlap, split the interval
70+
if (interval[0] < start) {
71+
newAvailableDays.add(intArrayOf(interval[0], start - 1))
72+
}
73+
if (interval[1] > end) {
74+
newAvailableDays.add(intArrayOf(end + 1, interval[1]))
75+
}
76+
}
77+
}
78+
availableDays = newAvailableDays
79+
}
80+
// Count the remaining available days
81+
var availableDaysCount = 0
82+
for (interval in availableDays) {
83+
availableDaysCount += interval[1] - interval[0] + 1
84+
}
85+
return availableDaysCount
86+
}
87+
}
88+
```
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
3+
4+
## 3170\. Lexicographically Minimum String After Removing Stars
5+
6+
Medium
7+
8+
You are given a string `s`. It may contain any number of `'*'` characters. Your task is to remove all `'*'` characters.
9+
10+
While there is a `'*'`, do the following operation:
11+
12+
* Delete the leftmost `'*'` and the **smallest** non-`'*'` character to its _left_. If there are several smallest characters, you can delete any of them.
13+
14+
Return the lexicographically smallest resulting string after removing all `'*'` characters.
15+
16+
**Example 1:**
17+
18+
**Input:** s = "aaba\*"
19+
20+
**Output:** "aab"
21+
22+
**Explanation:**
23+
24+
We should delete one of the `'a'` characters with `'*'`. If we choose `s[3]`, `s` becomes the lexicographically smallest.
25+
26+
**Example 2:**
27+
28+
**Input:** s = "abc"
29+
30+
**Output:** "abc"
31+
32+
**Explanation:**
33+
34+
There is no `'*'` in the string.
35+
36+
**Constraints:**
37+
38+
* <code>1 <= s.length <= 10<sup>5</sup></code>
39+
* `s` consists only of lowercase English letters and `'*'`.
40+
* The input is generated such that it is possible to delete all `'*'` characters.
41+
42+
## Solution
43+
44+
```kotlin
45+
class Solution {
46+
fun clearStars(s: String): String {
47+
val arr = s.toCharArray()
48+
val idxChain = IntArray(arr.size)
49+
val lastIdx = IntArray(26)
50+
idxChain.fill(-1)
51+
lastIdx.fill(-1)
52+
for (i in arr.indices) {
53+
if (arr[i] == '*') {
54+
for (j in 0..25) {
55+
if (lastIdx[j] != -1) {
56+
arr[lastIdx[j]] = '#'
57+
lastIdx[j] = idxChain[lastIdx[j]]
58+
break
59+
}
60+
}
61+
arr[i] = '#'
62+
} else {
63+
idxChain[i] = lastIdx[arr[i].code - 'a'.code]
64+
lastIdx[arr[i].code - 'a'.code] = i
65+
}
66+
}
67+
val sb = StringBuilder()
68+
for (c in arr) {
69+
if (c != '#') {
70+
sb.append(c)
71+
}
72+
}
73+
return sb.toString()
74+
}
75+
}
76+
```
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
3+
4+
## 3171\. Find Subarray With Bitwise AND Closest to K
5+
6+
Hard
7+
8+
You are given an array `nums` and an integer `k`. You need to find a subarray of `nums` such that the **absolute difference** between `k` and the bitwise `AND` of the subarray elements is as **small** as possible. In other words, select a subarray `nums[l..r]` such that `|k - (nums[l] AND nums[l + 1] ... AND nums[r])|` is minimum.
9+
10+
Return the **minimum** possible value of the absolute difference.
11+
12+
A **subarray** is a contiguous **non-empty** sequence of elements within an array.
13+
14+
**Example 1:**
15+
16+
**Input:** nums = [1,2,4,5], k = 3
17+
18+
**Output:** 1
19+
20+
**Explanation:**
21+
22+
The subarray `nums[2..3]` has `AND` value 4, which gives the minimum absolute difference `|3 - 4| = 1`.
23+
24+
**Example 2:**
25+
26+
**Input:** nums = [1,2,1,2], k = 2
27+
28+
**Output:** 0
29+
30+
**Explanation:**
31+
32+
The subarray `nums[1..1]` has `AND` value 2, which gives the minimum absolute difference `|2 - 2| = 0`.
33+
34+
**Example 3:**
35+
36+
**Input:** nums = [1], k = 10
37+
38+
**Output:** 9
39+
40+
**Explanation:**
41+
42+
There is a single subarray with `AND` value 1, which gives the minimum absolute difference `|10 - 1| = 9`.
43+
44+
**Constraints:**
45+
46+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
47+
* <code>1 <= nums[i] <= 10<sup>9</sup></code>
48+
* <code>1 <= k <= 10<sup>9</sup></code>
49+
50+
## Solution
51+
52+
```kotlin
53+
import kotlin.math.abs
54+
import kotlin.math.min
55+
56+
class Solution {
57+
fun minimumDifference(nums: IntArray, k: Int): Int {
58+
var res = Int.MAX_VALUE
59+
for (i in nums.indices) {
60+
res = min(res, abs((nums[i] - k)))
61+
var j = i - 1
62+
while (j >= 0 && (nums[j] and nums[i]) != nums[j]) {
63+
nums[j] = nums[j] and nums[i]
64+
res = min(res, abs((nums[j] - k)))
65+
j--
66+
}
67+
}
68+
return res
69+
}
70+
}
71+
```

0 commit comments

Comments
 (0)