Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1816,6 +1816,18 @@

| # | Title | Difficulty | Tag | Time, ms | Time, %
|------|----------------|-------------|-------------|----------|--------
| 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
| 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
| 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
| 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
| 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
| 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
| 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
| 3174 |[Clear Digits](src/main/kotlin/g3101_3200/s3174_clear_digits)| Easy | String, Hash_Table, Simulation | 180 | 70.18
| 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
| 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
| 3169 |[Count Days Without Meetings](src/main/kotlin/g3101_3200/s3169_count_days_without_meetings)| Medium | Array, Sorting | 733 | 97.59
| 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
| 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
| 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
| 3163 |[String Compression III](src/main/kotlin/g3101_3200/s3163_string_compression_iii)| Medium | String | 331 | 66.13
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,12 @@ class Trie {
root = TrieNode()
}
}

/*
* Your Trie object will be instantiated and called as such:
* var obj = Trie()
* obj.insert(word)
* var param_2 = obj.search(word)
* var param_3 = obj.startsWith(prefix)
*/
```
12 changes: 6 additions & 6 deletions src/main/kotlin/g0601_0700/s0620_not_boring_movies/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ The query result format is in the following example.
**Input:**
Cinema table:
+----+------------+-------------+--------+
\| id \| movie \| description \| rating \|
| id | movie | description | rating |
+----+------------+-------------+--------+
\| 1 \| War \| great 3D \| 8.9 \|
\| 2 \| Science \| fiction \| 8.5 \|
\| 3 \| irish \| boring \| 6.2 \|
\| 4 \| Ice song \| Fantacy \| 8.6 \|
\| 5 \| House card \| Interesting \| 9.1 \|
| 1 | War | great 3D | 8.9 |
| 2 | Science | fiction | 8.5 |
| 3 | irish | boring | 6.2 |
| 4 | Ice song | Fantacy | 8.6 |
| 5 | House card | Interesting | 9.1 |
+----+------------+-------------+--------+

**Output:**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
[![](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)

## 3168\. Minimum Number of Chairs in a Waiting Room

Easy

You are given a string `s`. Simulate events at each second `i`:

* If `s[i] == 'E'`, a person enters the waiting room and takes one of the chairs in it.
* If `s[i] == 'L'`, a person leaves the waiting room, freeing up a chair.

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**.

**Example 1:**

**Input:** s = "EEEEEEE"

**Output:** 7

**Explanation:**

After each second, a person enters the waiting room and no person leaves it. Therefore, a minimum of 7 chairs is needed.

**Example 2:**

**Input:** s = "ELELEEL"

**Output:** 2

**Explanation:**

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.

| Second | Event | People in the Waiting Room | Available Chairs |
|--------|-------|----------------------------|------------------|
| 0 | Enter | 1 | 1 |
| 1 | Leave | 0 | 2 |
| 2 | Enter | 1 | 1 |
| 3 | Leave | 0 | 2 |
| 4 | Enter | 1 | 1 |
| 5 | Enter | 2 | 0 |
| 6 | Leave | 1 | 1 |

**Example 3:**

**Input:** s = "ELEELEELLL"

**Output:** 3

**Explanation:**

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.

| Second | Event | People in the Waiting Room | Available Chairs |
|--------|-------|----------------------------|------------------|
| 0 | Enter | 1 | 2 |
| 1 | Leave | 0 | 3 |
| 2 | Enter | 1 | 2 |
| 3 | Enter | 2 | 1 |
| 4 | Leave | 1 | 2 |
| 5 | Enter | 2 | 1 |
| 6 | Enter | 3 | 0 |
| 7 | Leave | 2 | 1 |
| 8 | Leave | 1 | 2 |
| 9 | Leave | 0 | 3 |

**Constraints:**

* `1 <= s.length <= 50`
* `s` consists only of the letters `'E'` and `'L'`.
* `s` represents a valid sequence of entries and exits.

## Solution

```kotlin
import kotlin.math.max

class Solution {
fun minimumChairs(s: String): Int {
var count = 0
var ans = Int.MIN_VALUE
for (ch in s.toCharArray()) {
if (ch == 'E') {
count++
ans = max(ans, count)
} else {
count--
}
}
return ans
}
}
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
[![](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)

## 3169\. Count Days Without Meetings

Medium

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).

Return the count of days when the employee is available for work but no meetings are scheduled.

**Note:** The meetings may overlap.

**Example 1:**

**Input:** days = 10, meetings = \[\[5,7],[1,3],[9,10]]

**Output:** 2

**Explanation:**

There is no meeting scheduled on the 4<sup>th</sup> and 8<sup>th</sup> days.

**Example 2:**

**Input:** days = 5, meetings = \[\[2,4],[1,3]]

**Output:** 1

**Explanation:**

There is no meeting scheduled on the 5<sup>th</sup> day.

**Example 3:**

**Input:** days = 6, meetings = \[\[1,6]]

