From 3082844e4956cb7bb159f3986051adc2c34d06bd Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Sat, 15 Jun 2024 17:40:47 +0300 Subject: [PATCH] Added tasks 3168-3181 --- README.md | 12 ++ .../readme.md | 8 ++ .../s0620_not_boring_movies/readme.md | 12 +- .../readme.md | 94 +++++++++++++ .../readme.md | 88 ++++++++++++ .../readme.md | 76 +++++++++++ .../readme.md | 71 ++++++++++ .../g3101_3200/s3174_clear_digits/readme.md | 62 +++++++++ .../readme.md | 86 ++++++++++++ .../readme.md | 94 +++++++++++++ .../readme.md | 76 +++++++++++ .../readme.md | 129 ++++++++++++++++++ .../readme.md | 95 +++++++++++++ .../readme.md | 99 ++++++++++++++ .../readme.md | 111 +++++++++++++++ 15 files changed, 1107 insertions(+), 6 deletions(-) create mode 100644 src/main/kotlin/g3101_3200/s3168_minimum_number_of_chairs_in_a_waiting_room/readme.md create mode 100644 src/main/kotlin/g3101_3200/s3169_count_days_without_meetings/readme.md create mode 100644 src/main/kotlin/g3101_3200/s3170_lexicographically_minimum_string_after_removing_stars/readme.md create mode 100644 src/main/kotlin/g3101_3200/s3171_find_subarray_with_bitwise_and_closest_to_k/readme.md create mode 100644 src/main/kotlin/g3101_3200/s3174_clear_digits/readme.md create mode 100644 src/main/kotlin/g3101_3200/s3175_find_the_first_player_to_win_k_games_in_a_row/readme.md create mode 100644 src/main/kotlin/g3101_3200/s3176_find_the_maximum_length_of_a_good_subsequence_i/readme.md create mode 100644 src/main/kotlin/g3101_3200/s3177_find_the_maximum_length_of_a_good_subsequence_ii/readme.md create mode 100644 src/main/kotlin/g3101_3200/s3178_find_the_child_who_has_the_ball_after_k_seconds/readme.md create mode 100644 src/main/kotlin/g3101_3200/s3179_find_the_n_th_value_after_k_seconds/readme.md create mode 100644 src/main/kotlin/g3101_3200/s3180_maximum_total_reward_using_operations_i/readme.md create mode 100644 src/main/kotlin/g3101_3200/s3181_maximum_total_reward_using_operations_ii/readme.md diff --git a/README.md b/README.md index fb199027..5199a3da 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/main/kotlin/g0201_0300/s0208_implement_trie_prefix_tree/readme.md b/src/main/kotlin/g0201_0300/s0208_implement_trie_prefix_tree/readme.md index b56397f5..7f239cc3 100644 --- a/src/main/kotlin/g0201_0300/s0208_implement_trie_prefix_tree/readme.md +++ b/src/main/kotlin/g0201_0300/s0208_implement_trie_prefix_tree/readme.md @@ -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) + */ ``` \ No newline at end of file diff --git a/src/main/kotlin/g0601_0700/s0620_not_boring_movies/readme.md b/src/main/kotlin/g0601_0700/s0620_not_boring_movies/readme.md index a1e89819..bd5633b2 100644 --- a/src/main/kotlin/g0601_0700/s0620_not_boring_movies/readme.md +++ b/src/main/kotlin/g0601_0700/s0620_not_boring_movies/readme.md @@ -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:** diff --git a/src/main/kotlin/g3101_3200/s3168_minimum_number_of_chairs_in_a_waiting_room/readme.md b/src/main/kotlin/g3101_3200/s3168_minimum_number_of_chairs_in_a_waiting_room/readme.md new file mode 100644 index 00000000..8d671264 --- /dev/null +++ b/src/main/kotlin/g3101_3200/s3168_minimum_number_of_chairs_in_a_waiting_room/readme.md @@ -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 + } +} +``` \ No newline at end of file diff --git a/src/main/kotlin/g3101_3200/s3169_count_days_without_meetings/readme.md b/src/main/kotlin/g3101_3200/s3169_count_days_without_meetings/readme.md new file mode 100644 index 00000000..e0efd3e2 --- /dev/null +++ b/src/main/kotlin/g3101_3200/s3169_count_days_without_meetings/readme.md @@ -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 4th and 8th days. + +**Example 2:** + +**Input:** days = 5, meetings = \[\[2,4],[1,3]] + +**Output:** 1 + +**Explanation:** + +There is no meeting scheduled on the 5th day. + +**Example 3:** + +**Input:** days = 6, meetings = \[\[1,6]] + +**Output:** 0 + +**Explanation:** + +Meetings are scheduled for all working days. + +**Constraints:** + +* 1 <= days <= 109 +* 1 <= meetings.length <= 105 +* `meetings[i].length == 2` +* `1 <= meetings[i][0] <= meetings[i][1] <= days` + +## Solution + +```kotlin +class Solution { + fun countDays(days: Int, meetings: Array): Int { + var availableDays: MutableList = 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 = 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 + } +} +``` \ No newline at end of file diff --git a/src/main/kotlin/g3101_3200/s3170_lexicographically_minimum_string_after_removing_stars/readme.md b/src/main/kotlin/g3101_3200/s3170_lexicographically_minimum_string_after_removing_stars/readme.md new file mode 100644 index 00000000..94490959 --- /dev/null +++ b/src/main/kotlin/g3101_3200/s3170_lexicographically_minimum_string_after_removing_stars/readme.md @@ -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:** + +* 1 <= s.length <= 105 +* `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() + } +} +``` \ No newline at end of file diff --git a/src/main/kotlin/g3101_3200/s3171_find_subarray_with_bitwise_and_closest_to_k/readme.md b/src/main/kotlin/g3101_3200/s3171_find_subarray_with_bitwise_and_closest_to_k/readme.md new file mode 100644 index 00000000..2672f0b8 --- /dev/null +++ b/src/main/kotlin/g3101_3200/s3171_find_subarray_with_bitwise_and_closest_to_k/readme.md @@ -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:** + +* 1 <= nums.length <= 105 +* 1 <= nums[i] <= 109 +* 1 <= k <= 109 + +## 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 + } +} +``` \ No newline at end of file diff --git a/src/main/kotlin/g3101_3200/s3174_clear_digits/readme.md b/src/main/kotlin/g3101_3200/s3174_clear_digits/readme.md new file mode 100644 index 00000000..5d681986 --- /dev/null +++ b/src/main/kotlin/g3101_3200/s3174_clear_digits/readme.md @@ -0,0 +1,62 @@ +[![](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) + +## 3174\. Clear Digits + +Easy + +You are given a string `s`. + +Your task is to remove **all** digits by doing this operation repeatedly: + +* Delete the _first_ digit and the **closest** **non-digit** character to its _left_. + +Return the resulting string after removing all digits. + +**Example 1:** + +**Input:** s = "abc" + +**Output:** "abc" + +**Explanation:** + +There is no digit in the string. + +**Example 2:** + +**Input:** s = "cb34" + +**Output:** "" + +**Explanation:** + +First, we apply the operation on `s[2]`, and `s` becomes `"c4"`. + +Then we apply the operation on `s[1]`, and `s` becomes `""`. + +**Constraints:** + +* `1 <= s.length <= 100` +* `s` consists only of lowercase English letters and digits. +* The input is generated such that it is possible to delete all digits. + +## Solution + +```kotlin +class Solution { + fun clearDigits(s: String): String { + val result = StringBuilder() + for (ch in s.toCharArray()) { + if (ch in '0'..'9') { + if (result.isNotEmpty()) { + result.deleteCharAt(result.length - 1) + } + } else { + result.append(ch) + } + } + return result.toString() + } +} +``` \ No newline at end of file diff --git a/src/main/kotlin/g3101_3200/s3175_find_the_first_player_to_win_k_games_in_a_row/readme.md b/src/main/kotlin/g3101_3200/s3175_find_the_first_player_to_win_k_games_in_a_row/readme.md new file mode 100644 index 00000000..65f9442f --- /dev/null +++ b/src/main/kotlin/g3101_3200/s3175_find_the_first_player_to_win_k_games_in_a_row/readme.md @@ -0,0 +1,86 @@ +[![](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) + +## 3175\. Find The First Player to win K Games in a Row + +Medium + +A competition consists of `n` players numbered from `0` to `n - 1`. + +You are given an integer array `skills` of size `n` and a **positive** integer `k`, where `skills[i]` is the skill level of player `i`. All integers in `skills` are **unique**. + +All players are standing in a queue in order from player `0` to player `n - 1`. + +The competition process is as follows: + +* The first two players in the queue play a game, and the player with the **higher** skill level wins. +* After the game, the winner stays at the beginning of the queue, and the loser goes to the end of it. + +The winner of the competition is the **first** player who wins `k` games **in a row**. + +Return the initial index of the _winning_ player. + +**Example 1:** + +**Input:** skills = [4,2,6,3,9], k = 2 + +**Output:** 2 + +**Explanation:** + +Initially, the queue of players is `[0,1,2,3,4]`. The following process happens: + +* Players 0 and 1 play a game, since the skill of player 0 is higher than that of player 1, player 0 wins. The resulting queue is `[0,2,3,4,1]`. +* Players 0 and 2 play a game, since the skill of player 2 is higher than that of player 0, player 2 wins. The resulting queue is `[2,3,4,1,0]`. +* Players 2 and 3 play a game, since the skill of player 2 is higher than that of player 3, player 2 wins. The resulting queue is `[2,4,1,0,3]`. + +Player 2 won `k = 2` games in a row, so the winner is player 2. + +**Example 2:** + +**Input:** skills = [2,5,4], k = 3 + +**Output:** 1 + +**Explanation:** + +Initially, the queue of players is `[0,1,2]`. The following process happens: + +* Players 0 and 1 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is `[1,2,0]`. +* Players 1 and 2 play a game, since the skill of player 1 is higher than that of player 2, player 1 wins. The resulting queue is `[1,0,2]`. +* Players 1 and 0 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is `[1,2,0]`. + +Player 1 won `k = 3` games in a row, so the winner is player 1. + +**Constraints:** + +* `n == skills.length` +* 2 <= n <= 105 +* 1 <= k <= 109 +* 1 <= skills[i] <= 106 +* All integers in `skills` are unique. + +## Solution + +```kotlin +class Solution { + fun findWinningPlayer(skills: IntArray, k: Int): Int { + val n = skills.size + var max = skills[0] + var cnt = 0 + var res = 0 + for (i in 1 until n) { + if (skills[i] > max) { + max = skills[i] + cnt = 0 + res = i + } + cnt += 1 + if (cnt == k) { + break + } + } + return res + } +} +``` \ No newline at end of file diff --git a/src/main/kotlin/g3101_3200/s3176_find_the_maximum_length_of_a_good_subsequence_i/readme.md b/src/main/kotlin/g3101_3200/s3176_find_the_maximum_length_of_a_good_subsequence_i/readme.md new file mode 100644 index 00000000..3b2fa854 --- /dev/null +++ b/src/main/kotlin/g3101_3200/s3176_find_the_maximum_length_of_a_good_subsequence_i/readme.md @@ -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) + +## 3176\. Find the Maximum Length of a Good Subsequence I + +Medium + +You are given an integer array `nums` and a **non-negative** integer `k`. A sequence of integers `seq` is called **good** if there are **at most** `k` indices `i` in the range `[0, seq.length - 2]` such that `seq[i] != seq[i + 1]`. + +Return the **maximum** possible length of a **good** subsequence of `nums`. + +**Example 1:** + +**Input:** nums = [1,2,1,1,3], k = 2 + +**Output:** 4 + +**Explanation:** + +The maximum length subsequence is [1,2,1,1,3]. + +**Example 2:** + +**Input:** nums = [1,2,3,4,5,1], k = 0 + +**Output:** 2 + +**Explanation:** + +The maximum length subsequence is [1,2,3,4,5,1]. + +**Constraints:** + +* `1 <= nums.length <= 500` +* 1 <= nums[i] <= 109 +* `0 <= k <= min(nums.length, 25)` + +## Solution + +```kotlin +import kotlin.math.max + +class Solution { + fun maximumLength(nums: IntArray, k: Int): Int { + val n = nums.size + var count = 0 + for (i in 0 until nums.size - 1) { + if (nums[i] != nums[i + 1]) { + count++ + } + } + if (count <= k) { + return n + } + val max = IntArray(k + 1) + max.fill(1) + val vis = IntArray(n) + vis.fill(-1) + val map = HashMap() + for (i in 0 until n) { + if (!map.containsKey(nums[i])) { + map[nums[i]] = i + 1 + } else { + vis[i] = map[nums[i]]!! - 1 + map[nums[i]] = i + 1 + } + } + val dp = Array(n) { IntArray(k + 1) } + for (i in 0 until n) { + for (j in 0..k) { + dp[i][j] = 1 + } + } + for (i in 1 until n) { + for (j in k - 1 downTo 0) { + dp[i][j + 1] = max(dp[i][j + 1], (1 + max[j])) + max[j + 1] = max(max[j + 1], dp[i][j + 1]) + } + if (vis[i] != -1) { + val a = vis[i] + for (j in 0..k) { + dp[i][j] = max(dp[i][j], (1 + dp[a][j])) + max[j] = max(dp[i][j], max[j]) + } + } + } + var ans = 1 + for (i in 0..k) { + ans = max(ans, max[i]) + } + return ans + } +} +``` \ No newline at end of file diff --git a/src/main/kotlin/g3101_3200/s3177_find_the_maximum_length_of_a_good_subsequence_ii/readme.md b/src/main/kotlin/g3101_3200/s3177_find_the_maximum_length_of_a_good_subsequence_ii/readme.md new file mode 100644 index 00000000..319ffc5c --- /dev/null +++ b/src/main/kotlin/g3101_3200/s3177_find_the_maximum_length_of_a_good_subsequence_ii/readme.md @@ -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) + +## 3177\. Find the Maximum Length of a Good Subsequence II + +Hard + +You are given an integer array `nums` and a **non-negative** integer `k`. A sequence of integers `seq` is called **good** if there are **at most** `k` indices `i` in the range `[0, seq.length - 2]` such that `seq[i] != seq[i + 1]`. + +Return the **maximum** possible length of a **good** subsequence of `nums`. + +**Example 1:** + +**Input:** nums = [1,2,1,1,3], k = 2 + +**Output:** 4 + +**Explanation:** + +The maximum length subsequence is [1,2,1,1,3]. + +**Example 2:** + +**Input:** nums = [1,2,3,4,5,1], k = 0 + +**Output:** 2 + +**Explanation:** + +The maximum length subsequence is [1,2,3,4,5,1]. + +**Constraints:** + +* 1 <= nums.length <= 5 * 103 +* 1 <= nums[i] <= 109 +* `0 <= k <= min(50, nums.length)` + +## Solution + +```kotlin +import kotlin.math.max + +class Solution { + fun maximumLength(nums: IntArray, k: Int): Int { + val hm = HashMap() + val n = nums.size + val pre = IntArray(n) + for (i in 0 until n) { + pre[i] = hm.getOrDefault(nums[i], -1) + hm[nums[i]] = i + } + val dp = Array(k + 1) { IntArray(n) } + for (i in 0 until n) { + dp[0][i] = 1 + if (pre[i] >= 0) { + dp[0][i] = dp[0][pre[i]] + 1 + } + } + for (i in 1..k) { + var max = 0 + for (j in 0 until n) { + if (pre[j] >= 0) { + dp[i][j] = dp[i][pre[j]] + 1 + } + dp[i][j] = max(dp[i][j], (max + 1)) + max = max(max, dp[i - 1][j]) + } + } + var max = 0 + for (i in 0 until n) { + max = max(max, dp[k][i]) + } + return max + } +} +``` \ No newline at end of file diff --git a/src/main/kotlin/g3101_3200/s3178_find_the_child_who_has_the_ball_after_k_seconds/readme.md b/src/main/kotlin/g3101_3200/s3178_find_the_child_who_has_the_ball_after_k_seconds/readme.md new file mode 100644 index 00000000..0097a6e1 --- /dev/null +++ b/src/main/kotlin/g3101_3200/s3178_find_the_child_who_has_the_ball_after_k_seconds/readme.md @@ -0,0 +1,129 @@ +[![](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) + +## 3178\. Find the Child Who Has the Ball After K Seconds + +Easy + +You are given two **positive** integers `n` and `k`. There are `n` children numbered from `0` to `n - 1` standing in a queue _in order_ from left to right. + +Initially, child 0 holds a ball and the direction of passing the ball is towards the right direction. After each second, the child holding the ball passes it to the child next to them. Once the ball reaches **either** end of the line, i.e. child 0 or child `n - 1`, the direction of passing is **reversed**. + +Return the number of the child who receives the ball after `k` seconds. + +**Example 1:** + +**Input:** n = 3, k = 5 + +**Output:** 1 + +**Explanation:** + +Time elapsed + +Children + +`0` + +[0, 1, 2] + +`1` + +[0, 1, 2] + +`2` + +[0, 1, 2] + +`3` + +[0, 1, 2] + +`4` + +[0, 1, 2] + +`5` + +[0, 1, 2] + +**Example 2:** + +**Input:** n = 5, k = 6 + +**Output:** 2 + +**Explanation:** + +Time elapsed + +Children + +`0` + +[0, 1, 2, 3, 4] + +`1` + +[0, 1, 2, 3, 4] + +`2` + +[0, 1, 2, 3, 4] + +`3` + +[0, 1, 2, 3, 4] + +`4` + +[0, 1, 2, 3, 4] + +`5` + +[0, 1, 2, 3, 4] + +`6` + +[0, 1, 2, 3, 4] + +**Example 3:** + +**Input:** n = 4, k = 2 + +**Output:** 2 + +**Explanation:** + +Time elapsed + +Children + +`0` + +[0, 1, 2, 3] + +`1` + +[0, 1, 2, 3] + +`2` + +[0, 1, 2, 3] + +**Constraints:** + +* `2 <= n <= 50` +* `1 <= k <= 50` + +## Solution + +```kotlin +class Solution { + fun numberOfChild(n: Int, k: Int): Int { + val bigN = 2 * n - 2 + val x = k % bigN + return if (x < n) x else bigN - x + } +} +``` \ No newline at end of file diff --git a/src/main/kotlin/g3101_3200/s3179_find_the_n_th_value_after_k_seconds/readme.md b/src/main/kotlin/g3101_3200/s3179_find_the_n_th_value_after_k_seconds/readme.md new file mode 100644 index 00000000..fa5053b9 --- /dev/null +++ b/src/main/kotlin/g3101_3200/s3179_find_the_n_th_value_after_k_seconds/readme.md @@ -0,0 +1,95 @@ +[![](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) + +## 3179\. Find the N-th Value After K Seconds + +Medium + +You are given two integers `n` and `k`. + +Initially, you start with an array `a` of `n` integers where `a[i] = 1` for all `0 <= i <= n - 1`. After each second, you simultaneously update each element to be the sum of all its preceding elements plus the element itself. For example, after one second, `a[0]` remains the same, `a[1]` becomes `a[0] + a[1]`, `a[2]` becomes `a[0] + a[1] + a[2]`, and so on. + +Return the **value** of `a[n - 1]` after `k` seconds. + +Since the answer may be very large, return it **modulo** 109 + 7. + +**Example 1:** + +**Input:** n = 4, k = 5 + +**Output:** 56 + +**Explanation:** + +| Second | State After | +|--------|-------------------| +| 0 | `[1, 1, 1, 1]` | +| 1 | `[1, 2, 3, 4]` | +| 2 | `[1, 3, 6, 10]` | +| 3 | `[1, 4, 10, 20]` | +| 4 | `[1, 5, 15, 35]` | +| 5 | `[1, 6, 21, 56]` | + +**Example 2:** + +**Input:** n = 5, k = 3 + +**Output:** 35 + +**Explanation:** + +| Second | State After | +|--------|-------------------| +| 0 | `[1, 1, 1, 1, 1]` | +| 1 | `[1, 2, 3, 4, 5]` | +| 2 | `[1, 3, 6, 10, 15]` | +| 3 | `[1, 4, 10, 20, 35]` | + +**Constraints:** + +* `1 <= n, k <= 1000` + +## Solution + +```kotlin +import kotlin.math.pow + +@Suppress("NAME_SHADOWING") +class Solution { + private val mod: Int = (10.0.pow(9.0) + 7).toInt() + + fun valueAfterKSeconds(n: Int, k: Int): Int { + if (n == 1) { + return 1 + } + return combination(k + n - 1, k) + } + + private fun combination(a: Int, b: Int): Int { + var numerator: Long = 1 + var denominator: Long = 1 + for (i in 0 until b) { + numerator = (numerator * (a - i)) % mod + denominator = (denominator * (i + 1)) % mod + } + // Calculate the modular inverse of denominator + val denominatorInverse = power(denominator, mod - 2) + return ((numerator * denominatorInverse) % mod).toInt() + } + + // Function to calculate power + private fun power(x: Long, y: Int): Long { + var x = x + var y = y + var result: Long = 1 + while (y > 0) { + if (y % 2 == 1) { + result = (result * x) % mod + } + y = y shr 1 + x = (x * x) % mod + } + return result + } +} +``` \ No newline at end of file diff --git a/src/main/kotlin/g3101_3200/s3180_maximum_total_reward_using_operations_i/readme.md b/src/main/kotlin/g3101_3200/s3180_maximum_total_reward_using_operations_i/readme.md new file mode 100644 index 00000000..8c70328f --- /dev/null +++ b/src/main/kotlin/g3101_3200/s3180_maximum_total_reward_using_operations_i/readme.md @@ -0,0 +1,99 @@ +[![](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) + +## 3180\. Maximum Total Reward Using Operations I + +Medium + +You are given an integer array `rewardValues` of length `n`, representing the values of rewards. + +Initially, your total reward `x` is 0, and all indices are **unmarked**. You are allowed to perform the following operation **any** number of times: + +* Choose an **unmarked** index `i` from the range `[0, n - 1]`. +* If `rewardValues[i]` is **greater** than your current total reward `x`, then add `rewardValues[i]` to `x` (i.e., `x = x + rewardValues[i]`), and **mark** the index `i`. + +Return an integer denoting the **maximum** _total reward_ you can collect by performing the operations optimally. + +**Example 1:** + +**Input:** rewardValues = [1,1,3,3] + +**Output:** 4 + +**Explanation:** + +During the operations, we can choose to mark the indices 0 and 2 in order, and the total reward will be 4, which is the maximum. + +**Example 2:** + +**Input:** rewardValues = [1,6,4,3,2] + +**Output:** 11 + +**Explanation:** + +Mark the indices 0, 2, and 1 in order. The total reward will then be 11, which is the maximum. + +**Constraints:** + +* `1 <= rewardValues.length <= 2000` +* `1 <= rewardValues[i] <= 2000` + +## Solution + +```kotlin +class Solution { + private fun sortedSet(values: IntArray): IntArray { + var max = 0 + for (x in values) { + if (x > max) { + max = x + } + } + val set = BooleanArray(max + 1) + var n = 0 + for (x in values) { + if (!set[x]) { + set[x] = true + n++ + } + } + val result = IntArray(n) + for (x in max downTo 1) { + if (set[x]) { + result[--n] = x + } + } + return result + } + + fun maxTotalReward(rewardValues: IntArray): Int { + var rewardValues = rewardValues + rewardValues = sortedSet(rewardValues) + val n = rewardValues.size + val max = rewardValues[n - 1] + val isSumPossible = BooleanArray(max) + isSumPossible[0] = true + var maxSum = 0 + var last = 1 + for (sum in rewardValues[0] until max) { + while (last < n && rewardValues[last] <= sum) { + last++ + } + val s2 = sum / 2 + for (i in last - 1 downTo 0) { + val x = rewardValues[i] + if (x <= s2) { + break + } + if (isSumPossible[sum - x]) { + isSumPossible[sum] = true + maxSum = sum + break + } + } + } + return maxSum + max + } +} +``` \ No newline at end of file diff --git a/src/main/kotlin/g3101_3200/s3181_maximum_total_reward_using_operations_ii/readme.md b/src/main/kotlin/g3101_3200/s3181_maximum_total_reward_using_operations_ii/readme.md new file mode 100644 index 00000000..9004e2cd --- /dev/null +++ b/src/main/kotlin/g3101_3200/s3181_maximum_total_reward_using_operations_ii/readme.md @@ -0,0 +1,111 @@ +[![](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) + +## 3181\. Maximum Total Reward Using Operations II + +Hard + +You are given an integer array `rewardValues` of length `n`, representing the values of rewards. + +Initially, your total reward `x` is 0, and all indices are **unmarked**. You are allowed to perform the following operation **any** number of times: + +* Choose an **unmarked** index `i` from the range `[0, n - 1]`. +* If `rewardValues[i]` is **greater** than your current total reward `x`, then add `rewardValues[i]` to `x` (i.e., `x = x + rewardValues[i]`), and **mark** the index `i`. + +Return an integer denoting the **maximum** _total reward_ you can collect by performing the operations optimally. + +**Example 1:** + +**Input:** rewardValues = [1,1,3,3] + +**Output:** 4 + +**Explanation:** + +During the operations, we can choose to mark the indices 0 and 2 in order, and the total reward will be 4, which is the maximum. + +**Example 2:** + +**Input:** rewardValues = [1,6,4,3,2] + +**Output:** 11 + +**Explanation:** + +Mark the indices 0, 2, and 1 in order. The total reward will then be 11, which is the maximum. + +**Constraints:** + +* 1 <= rewardValues.length <= 5 * 104 +* 1 <= rewardValues[i] <= 5 * 104 + +## Solution + +```kotlin +import kotlin.math.max +import kotlin.math.min + +class Solution { + fun maxTotalReward(rewardValues: IntArray): Int { + var max = rewardValues[0] + var n = 0 + for (i in 1 until rewardValues.size) { + max = max(max, rewardValues[i]) + } + val vis = BooleanArray(max + 1) + for (i in rewardValues) { + if (!vis[i]) { + n++ + vis[i] = true + } + } + val rew = IntArray(n) + var j = 0 + for (i in 0..max) { + if (vis[i]) { + rew[j++] = i + } + } + return rew[n - 1] + getAns(rew, n - 1, rew[n - 1] - 1) + } + + private fun getAns(rewards: IntArray, i: Int, validLimit: Int): Int { + var res = 0 + var j = nextElemWithinLimits(rewards, i - 1, validLimit) + while (j >= 0) { + if (res >= rewards[j] + min((validLimit - rewards[j]), (rewards[j] - 1))) { + break + } + res = max( + res, + ( + rewards[j] + + getAns( + rewards, + j, + min((validLimit - rewards[j]), (rewards[j] - 1)) + ) + ) + ) + j-- + } + return res + } + + private fun nextElemWithinLimits(rewards: IntArray, h: Int, k: Int): Int { + var h = h + var l = 0 + var resInd = -1 + while (l <= h) { + val m = (l + h) / 2 + if (rewards[m] <= k) { + resInd = m + l = m + 1 + } else { + h = m - 1 + } + } + return resInd + } +} +``` \ No newline at end of file