From 81afab612f9c6933d3ae25ee85c1345fdd86ce74 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Sat, 15 Jun 2024 08:16:23 +0300 Subject: [PATCH] Added tasks 3174-3181 --- .../g3101_3200/s3174_clear_digits/Solution.kt | 19 +++ .../g3101_3200/s3174_clear_digits/readme.md | 39 ++++++ .../Solution.kt | 24 ++++ .../readme.md | 58 +++++++++ .../Solution.kt | 58 +++++++++ .../readme.md | 33 +++++ .../Solution.kt | 40 ++++++ .../readme.md | 33 +++++ .../Solution.kt | 11 ++ .../readme.md | 114 ++++++++++++++++++ .../Solution.kt | 45 +++++++ .../readme.md | 47 ++++++++ .../Solution.kt | 58 +++++++++ .../readme.md | 37 ++++++ .../Solution.kt | 71 +++++++++++ .../readme.md | 37 ++++++ .../s3174_clear_digits/SolutionTest.kt | 17 +++ .../SolutionTest.kt | 17 +++ .../SolutionTest.kt | 17 +++ .../SolutionTest.kt | 17 +++ .../SolutionTest.kt | 22 ++++ .../SolutionTest.kt | 17 +++ .../SolutionTest.kt | 17 +++ .../SolutionTest.kt | 17 +++ 24 files changed, 865 insertions(+) create mode 100644 src/main/kotlin/g3101_3200/s3174_clear_digits/Solution.kt 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/Solution.kt 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/Solution.kt 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/Solution.kt 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/Solution.kt 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/Solution.kt 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/Solution.kt 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/Solution.kt create mode 100644 src/main/kotlin/g3101_3200/s3181_maximum_total_reward_using_operations_ii/readme.md create mode 100644 src/test/kotlin/g3101_3200/s3174_clear_digits/SolutionTest.kt create mode 100644 src/test/kotlin/g3101_3200/s3175_find_the_first_player_to_win_k_games_in_a_row/SolutionTest.kt create mode 100644 src/test/kotlin/g3101_3200/s3176_find_the_maximum_length_of_a_good_subsequence_i/SolutionTest.kt create mode 100644 src/test/kotlin/g3101_3200/s3177_find_the_maximum_length_of_a_good_subsequence_ii/SolutionTest.kt create mode 100644 src/test/kotlin/g3101_3200/s3178_find_the_child_who_has_the_ball_after_k_seconds/SolutionTest.kt create mode 100644 src/test/kotlin/g3101_3200/s3179_find_the_n_th_value_after_k_seconds/SolutionTest.kt create mode 100644 src/test/kotlin/g3101_3200/s3180_maximum_total_reward_using_operations_i/SolutionTest.kt create mode 100644 src/test/kotlin/g3101_3200/s3181_maximum_total_reward_using_operations_ii/SolutionTest.kt diff --git a/src/main/kotlin/g3101_3200/s3174_clear_digits/Solution.kt b/src/main/kotlin/g3101_3200/s3174_clear_digits/Solution.kt new file mode 100644 index 000000000..4cd9b1bdd --- /dev/null +++ b/src/main/kotlin/g3101_3200/s3174_clear_digits/Solution.kt @@ -0,0 +1,19 @@ +package g3101_3200.s3174_clear_digits + +// #Easy #String #Hash_Table #Simulation #2024_06_15_Time_180_ms_(70.18%)_Space_35.1_MB_(94.74%) + +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() + } +} 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 000000000..9dab03487 --- /dev/null +++ b/src/main/kotlin/g3101_3200/s3174_clear_digits/readme.md @@ -0,0 +1,39 @@ +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. \ 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/Solution.kt b/src/main/kotlin/g3101_3200/s3175_find_the_first_player_to_win_k_games_in_a_row/Solution.kt new file mode 100644 index 000000000..f1e6f1bcc --- /dev/null +++ b/src/main/kotlin/g3101_3200/s3175_find_the_first_player_to_win_k_games_in_a_row/Solution.kt @@ -0,0 +1,24 @@ +package g3101_3200.s3175_find_the_first_player_to_win_k_games_in_a_row + +// #Medium #Array #Simulation #2024_06_15_Time_536_ms_(100.00%)_Space_63.9_MB_(81.82%) + +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 + } +} 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 000000000..0c5e09fdf --- /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,58 @@ +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. \ No newline at end of file diff --git a/src/main/kotlin/g3101_3200/s3176_find_the_maximum_length_of_a_good_subsequence_i/Solution.kt b/src/main/kotlin/g3101_3200/s3176_find_the_maximum_length_of_a_good_subsequence_i/Solution.kt new file mode 100644 index 000000000..335db1b94 --- /dev/null +++ b/src/main/kotlin/g3101_3200/s3176_find_the_maximum_length_of_a_good_subsequence_i/Solution.kt @@ -0,0 +1,58 @@ +package g3101_3200.s3176_find_the_maximum_length_of_a_good_subsequence_i + +// #Medium #Array #Hash_Table #Dynamic_Programming +// #2024_06_15_Time_183_ms_(100.00%)_Space_37.6_MB_(91.30%) + +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 + } +} 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 000000000..2d7b340ef --- /dev/null +++ b/src/main/kotlin/g3101_3200/s3176_find_the_maximum_length_of_a_good_subsequence_i/readme.md @@ -0,0 +1,33 @@ +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)` \ No newline at end of file diff --git a/src/main/kotlin/g3101_3200/s3177_find_the_maximum_length_of_a_good_subsequence_ii/Solution.kt b/src/main/kotlin/g3101_3200/s3177_find_the_maximum_length_of_a_good_subsequence_ii/Solution.kt new file mode 100644 index 000000000..a8713b3b3 --- /dev/null +++ b/src/main/kotlin/g3101_3200/s3177_find_the_maximum_length_of_a_good_subsequence_ii/Solution.kt @@ -0,0 +1,40 @@ +package g3101_3200.s3177_find_the_maximum_length_of_a_good_subsequence_ii + +// #Hard #Array #Hash_Table #Dynamic_Programming +// #2024_06_15_Time_284_ms_(100.00%)_Space_40.3_MB_(100.00%) + +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 + } +} 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 000000000..8c6b300b0 --- /dev/null +++ b/src/main/kotlin/g3101_3200/s3177_find_the_maximum_length_of_a_good_subsequence_ii/readme.md @@ -0,0 +1,33 @@ +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)` \ 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/Solution.kt b/src/main/kotlin/g3101_3200/s3178_find_the_child_who_has_the_ball_after_k_seconds/Solution.kt new file mode 100644 index 000000000..ede879823 --- /dev/null +++ b/src/main/kotlin/g3101_3200/s3178_find_the_child_who_has_the_ball_after_k_seconds/Solution.kt @@ -0,0 +1,11 @@ +package g3101_3200.s3178_find_the_child_who_has_the_ball_after_k_seconds + +// #Easy #Math #Simulation #2024_06_15_Time_136_ms_(82.35%)_Space_33.7_MB_(45.10%) + +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 + } +} 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 000000000..eafcd9098 --- /dev/null +++ b/src/main/kotlin/g3101_3200/s3178_find_the_child_who_has_the_ball_after_k_seconds/readme.md @@ -0,0 +1,114 @@ +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` \ No newline at end of file diff --git a/src/main/kotlin/g3101_3200/s3179_find_the_n_th_value_after_k_seconds/Solution.kt b/src/main/kotlin/g3101_3200/s3179_find_the_n_th_value_after_k_seconds/Solution.kt new file mode 100644 index 000000000..a409bc051 --- /dev/null +++ b/src/main/kotlin/g3101_3200/s3179_find_the_n_th_value_after_k_seconds/Solution.kt @@ -0,0 +1,45 @@ +package g3101_3200.s3179_find_the_n_th_value_after_k_seconds + +// #Medium #Array #Math #Simulation #Prefix_Sum #Combinatorics +// #2024_06_15_Time_175_ms_(100.00%)_Space_33.2_MB_(100.00%) + +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 + } +} 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 000000000..5c0f8c7c0 --- /dev/null +++ b/src/main/kotlin/g3101_3200/s3179_find_the_n_th_value_after_k_seconds/readme.md @@ -0,0 +1,47 @@ +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` \ No newline at end of file diff --git a/src/main/kotlin/g3101_3200/s3180_maximum_total_reward_using_operations_i/Solution.kt b/src/main/kotlin/g3101_3200/s3180_maximum_total_reward_using_operations_i/Solution.kt new file mode 100644 index 000000000..6756d329d --- /dev/null +++ b/src/main/kotlin/g3101_3200/s3180_maximum_total_reward_using_operations_i/Solution.kt @@ -0,0 +1,58 @@ +package g3101_3200.s3180_maximum_total_reward_using_operations_i + +// #Medium #Array #Dynamic_Programming #2024_06_15_Time_183_ms_(100.00%)_Space_36.9_MB_(100.00%) + +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 + } +} 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 000000000..1e99f1b4b --- /dev/null +++ b/src/main/kotlin/g3101_3200/s3180_maximum_total_reward_using_operations_i/readme.md @@ -0,0 +1,37 @@ +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` \ No newline at end of file diff --git a/src/main/kotlin/g3101_3200/s3181_maximum_total_reward_using_operations_ii/Solution.kt b/src/main/kotlin/g3101_3200/s3181_maximum_total_reward_using_operations_ii/Solution.kt new file mode 100644 index 000000000..52cb68337 --- /dev/null +++ b/src/main/kotlin/g3101_3200/s3181_maximum_total_reward_using_operations_ii/Solution.kt @@ -0,0 +1,71 @@ +package g3101_3200.s3181_maximum_total_reward_using_operations_ii + +// #Hard #Array #Dynamic_Programming #Bit_Manipulation +// #2024_06_15_Time_376_ms_(100.00%)_Space_67.5_MB_(12.50%) + +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 + } +} 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 000000000..8f40bfbc0 --- /dev/null +++ b/src/main/kotlin/g3101_3200/s3181_maximum_total_reward_using_operations_ii/readme.md @@ -0,0 +1,37 @@ +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 \ No newline at end of file diff --git a/src/test/kotlin/g3101_3200/s3174_clear_digits/SolutionTest.kt b/src/test/kotlin/g3101_3200/s3174_clear_digits/SolutionTest.kt new file mode 100644 index 000000000..745775583 --- /dev/null +++ b/src/test/kotlin/g3101_3200/s3174_clear_digits/SolutionTest.kt @@ -0,0 +1,17 @@ +package g3101_3200.s3174_clear_digits + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun clearDigits() { + assertThat(Solution().clearDigits("abc"), equalTo("abc")) + } + + @Test + fun clearDigits2() { + assertThat(Solution().clearDigits("cb34"), equalTo("")) + } +} diff --git a/src/test/kotlin/g3101_3200/s3175_find_the_first_player_to_win_k_games_in_a_row/SolutionTest.kt b/src/test/kotlin/g3101_3200/s3175_find_the_first_player_to_win_k_games_in_a_row/SolutionTest.kt new file mode 100644 index 000000000..cbb4d9b09 --- /dev/null +++ b/src/test/kotlin/g3101_3200/s3175_find_the_first_player_to_win_k_games_in_a_row/SolutionTest.kt @@ -0,0 +1,17 @@ +package g3101_3200.s3175_find_the_first_player_to_win_k_games_in_a_row + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun findWinningPlayer() { + assertThat(Solution().findWinningPlayer(intArrayOf(4, 2, 6, 3, 9), 2), equalTo(2)) + } + + @Test + fun findWinningPlayer2() { + assertThat(Solution().findWinningPlayer(intArrayOf(2, 5, 4), 3), equalTo(1)) + } +} diff --git a/src/test/kotlin/g3101_3200/s3176_find_the_maximum_length_of_a_good_subsequence_i/SolutionTest.kt b/src/test/kotlin/g3101_3200/s3176_find_the_maximum_length_of_a_good_subsequence_i/SolutionTest.kt new file mode 100644 index 000000000..ea97dae09 --- /dev/null +++ b/src/test/kotlin/g3101_3200/s3176_find_the_maximum_length_of_a_good_subsequence_i/SolutionTest.kt @@ -0,0 +1,17 @@ +package g3101_3200.s3176_find_the_maximum_length_of_a_good_subsequence_i + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun maximumLength() { + assertThat(Solution().maximumLength(intArrayOf(1, 2, 1, 1, 3), 2), equalTo(4)) + } + + @Test + fun maximumLength2() { + assertThat(Solution().maximumLength(intArrayOf(1, 2, 3, 4, 5, 1), 0), equalTo(2)) + } +} diff --git a/src/test/kotlin/g3101_3200/s3177_find_the_maximum_length_of_a_good_subsequence_ii/SolutionTest.kt b/src/test/kotlin/g3101_3200/s3177_find_the_maximum_length_of_a_good_subsequence_ii/SolutionTest.kt new file mode 100644 index 000000000..06d7569bc --- /dev/null +++ b/src/test/kotlin/g3101_3200/s3177_find_the_maximum_length_of_a_good_subsequence_ii/SolutionTest.kt @@ -0,0 +1,17 @@ +package g3101_3200.s3177_find_the_maximum_length_of_a_good_subsequence_ii + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun maximumLength() { + assertThat(Solution().maximumLength(intArrayOf(1, 2, 1, 1, 3), 2), equalTo(4)) + } + + @Test + fun maximumLength2() { + assertThat(Solution().maximumLength(intArrayOf(1, 2, 3, 4, 5, 1), 0), equalTo(2)) + } +} diff --git a/src/test/kotlin/g3101_3200/s3178_find_the_child_who_has_the_ball_after_k_seconds/SolutionTest.kt b/src/test/kotlin/g3101_3200/s3178_find_the_child_who_has_the_ball_after_k_seconds/SolutionTest.kt new file mode 100644 index 000000000..02221077b --- /dev/null +++ b/src/test/kotlin/g3101_3200/s3178_find_the_child_who_has_the_ball_after_k_seconds/SolutionTest.kt @@ -0,0 +1,22 @@ +package g3101_3200.s3178_find_the_child_who_has_the_ball_after_k_seconds + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun numberOfChild() { + assertThat(Solution().numberOfChild(3, 5), equalTo(1)) + } + + @Test + fun numberOfChild2() { + assertThat(Solution().numberOfChild(5, 6), equalTo(2)) + } + + @Test + fun numberOfChild3() { + assertThat(Solution().numberOfChild(4, 2), equalTo(2)) + } +} diff --git a/src/test/kotlin/g3101_3200/s3179_find_the_n_th_value_after_k_seconds/SolutionTest.kt b/src/test/kotlin/g3101_3200/s3179_find_the_n_th_value_after_k_seconds/SolutionTest.kt new file mode 100644 index 000000000..e1676c93e --- /dev/null +++ b/src/test/kotlin/g3101_3200/s3179_find_the_n_th_value_after_k_seconds/SolutionTest.kt @@ -0,0 +1,17 @@ +package g3101_3200.s3179_find_the_n_th_value_after_k_seconds + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun valueAfterKSeconds() { + assertThat(Solution().valueAfterKSeconds(4, 5), equalTo(56)) + } + + @Test + fun valueAfterKSeconds2() { + assertThat(Solution().valueAfterKSeconds(5, 3), equalTo(35)) + } +} diff --git a/src/test/kotlin/g3101_3200/s3180_maximum_total_reward_using_operations_i/SolutionTest.kt b/src/test/kotlin/g3101_3200/s3180_maximum_total_reward_using_operations_i/SolutionTest.kt new file mode 100644 index 000000000..aaaa80e97 --- /dev/null +++ b/src/test/kotlin/g3101_3200/s3180_maximum_total_reward_using_operations_i/SolutionTest.kt @@ -0,0 +1,17 @@ +package g3101_3200.s3180_maximum_total_reward_using_operations_i + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun maxTotalReward() { + assertThat(Solution().maxTotalReward(intArrayOf(1, 1, 3, 3)), equalTo(4)) + } + + @Test + fun maxTotalReward2() { + assertThat(Solution().maxTotalReward(intArrayOf(1, 6, 4, 3, 2)), equalTo(11)) + } +} diff --git a/src/test/kotlin/g3101_3200/s3181_maximum_total_reward_using_operations_ii/SolutionTest.kt b/src/test/kotlin/g3101_3200/s3181_maximum_total_reward_using_operations_ii/SolutionTest.kt new file mode 100644 index 000000000..6471f8788 --- /dev/null +++ b/src/test/kotlin/g3101_3200/s3181_maximum_total_reward_using_operations_ii/SolutionTest.kt @@ -0,0 +1,17 @@ +package g3101_3200.s3181_maximum_total_reward_using_operations_ii + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun maxTotalReward() { + assertThat(Solution().maxTotalReward(intArrayOf(1, 1, 3, 3)), equalTo(4)) + } + + @Test + fun maxTotalReward2() { + assertThat(Solution().maxTotalReward(intArrayOf(1, 6, 4, 3, 2)), equalTo(11)) + } +}