**Output:** 0

**Explanation:**

Meetings are scheduled for all working days.

**Constraints:**

* <code>1 <= days <= 10<sup>9</sup></code>
* <code>1 <= meetings.length <= 10<sup>5</sup></code>
* `meetings[i].length == 2`
* `1 <= meetings[i][0] <= meetings[i][1] <= days`

## Solution

```kotlin
class Solution {
fun countDays(days: Int, meetings: Array<IntArray>): Int {
var availableDays: MutableList<IntArray> = ArrayList()
availableDays.add(intArrayOf(1, days))
// Iterate through each meeting
for (meeting in meetings) {
val start = meeting[0]
val end = meeting[1]
val newAvailableDays: MutableList<IntArray> = ArrayList()
// Iterate through available days and split the intervals
for (interval in availableDays) {
if (start > interval[1] || end < interval[0]) {
// No overlap, keep the interval
newAvailableDays.add(interval)
} else {
// Overlap, split the interval
if (interval[0] < start) {
newAvailableDays.add(intArrayOf(interval[0], start - 1))
}
if (interval[1] > end) {
newAvailableDays.add(intArrayOf(end + 1, interval[1]))
}
}
}
availableDays = newAvailableDays
}
// Count the remaining available days
var availableDaysCount = 0
for (interval in availableDays) {
availableDaysCount += interval[1] - interval[0] + 1
}
return availableDaysCount
}
}
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
[![](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)

## 3170\. Lexicographically Minimum String After Removing Stars

Medium

You are given a string `s`. It may contain any number of `'*'` characters. Your task is to remove all `'*'` characters.

While there is a `'*'`, do the following operation:

* Delete the leftmost `'*'` and the **smallest** non-`'*'` character to its _left_. If there are several smallest characters, you can delete any of them.

Return the lexicographically smallest resulting string after removing all `'*'` characters.

**Example 1:**

**Input:** s = "aaba\*"

**Output:** "aab"

**Explanation:**

We should delete one of the `'a'` characters with `'*'`. If we choose `s[3]`, `s` becomes the lexicographically smallest.

**Example 2:**

**Input:** s = "abc"

**Output:** "abc"

**Explanation:**

There is no `'*'` in the string.

**Constraints:**

* <code>1 <= s.length <= 10<sup>5</sup></code>
* `s` consists only of lowercase English letters and `'*'`.
* The input is generated such that it is possible to delete all `'*'` characters.

## Solution

```kotlin
class Solution {
fun clearStars(s: String): String {
val arr = s.toCharArray()
val idxChain = IntArray(arr.size)
val lastIdx = IntArray(26)
idxChain.fill(-1)
lastIdx.fill(-1)
for (i in arr.indices) {
if (arr[i] == '*') {
for (j in 0..25) {
if (lastIdx[j] != -1) {
arr[lastIdx[j]] = '#'
lastIdx[j] = idxChain[lastIdx[j]]
break
}
}
arr[i] = '#'
} else {
idxChain[i] = lastIdx[arr[i].code - 'a'.code]
lastIdx[arr[i].code - 'a'.code] = i
}
}
val sb = StringBuilder()
for (c in arr) {
if (c != '#') {
sb.append(c)
}
}
return sb.toString()
}
}
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
[![](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)

## 3171\. Find Subarray With Bitwise AND Closest to K

Hard

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.

Return the **minimum** possible value of the absolute difference.

A **subarray** is a contiguous **non-empty** sequence of elements within an array.

**Example 1:**

**Input:** nums = [1,2,4,5], k = 3

**Output:** 1

**Explanation:**

The subarray `nums[2..3]` has `AND` value 4, which gives the minimum absolute difference `|3 - 4| = 1`.

**Example 2:**

**Input:** nums = [1,2,1,2], k = 2

**Output:** 0

**Explanation:**

The subarray `nums[1..1]` has `AND` value 2, which gives the minimum absolute difference `|2 - 2| = 0`.

**Example 3:**

**Input:** nums = [1], k = 10

**Output:** 9

**Explanation:**

There is a single subarray with `AND` value 1, which gives the minimum absolute difference `|10 - 1| = 9`.

**Constraints:**

* <code>1 <= nums.length <= 10<sup>5</sup></code>
* <code>1 <= nums[i] <= 10<sup>9</sup></code>
* <code>1 <= k <= 10<sup>9</sup></code>

## Solution

```kotlin
import kotlin.math.abs
import kotlin.math.min

class Solution {
fun minimumDifference(nums: IntArray, k: Int): Int {
var res = Int.MAX_VALUE
for (i in nums.indices) {
res = min(res, abs((nums[i] - k)))
var j = i - 1
while (j >= 0 && (nums[j] and nums[i]) != nums[j]) {
nums[j] = nums[j] and nums[i]
res = min(res, abs((nums[j] - k)))
j--
}
}
return res
}
}
```
Loading