From eac7356d4198f454d9c0194172db68c832503255 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Sun, 24 Nov 2024 16:52:00 +0200 Subject: [PATCH 1/8] Added tasks 3360-3367 --- .../s3360_stone_removal_game/Solution.kt | 20 +++++++ .../s3360_stone_removal_game/readme.md | 37 ++++++++++++ .../Solution.kt | 28 ++++++++++ .../readme.md | 47 ++++++++++++++++ .../Solution.kt | 30 ++++++++++ .../readme.md | 55 ++++++++++++++++++ .../Solution.kt | 47 ++++++++++++++++ .../readme.md | 56 +++++++++++++++++++ .../Solution.kt | 22 ++++++++ .../readme.md | 52 +++++++++++++++++ .../Solution.kt | 30 ++++++++++ .../readme.md | 54 ++++++++++++++++++ .../s3366_minimum_array_sum/Solution.kt | 41 ++++++++++++++ .../s3366_minimum_array_sum/readme.md | 46 +++++++++++++++ .../Solution.kt | 41 ++++++++++++++ .../readme.md | 47 ++++++++++++++++ .../SolutionTest.java | 23 ++++++++ .../SolutionTest.java | 44 +++++++++++++++ .../SolutionTest.java | 33 +++++++++++ .../SolutionTest.java | 24 ++++++++ .../SolutionTest.java | 24 ++++++++ .../SolutionTest.java | 23 ++++++++ .../s3366_minimum_array_sum/SolutionTest.java | 18 ++++++ .../SolutionTest.java | 34 +++++++++++ 24 files changed, 876 insertions(+) create mode 100644 src/main/kotlin/g3301_3400/s3360_stone_removal_game/Solution.kt create mode 100644 src/main/kotlin/g3301_3400/s3360_stone_removal_game/readme.md create mode 100644 src/main/kotlin/g3301_3400/s3361_shift_distance_between_two_strings/Solution.kt create mode 100644 src/main/kotlin/g3301_3400/s3361_shift_distance_between_two_strings/readme.md create mode 100644 src/main/kotlin/g3301_3400/s3362_zero_array_transformation_iii/Solution.kt create mode 100644 src/main/kotlin/g3301_3400/s3362_zero_array_transformation_iii/readme.md create mode 100644 src/main/kotlin/g3301_3400/s3363_find_the_maximum_number_of_fruits_collected/Solution.kt create mode 100644 src/main/kotlin/g3301_3400/s3363_find_the_maximum_number_of_fruits_collected/readme.md create mode 100644 src/main/kotlin/g3301_3400/s3364_minimum_positive_sum_subarray/Solution.kt create mode 100644 src/main/kotlin/g3301_3400/s3364_minimum_positive_sum_subarray/readme.md create mode 100644 src/main/kotlin/g3301_3400/s3365_rearrange_k_substrings_to_form_target_string/Solution.kt create mode 100644 src/main/kotlin/g3301_3400/s3365_rearrange_k_substrings_to_form_target_string/readme.md create mode 100644 src/main/kotlin/g3301_3400/s3366_minimum_array_sum/Solution.kt create mode 100644 src/main/kotlin/g3301_3400/s3366_minimum_array_sum/readme.md create mode 100644 src/main/kotlin/g3301_3400/s3367_maximize_sum_of_weights_after_edge_removals/Solution.kt create mode 100644 src/main/kotlin/g3301_3400/s3367_maximize_sum_of_weights_after_edge_removals/readme.md create mode 100644 src/test/kotlin/g3301_3400/s3360_stone_removal_game/SolutionTest.java create mode 100644 src/test/kotlin/g3301_3400/s3361_shift_distance_between_two_strings/SolutionTest.java create mode 100644 src/test/kotlin/g3301_3400/s3362_zero_array_transformation_iii/SolutionTest.java create mode 100644 src/test/kotlin/g3301_3400/s3363_find_the_maximum_number_of_fruits_collected/SolutionTest.java create mode 100644 src/test/kotlin/g3301_3400/s3364_minimum_positive_sum_subarray/SolutionTest.java create mode 100644 src/test/kotlin/g3301_3400/s3365_rearrange_k_substrings_to_form_target_string/SolutionTest.java create mode 100644 src/test/kotlin/g3301_3400/s3366_minimum_array_sum/SolutionTest.java create mode 100644 src/test/kotlin/g3301_3400/s3367_maximize_sum_of_weights_after_edge_removals/SolutionTest.java diff --git a/src/main/kotlin/g3301_3400/s3360_stone_removal_game/Solution.kt b/src/main/kotlin/g3301_3400/s3360_stone_removal_game/Solution.kt new file mode 100644 index 000000000..b775c1e99 --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3360_stone_removal_game/Solution.kt @@ -0,0 +1,20 @@ +package g3301_3400.s3360_stone_removal_game + +// #Easy #2024_11_24_Time_0_ms_(100.00%)_Space_33.5_MB_(100.00%) + +class Solution { + fun canAliceWin(n: Int): Boolean { + if (n < 10) { + return false + } + var stonesRemaining = n - 10 + var stonesToBeRemoved = 9 + var i = 1 + while (stonesRemaining >= stonesToBeRemoved) { + stonesRemaining -= stonesToBeRemoved + i++ + stonesToBeRemoved-- + } + return i % 2 != 0 + } +} diff --git a/src/main/kotlin/g3301_3400/s3360_stone_removal_game/readme.md b/src/main/kotlin/g3301_3400/s3360_stone_removal_game/readme.md new file mode 100644 index 000000000..aa45026ba --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3360_stone_removal_game/readme.md @@ -0,0 +1,37 @@ +3360\. Stone Removal Game + +Easy + +Alice and Bob are playing a game where they take turns removing stones from a pile, with _Alice going first_. + +* Alice starts by removing **exactly** 10 stones on her first turn. +* For each subsequent turn, each player removes **exactly** 1 fewer stone than the previous opponent. + +The player who cannot make a move loses the game. + +Given a positive integer `n`, return `true` if Alice wins the game and `false` otherwise. + +**Example 1:** + +**Input:** n = 12 + +**Output:** true + +**Explanation:** + +* Alice removes 10 stones on her first turn, leaving 2 stones for Bob. +* Bob cannot remove 9 stones, so Alice wins. + +**Example 2:** + +**Input:** n = 1 + +**Output:** false + +**Explanation:** + +* Alice cannot remove 10 stones, so Alice loses. + +**Constraints:** + +* `1 <= n <= 50` \ No newline at end of file diff --git a/src/main/kotlin/g3301_3400/s3361_shift_distance_between_two_strings/Solution.kt b/src/main/kotlin/g3301_3400/s3361_shift_distance_between_two_strings/Solution.kt new file mode 100644 index 000000000..2d20b3d22 --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3361_shift_distance_between_two_strings/Solution.kt @@ -0,0 +1,28 @@ +package g3301_3400.s3361_shift_distance_between_two_strings + +// #Medium #2024_11_24_Time_425_ms_(100.00%)_Space_41_MB_(100.00%) + +import kotlin.math.min + +class Solution { + fun shiftDistance(s: String, t: String, nextCost: IntArray, previousCost: IntArray): Long { + var sum: Long = 0 + val n = s.length + for (i in 0..1 <= s.length == t.length <= 105 +* `s` and `t` consist only of lowercase English letters. +* `nextCost.length == previousCost.length == 26` +* 0 <= nextCost[i], previousCost[i] <= 109 \ No newline at end of file diff --git a/src/main/kotlin/g3301_3400/s3362_zero_array_transformation_iii/Solution.kt b/src/main/kotlin/g3301_3400/s3362_zero_array_transformation_iii/Solution.kt new file mode 100644 index 000000000..87131c56c --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3362_zero_array_transformation_iii/Solution.kt @@ -0,0 +1,30 @@ +package g3301_3400.s3362_zero_array_transformation_iii + +// #Medium #2024_11_24_Time_217_ms_(100.00%)_Space_105.7_MB_(100.00%) + +import java.util.PriorityQueue + +class Solution { + fun maxRemoval(nums: IntArray, queries: Array): Int { + queries.sortWith { a: IntArray, b: IntArray -> a[0] - b[0] } + val last = PriorityQueue(Comparator { a: Int?, b: Int? -> b!! - a!! }) + val diffs = IntArray(nums.size + 1) + var idx = 0 + var cur = 0 + for (i in nums.indices) { + while (idx < queries.size && queries[idx][0] == i) { + last.add(queries[idx][1]) + idx++ + } + cur += diffs[i] + while (cur < nums[i] && !last.isEmpty() && last.peek()!! >= i) { + cur++ + diffs[last.poll()!! + 1]-- + } + if (cur < nums[i]) { + return -1 + } + } + return last.size + } +} diff --git a/src/main/kotlin/g3301_3400/s3362_zero_array_transformation_iii/readme.md b/src/main/kotlin/g3301_3400/s3362_zero_array_transformation_iii/readme.md new file mode 100644 index 000000000..c82de843a --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3362_zero_array_transformation_iii/readme.md @@ -0,0 +1,55 @@ +3362\. Zero Array Transformation III + +Medium + +You are given an integer array `nums` of length `n` and a 2D array `queries` where queries[i] = [li, ri]. + +Each `queries[i]` represents the following action on `nums`: + +* Decrement the value at each index in the range [li, ri] in `nums` by **at most** 1. +* The amount by which the value is decremented can be chosen **independently** for each index. + +A **Zero Array** is an array with all its elements equal to 0. + +Return the **maximum** number of elements that can be removed from `queries`, such that `nums` can still be converted to a **zero array** using the _remaining_ queries. If it is not possible to convert `nums` to a **zero array**, return -1. + +**Example 1:** + +**Input:** nums = [2,0,2], queries = [[0,2],[0,2],[1,1]] + +**Output:** 1 + +**Explanation:** + +After removing `queries[2]`, `nums` can still be converted to a zero array. + +* Using `queries[0]`, decrement `nums[0]` and `nums[2]` by 1 and `nums[1]` by 0. +* Using `queries[1]`, decrement `nums[0]` and `nums[2]` by 1 and `nums[1]` by 0. + +**Example 2:** + +**Input:** nums = [1,1,1,1], queries = [[1,3],[0,2],[1,3],[1,2]] + +**Output:** 2 + +**Explanation:** + +We can remove `queries[2]` and `queries[3]`. + +**Example 3:** + +**Input:** nums = [1,2,3,4], queries = [[0,3]] + +**Output:** \-1 + +**Explanation:** + +`nums` cannot be converted to a zero array even after using all the queries. + +**Constraints:** + +* 1 <= nums.length <= 105 +* 0 <= nums[i] <= 105 +* 1 <= queries.length <= 105 +* `queries[i].length == 2` +* 0 <= li <= ri < nums.length \ No newline at end of file diff --git a/src/main/kotlin/g3301_3400/s3363_find_the_maximum_number_of_fruits_collected/Solution.kt b/src/main/kotlin/g3301_3400/s3363_find_the_maximum_number_of_fruits_collected/Solution.kt new file mode 100644 index 000000000..6eee12bb8 --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3363_find_the_maximum_number_of_fruits_collected/Solution.kt @@ -0,0 +1,47 @@ +package g3301_3400.s3363_find_the_maximum_number_of_fruits_collected + +// #Hard #2024_11_24_Time_53_ms_(100.00%)_Space_162_MB_(100.00%) + +import kotlin.math.max + +class Solution { + fun maxCollectedFruits(fruits: Array): Int { + val n = fruits.size + // Set inaccessible cells to 0 + for (i in 0..st child (green) moves on the path `(0,0) -> (1,1) -> (2,2) -> (3, 3)`. +* The 2nd child (red) moves on the path `(0,3) -> (1,2) -> (2,3) -> (3, 3)`. +* The 3rd child (blue) moves on the path `(3,0) -> (3,1) -> (3,2) -> (3, 3)`. + +In total they collect `1 + 6 + 11 + 1 + 4 + 8 + 12 + 13 + 14 + 15 = 100` fruits. + +**Example 2:** + +**Input:** fruits = [[1,1],[1,1]] + +**Output:** 4 + +**Explanation:** + +In this example: + +* The 1st child moves on the path `(0,0) -> (1,1)`. +* The 2nd child moves on the path `(0,1) -> (1,1)`. +* The 3rd child moves on the path `(1,0) -> (1,1)`. + +In total they collect `1 + 1 + 1 + 1 = 4` fruits. + +**Constraints:** + +* `2 <= n == fruits.length == fruits[i].length <= 1000` +* `0 <= fruits[i][j] <= 1000` \ No newline at end of file diff --git a/src/main/kotlin/g3301_3400/s3364_minimum_positive_sum_subarray/Solution.kt b/src/main/kotlin/g3301_3400/s3364_minimum_positive_sum_subarray/Solution.kt new file mode 100644 index 000000000..910013e57 --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3364_minimum_positive_sum_subarray/Solution.kt @@ -0,0 +1,22 @@ +package g3301_3400.s3364_minimum_positive_sum_subarray + +// #Easy #2024_11_24_Time_24_ms_(100.00%)_Space_38_MB_(100.00%) + +class Solution { + fun minimumSumSubarray(nums: List, l: Int, r: Int): Int { + val size = nums.size + var res = -1 + for (s in l..r) { + for (i in 0..size - s) { + var sum = 0 + for (j in i.. 0 && (res == -1 || res > sum)) { + res = sum + } + } + } + return res + } +} diff --git a/src/main/kotlin/g3301_3400/s3364_minimum_positive_sum_subarray/readme.md b/src/main/kotlin/g3301_3400/s3364_minimum_positive_sum_subarray/readme.md new file mode 100644 index 000000000..97fae38a4 --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3364_minimum_positive_sum_subarray/readme.md @@ -0,0 +1,52 @@ +3364\. Minimum Positive Sum Subarray + +Easy + +You are given an integer array `nums` and **two** integers `l` and `r`. Your task is to find the **minimum** sum of a **subarray** whose size is between `l` and `r` (inclusive) and whose sum is greater than 0. + +Return the **minimum** sum of such a subarray. If no such subarray exists, return -1. + +A **subarray** is a contiguous **non-empty** sequence of elements within an array. + +**Example 1:** + +**Input:** nums = [3, -2, 1, 4], l = 2, r = 3 + +**Output:** 1 + +**Explanation:** + +The subarrays of length between `l = 2` and `r = 3` where the sum is greater than 0 are: + +* `[3, -2]` with a sum of 1 +* `[1, 4]` with a sum of 5 +* `[3, -2, 1]` with a sum of 2 +* `[-2, 1, 4]` with a sum of 3 + +Out of these, the subarray `[3, -2]` has a sum of 1, which is the smallest positive sum. Hence, the answer is 1. + +**Example 2:** + +**Input:** nums = [-2, 2, -3, 1], l = 2, r = 3 + +**Output:** \-1 + +**Explanation:** + +There is no subarray of length between `l` and `r` that has a sum greater than 0. So, the answer is -1. + +**Example 3:** + +**Input:** nums = [1, 2, 3, 4], l = 2, r = 4 + +**Output:** 3 + +**Explanation:** + +The subarray `[1, 2]` has a length of 2 and the minimum sum greater than 0. So, the answer is 3. + +**Constraints:** + +* `1 <= nums.length <= 100` +* `1 <= l <= r <= nums.length` +* `-1000 <= nums[i] <= 1000` \ No newline at end of file diff --git a/src/main/kotlin/g3301_3400/s3365_rearrange_k_substrings_to_form_target_string/Solution.kt b/src/main/kotlin/g3301_3400/s3365_rearrange_k_substrings_to_form_target_string/Solution.kt new file mode 100644 index 000000000..b23cf3ab4 --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3365_rearrange_k_substrings_to_form_target_string/Solution.kt @@ -0,0 +1,30 @@ +package g3301_3400.s3365_rearrange_k_substrings_to_form_target_string + +// #Medium #2024_11_24_Time_490_ms_(100.00%)_Space_50.5_MB_(100.00%) + +class Solution { + fun isPossibleToRearrange(s: String, t: String, k: Int): Boolean { + val size = s.length + val div = size / k + val map: MutableMap = HashMap() + run { + var i = 0 + while (i < size) { + val sub = s.substring(i, i + div) + map.put(sub, map.getOrDefault(sub, 0)!! + 1) + i += div + } + } + var i = 0 + while (i < size) { + val sub = t.substring(i, i + div) + if (map.getOrDefault(sub, 0)!! > 0) { + map.put(sub, map[sub]!! - 1) + } else { + return false + } + i += div + } + return true + } +} diff --git a/src/main/kotlin/g3301_3400/s3365_rearrange_k_substrings_to_form_target_string/readme.md b/src/main/kotlin/g3301_3400/s3365_rearrange_k_substrings_to_form_target_string/readme.md new file mode 100644 index 000000000..d9eb1a2db --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3365_rearrange_k_substrings_to_form_target_string/readme.md @@ -0,0 +1,54 @@ +3365\. Rearrange K Substrings to Form Target String + +Medium + +You are given two strings `s` and `t`, both of which are anagrams of each other, and an integer `k`. + +Your task is to determine whether it is possible to split the string `s` into `k` equal-sized substrings, rearrange the substrings, and concatenate them in _any order_ to create a new string that matches the given string `t`. + +Return `true` if this is possible, otherwise, return `false`. + +An **anagram** is a word or phrase formed by rearranging the letters of a different word or phrase, using all the original letters exactly once. + +A **substring** is a contiguous **non-empty** sequence of characters within a string. + +**Example 1:** + +**Input:** s = "abcd", t = "cdab", k = 2 + +**Output:** true + +**Explanation:** + +* Split `s` into 2 substrings of length 2: `["ab", "cd"]`. +* Rearranging these substrings as `["cd", "ab"]`, and then concatenating them results in `"cdab"`, which matches `t`. + +**Example 2:** + +**Input:** s = "aabbcc", t = "bbaacc", k = 3 + +**Output:** true + +**Explanation:** + +* Split `s` into 3 substrings of length 2: `["aa", "bb", "cc"]`. +* Rearranging these substrings as `["bb", "aa", "cc"]`, and then concatenating them results in `"bbaacc"`, which matches `t`. + +**Example 3:** + +**Input:** s = "aabbcc", t = "bbaacc", k = 2 + +**Output:** false + +**Explanation:** + +* Split `s` into 2 substrings of length 3: `["aab", "bcc"]`. +* These substrings cannot be rearranged to form `t = "bbaacc"`, so the output is `false`. + +**Constraints:** + +* 1 <= s.length == t.length <= 2 * 105 +* `1 <= k <= s.length` +* `s.length` is divisible by `k`. +* `s` and `t` consist only of lowercase English letters. +* The input is generated such that `s` and `t` are anagrams of each other. \ No newline at end of file diff --git a/src/main/kotlin/g3301_3400/s3366_minimum_array_sum/Solution.kt b/src/main/kotlin/g3301_3400/s3366_minimum_array_sum/Solution.kt new file mode 100644 index 000000000..97732ab2d --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3366_minimum_array_sum/Solution.kt @@ -0,0 +1,41 @@ +package g3301_3400.s3366_minimum_array_sum + +// #Medium #2024_11_24_Time_59_ms_(100.00%)_Space_53.4_MB_(100.00%) + +import kotlin.math.ceil +import kotlin.math.min + +class Solution { + fun minArraySum(nums: IntArray, k: Int, op1: Int, op2: Int): Int { + val dp = Array?>?>(nums.size) { Array?>(op1 + 1) { arrayOfNulls(op2 + 1) } } + return sub(dp, nums, 0, k, op1, op2) + } + + private fun sub(dp: Array?>?>, nums: IntArray, i: Int, k: Int, op1: Int, op2: Int): Int { + if (i == nums.size) { + return 0 + } + if (dp[i]!![op1]!![op2] != null) { + return dp[i]!![op1]!![op2]!! + } + var res = sub(dp, nums, i + 1, k, op1, op2) + nums[i] + if (nums[i] >= k && op2 > 0) { + res = min(res, (sub(dp, nums, i + 1, k, op1, op2 - 1) + nums[i] - k)) + var v = ceil(nums[i] / 2.0) + if (v < k) { + v = ceil((nums[i] - k) / 2.0) + } else { + v -= k + } + if (op1 > 0) { + res = min(res, (sub(dp, nums, i + 1, k, op1 - 1, op2 - 1) + v).toInt()) + } + } + if (op1 > 0) { + val v = ceil(nums[i] / 2.0) + res = min(res, (sub(dp, nums, i + 1, k, op1 - 1, op2) + v).toInt()) + } + dp[i]!![op1]!![op2] = res + return res + } +} diff --git a/src/main/kotlin/g3301_3400/s3366_minimum_array_sum/readme.md b/src/main/kotlin/g3301_3400/s3366_minimum_array_sum/readme.md new file mode 100644 index 000000000..086dfd9db --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3366_minimum_array_sum/readme.md @@ -0,0 +1,46 @@ +3366\. Minimum Array Sum + +Medium + +You are given an integer array `nums` and three integers `k`, `op1`, and `op2`. + +You can perform the following operations on `nums`: + +* **Operation 1**: Choose an index `i` and divide `nums[i]` by 2, **rounding up** to the nearest whole number. You can perform this operation at most `op1` times, and not more than **once** per index. +* **Operation 2**: Choose an index `i` and subtract `k` from `nums[i]`, but only if `nums[i]` is greater than or equal to `k`. You can perform this operation at most `op2` times, and not more than **once** per index. + +**Note:** Both operations can be applied to the same index, but at most once each. + +Return the **minimum** possible **sum** of all elements in `nums` after performing any number of operations. + +**Example 1:** + +**Input:** nums = [2,8,3,19,3], k = 3, op1 = 1, op2 = 1 + +**Output:** 23 + +**Explanation:** + +* Apply Operation 2 to `nums[1] = 8`, making `nums[1] = 5`. +* Apply Operation 1 to `nums[3] = 19`, making `nums[3] = 10`. +* The resulting array becomes `[2, 5, 3, 10, 3]`, which has the minimum possible sum of 23 after applying the operations. + +**Example 2:** + +**Input:** nums = [2,4,3], k = 3, op1 = 2, op2 = 1 + +**Output:** 3 + +**Explanation:** + +* Apply Operation 1 to `nums[0] = 2`, making `nums[0] = 1`. +* Apply Operation 1 to `nums[1] = 4`, making `nums[1] = 2`. +* Apply Operation 2 to `nums[2] = 3`, making `nums[2] = 0`. +* The resulting array becomes `[1, 2, 0]`, which has the minimum possible sum of 3 after applying the operations. + +**Constraints:** + +* `1 <= nums.length <= 100` +* 0 <= nums[i] <= 105 +* 0 <= k <= 105 +* `0 <= op1, op2 <= nums.length` \ No newline at end of file diff --git a/src/main/kotlin/g3301_3400/s3367_maximize_sum_of_weights_after_edge_removals/Solution.kt b/src/main/kotlin/g3301_3400/s3367_maximize_sum_of_weights_after_edge_removals/Solution.kt new file mode 100644 index 000000000..fef51f3a9 --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3367_maximize_sum_of_weights_after_edge_removals/Solution.kt @@ -0,0 +1,41 @@ +package g3301_3400.s3367_maximize_sum_of_weights_after_edge_removals + +// #Hard #2024_11_24_Time_220_ms_(100.00%)_Space_154.5_MB_(100.00%) + +import java.util.PriorityQueue +import kotlin.math.max + +class Solution { + fun maximizeSumOfWeights(edges: Array, k: Int): Long { + val map = HashMap?>() + for (edge in edges) { + map.computeIfAbsent(edge[0]) { t: Int? -> ArrayList() }!! + .add(intArrayOf(edge[1], edge[2])) + map.computeIfAbsent(edge[1]) { t: Int? -> ArrayList() }!! + .add(intArrayOf(edge[0], edge[2])) + } + return maximizeSumOfWeights(0, -1, k, map)[0] + } + + private fun maximizeSumOfWeights( + v: Int, + from: Int, + k: Int, + map: HashMap?>, + ): LongArray { + var sum: Long = 0 + val queue = PriorityQueue() + for (i in map.get(v)!!) { + if (i[0] != from) { + val next = maximizeSumOfWeights(i[0], v, k, map) + next[1] += i[1].toLong() + sum = sum + max(next[0], next[1]) + if (next[0] < next[1]) { + queue.offer(next[1] - next[0]) + sum = sum - (if (queue.size > k) queue.poll() else 0) + } + } + } + return longArrayOf(sum, sum - (if (queue.size < k) 0 else queue.peek())!!) + } +} diff --git a/src/main/kotlin/g3301_3400/s3367_maximize_sum_of_weights_after_edge_removals/readme.md b/src/main/kotlin/g3301_3400/s3367_maximize_sum_of_weights_after_edge_removals/readme.md new file mode 100644 index 000000000..e28eec246 --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3367_maximize_sum_of_weights_after_edge_removals/readme.md @@ -0,0 +1,47 @@ +3367\. Maximize Sum of Weights after Edge Removals + +Hard + +There exists an **undirected** tree with `n` nodes numbered `0` to `n - 1`. You are given a 2D integer array `edges` of length `n - 1`, where edges[i] = [ui, vi, wi] indicates that there is an edge between nodes ui and vi with weight wi in the tree. + +Your task is to remove _zero or more_ edges such that: + +* Each node has an edge with **at most** `k` other nodes, where `k` is given. +* The sum of the weights of the remaining edges is **maximized**. + +Return the **maximum** possible sum of weights for the remaining edges after making the necessary removals. + +**Example 1:** + +**Input:** edges = [[0,1,4],[0,2,2],[2,3,12],[2,4,6]], k = 2 + +**Output:** 22 + +**Explanation:** + +![](https://assets.leetcode.com/uploads/2024/10/30/test1drawio.png) + +* Node 2 has edges with 3 other nodes. We remove the edge `[0, 2, 2]`, ensuring that no node has edges with more than `k = 2` nodes. +* The sum of weights is 22, and we can't achieve a greater sum. Thus, the answer is 22. + +**Example 2:** + +**Input:** edges = [[0,1,5],[1,2,10],[0,3,15],[3,4,20],[3,5,5],[0,6,10]], k = 3 + +**Output:** 65 + +**Explanation:** + +* Since no node has edges connecting it to more than `k = 3` nodes, we don't remove any edges. +* The sum of weights is 65. Thus, the answer is 65. + +**Constraints:** + +* 2 <= n <= 105 +* `1 <= k <= n - 1` +* `edges.length == n - 1` +* `edges[i].length == 3` +* `0 <= edges[i][0] <= n - 1` +* `0 <= edges[i][1] <= n - 1` +* 1 <= edges[i][2] <= 106 +* The input is generated such that `edges` form a valid tree. \ No newline at end of file diff --git a/src/test/kotlin/g3301_3400/s3360_stone_removal_game/SolutionTest.java b/src/test/kotlin/g3301_3400/s3360_stone_removal_game/SolutionTest.java new file mode 100644 index 000000000..084229aab --- /dev/null +++ b/src/test/kotlin/g3301_3400/s3360_stone_removal_game/SolutionTest.java @@ -0,0 +1,23 @@ +package g3301_3400.s3360_stone_removal_game; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void canAliceWin() { + assertThat(new Solution().canAliceWin(12), equalTo(true)); + } + + @Test + void canAliceWin2() { + assertThat(new Solution().canAliceWin(1), equalTo(false)); + } + + @Test + void canAliceWin3() { + assertThat(new Solution().canAliceWin(19), equalTo(false)); + } +} diff --git a/src/test/kotlin/g3301_3400/s3361_shift_distance_between_two_strings/SolutionTest.java b/src/test/kotlin/g3301_3400/s3361_shift_distance_between_two_strings/SolutionTest.java new file mode 100644 index 000000000..39c2191e0 --- /dev/null +++ b/src/test/kotlin/g3301_3400/s3361_shift_distance_between_two_strings/SolutionTest.java @@ -0,0 +1,44 @@ +package g3301_3400.s3361_shift_distance_between_two_strings; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void shiftDistance() { + assertThat( + new Solution() + .shiftDistance( + "abab", + "baba", + new int[] { + 100, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0 + }, + new int[] { + 1, 100, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0 + }), + equalTo(2L)); + } + + @Test + void shiftDistance2() { + assertThat( + new Solution() + .shiftDistance( + "leet", + "code", + new int[] { + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1 + }, + new int[] { + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1 + }), + equalTo(31L)); + } +} diff --git a/src/test/kotlin/g3301_3400/s3362_zero_array_transformation_iii/SolutionTest.java b/src/test/kotlin/g3301_3400/s3362_zero_array_transformation_iii/SolutionTest.java new file mode 100644 index 000000000..1d5b4591e --- /dev/null +++ b/src/test/kotlin/g3301_3400/s3362_zero_array_transformation_iii/SolutionTest.java @@ -0,0 +1,33 @@ +package g3301_3400.s3362_zero_array_transformation_iii; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void maxRemoval() { + assertThat( + new Solution() + .maxRemoval(new int[] {2, 0, 2}, new int[][] {{0, 2}, {0, 2}, {1, 1}}), + equalTo(1)); + } + + @Test + void maxRemoval2() { + assertThat( + new Solution() + .maxRemoval( + new int[] {1, 1, 1, 1}, + new int[][] {{1, 3}, {0, 2}, {1, 3}, {1, 2}}), + equalTo(2)); + } + + @Test + void maxRemoval3() { + assertThat( + new Solution().maxRemoval(new int[] {1, 2, 3, 4}, new int[][] {{0, 3}}), + equalTo(-1)); + } +} diff --git a/src/test/kotlin/g3301_3400/s3363_find_the_maximum_number_of_fruits_collected/SolutionTest.java b/src/test/kotlin/g3301_3400/s3363_find_the_maximum_number_of_fruits_collected/SolutionTest.java new file mode 100644 index 000000000..ad24df6fc --- /dev/null +++ b/src/test/kotlin/g3301_3400/s3363_find_the_maximum_number_of_fruits_collected/SolutionTest.java @@ -0,0 +1,24 @@ +package g3301_3400.s3363_find_the_maximum_number_of_fruits_collected; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void maxCollectedFruits() { + assertThat( + new Solution() + .maxCollectedFruits( + new int[][] { + {1, 2, 3, 4}, {5, 6, 8, 7}, {9, 10, 11, 12}, {13, 14, 15, 16} + }), + equalTo(100)); + } + + @Test + void maxCollectedFruits2() { + assertThat(new Solution().maxCollectedFruits(new int[][] {{1, 1}, {1, 1}}), equalTo(4)); + } +} diff --git a/src/test/kotlin/g3301_3400/s3364_minimum_positive_sum_subarray/SolutionTest.java b/src/test/kotlin/g3301_3400/s3364_minimum_positive_sum_subarray/SolutionTest.java new file mode 100644 index 000000000..6778792c1 --- /dev/null +++ b/src/test/kotlin/g3301_3400/s3364_minimum_positive_sum_subarray/SolutionTest.java @@ -0,0 +1,24 @@ +package g3301_3400.s3364_minimum_positive_sum_subarray; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import java.util.List; +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void minimumSumSubarray() { + assertThat(new Solution().minimumSumSubarray(List.of(3, -2, 1, 4), 2, 3), equalTo(1)); + } + + @Test + void minimumSumSubarray2() { + assertThat(new Solution().minimumSumSubarray(List.of(-2, 2, -3, 1), 2, 3), equalTo(-1)); + } + + @Test + void minimumSumSubarray3() { + assertThat(new Solution().minimumSumSubarray(List.of(1, 2, 3, 4), 2, 4), equalTo(3)); + } +} diff --git a/src/test/kotlin/g3301_3400/s3365_rearrange_k_substrings_to_form_target_string/SolutionTest.java b/src/test/kotlin/g3301_3400/s3365_rearrange_k_substrings_to_form_target_string/SolutionTest.java new file mode 100644 index 000000000..5d14f5870 --- /dev/null +++ b/src/test/kotlin/g3301_3400/s3365_rearrange_k_substrings_to_form_target_string/SolutionTest.java @@ -0,0 +1,23 @@ +package g3301_3400.s3365_rearrange_k_substrings_to_form_target_string; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void isPossibleToRearrange() { + assertThat(new Solution().isPossibleToRearrange("abcd", "cdab", 2), equalTo(true)); + } + + @Test + void isPossibleToRearrange2() { + assertThat(new Solution().isPossibleToRearrange("aabbcc", "bbaacc", 3), equalTo(true)); + } + + @Test + void isPossibleToRearrange3() { + assertThat(new Solution().isPossibleToRearrange("aabbcc", "bbaacc", 2), equalTo(false)); + } +} diff --git a/src/test/kotlin/g3301_3400/s3366_minimum_array_sum/SolutionTest.java b/src/test/kotlin/g3301_3400/s3366_minimum_array_sum/SolutionTest.java new file mode 100644 index 000000000..aed497bf9 --- /dev/null +++ b/src/test/kotlin/g3301_3400/s3366_minimum_array_sum/SolutionTest.java @@ -0,0 +1,18 @@ +package g3301_3400.s3366_minimum_array_sum; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void minArraySum() { + assertThat(new Solution().minArraySum(new int[] {2, 8, 3, 19, 3}, 3, 1, 1), equalTo(23)); + } + + @Test + void minArraySum2() { + assertThat(new Solution().minArraySum(new int[] {2, 4, 3}, 3, 2, 1), equalTo(3)); + } +} diff --git a/src/test/kotlin/g3301_3400/s3367_maximize_sum_of_weights_after_edge_removals/SolutionTest.java b/src/test/kotlin/g3301_3400/s3367_maximize_sum_of_weights_after_edge_removals/SolutionTest.java new file mode 100644 index 000000000..412a05941 --- /dev/null +++ b/src/test/kotlin/g3301_3400/s3367_maximize_sum_of_weights_after_edge_removals/SolutionTest.java @@ -0,0 +1,34 @@ +package g3301_3400.s3367_maximize_sum_of_weights_after_edge_removals; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void maximizeSumOfWeights() { + assertThat( + new Solution() + .maximizeSumOfWeights( + new int[][] {{0, 1, 4}, {0, 2, 2}, {2, 3, 12}, {2, 4, 6}}, 2), + equalTo(22L)); + } + + @Test + void maximizeSumOfWeights2() { + assertThat( + new Solution() + .maximizeSumOfWeights( + new int[][] { + {0, 1, 5}, + {1, 2, 10}, + {0, 3, 15}, + {3, 4, 20}, + {3, 5, 5}, + {0, 6, 10} + }, + 3), + equalTo(65L)); + } +} From 8e7883e9bd5ce11b213ca412e7b81d6ad6ec5b8a Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Sun, 24 Nov 2024 17:10:37 +0200 Subject: [PATCH 2/8] Rename .java to .kt --- .../{SolutionTest.java => SolutionTest.kt} | 0 .../{SolutionTest.java => SolutionTest.kt} | 0 .../{SolutionTest.java => SolutionTest.kt} | 0 .../{SolutionTest.java => SolutionTest.kt} | 0 .../{SolutionTest.java => SolutionTest.kt} | 0 .../{SolutionTest.java => SolutionTest.kt} | 0 .../{SolutionTest.java => SolutionTest.kt} | 0 .../{SolutionTest.java => SolutionTest.kt} | 0 8 files changed, 0 insertions(+), 0 deletions(-) rename src/test/kotlin/g3301_3400/s3360_stone_removal_game/{SolutionTest.java => SolutionTest.kt} (100%) rename src/test/kotlin/g3301_3400/s3361_shift_distance_between_two_strings/{SolutionTest.java => SolutionTest.kt} (100%) rename src/test/kotlin/g3301_3400/s3362_zero_array_transformation_iii/{SolutionTest.java => SolutionTest.kt} (100%) rename src/test/kotlin/g3301_3400/s3363_find_the_maximum_number_of_fruits_collected/{SolutionTest.java => SolutionTest.kt} (100%) rename src/test/kotlin/g3301_3400/s3364_minimum_positive_sum_subarray/{SolutionTest.java => SolutionTest.kt} (100%) rename src/test/kotlin/g3301_3400/s3365_rearrange_k_substrings_to_form_target_string/{SolutionTest.java => SolutionTest.kt} (100%) rename src/test/kotlin/g3301_3400/s3366_minimum_array_sum/{SolutionTest.java => SolutionTest.kt} (100%) rename src/test/kotlin/g3301_3400/s3367_maximize_sum_of_weights_after_edge_removals/{SolutionTest.java => SolutionTest.kt} (100%) diff --git a/src/test/kotlin/g3301_3400/s3360_stone_removal_game/SolutionTest.java b/src/test/kotlin/g3301_3400/s3360_stone_removal_game/SolutionTest.kt similarity index 100% rename from src/test/kotlin/g3301_3400/s3360_stone_removal_game/SolutionTest.java rename to src/test/kotlin/g3301_3400/s3360_stone_removal_game/SolutionTest.kt diff --git a/src/test/kotlin/g3301_3400/s3361_shift_distance_between_two_strings/SolutionTest.java b/src/test/kotlin/g3301_3400/s3361_shift_distance_between_two_strings/SolutionTest.kt similarity index 100% rename from src/test/kotlin/g3301_3400/s3361_shift_distance_between_two_strings/SolutionTest.java rename to src/test/kotlin/g3301_3400/s3361_shift_distance_between_two_strings/SolutionTest.kt diff --git a/src/test/kotlin/g3301_3400/s3362_zero_array_transformation_iii/SolutionTest.java b/src/test/kotlin/g3301_3400/s3362_zero_array_transformation_iii/SolutionTest.kt similarity index 100% rename from src/test/kotlin/g3301_3400/s3362_zero_array_transformation_iii/SolutionTest.java rename to src/test/kotlin/g3301_3400/s3362_zero_array_transformation_iii/SolutionTest.kt diff --git a/src/test/kotlin/g3301_3400/s3363_find_the_maximum_number_of_fruits_collected/SolutionTest.java b/src/test/kotlin/g3301_3400/s3363_find_the_maximum_number_of_fruits_collected/SolutionTest.kt similarity index 100% rename from src/test/kotlin/g3301_3400/s3363_find_the_maximum_number_of_fruits_collected/SolutionTest.java rename to src/test/kotlin/g3301_3400/s3363_find_the_maximum_number_of_fruits_collected/SolutionTest.kt diff --git a/src/test/kotlin/g3301_3400/s3364_minimum_positive_sum_subarray/SolutionTest.java b/src/test/kotlin/g3301_3400/s3364_minimum_positive_sum_subarray/SolutionTest.kt similarity index 100% rename from src/test/kotlin/g3301_3400/s3364_minimum_positive_sum_subarray/SolutionTest.java rename to src/test/kotlin/g3301_3400/s3364_minimum_positive_sum_subarray/SolutionTest.kt diff --git a/src/test/kotlin/g3301_3400/s3365_rearrange_k_substrings_to_form_target_string/SolutionTest.java b/src/test/kotlin/g3301_3400/s3365_rearrange_k_substrings_to_form_target_string/SolutionTest.kt similarity index 100% rename from src/test/kotlin/g3301_3400/s3365_rearrange_k_substrings_to_form_target_string/SolutionTest.java rename to src/test/kotlin/g3301_3400/s3365_rearrange_k_substrings_to_form_target_string/SolutionTest.kt diff --git a/src/test/kotlin/g3301_3400/s3366_minimum_array_sum/SolutionTest.java b/src/test/kotlin/g3301_3400/s3366_minimum_array_sum/SolutionTest.kt similarity index 100% rename from src/test/kotlin/g3301_3400/s3366_minimum_array_sum/SolutionTest.java rename to src/test/kotlin/g3301_3400/s3366_minimum_array_sum/SolutionTest.kt diff --git a/src/test/kotlin/g3301_3400/s3367_maximize_sum_of_weights_after_edge_removals/SolutionTest.java b/src/test/kotlin/g3301_3400/s3367_maximize_sum_of_weights_after_edge_removals/SolutionTest.kt similarity index 100% rename from src/test/kotlin/g3301_3400/s3367_maximize_sum_of_weights_after_edge_removals/SolutionTest.java rename to src/test/kotlin/g3301_3400/s3367_maximize_sum_of_weights_after_edge_removals/SolutionTest.kt From 55dea6a0caf569d647bd54f4d7bda5b7c6fa397c Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Sun, 24 Nov 2024 17:10:37 +0200 Subject: [PATCH 3/8] Improved tests --- .../Solution.kt | 2 +- .../Solution.kt | 6 +- .../s3360_stone_removal_game/SolutionTest.kt | 23 +++--- .../SolutionTest.kt | 75 ++++++++++--------- .../SolutionTest.kt | 50 +++++++------ .../SolutionTest.kt | 44 +++++++---- .../SolutionTest.kt | 33 ++++---- .../SolutionTest.kt | 35 ++++++--- .../s3366_minimum_array_sum/SolutionTest.kt | 25 ++++--- .../SolutionTest.kt | 61 ++++++++------- 10 files changed, 203 insertions(+), 151 deletions(-) diff --git a/src/main/kotlin/g3301_3400/s3362_zero_array_transformation_iii/Solution.kt b/src/main/kotlin/g3301_3400/s3362_zero_array_transformation_iii/Solution.kt index 87131c56c..4e5d07cdf 100644 --- a/src/main/kotlin/g3301_3400/s3362_zero_array_transformation_iii/Solution.kt +++ b/src/main/kotlin/g3301_3400/s3362_zero_array_transformation_iii/Solution.kt @@ -17,7 +17,7 @@ class Solution { idx++ } cur += diffs[i] - while (cur < nums[i] && !last.isEmpty() && last.peek()!! >= i) { + while (cur < nums[i] && last.isNotEmpty() && last.peek()!! >= i) { cur++ diffs[last.poll()!! + 1]-- } diff --git a/src/main/kotlin/g3301_3400/s3367_maximize_sum_of_weights_after_edge_removals/Solution.kt b/src/main/kotlin/g3301_3400/s3367_maximize_sum_of_weights_after_edge_removals/Solution.kt index fef51f3a9..ab39fccba 100644 --- a/src/main/kotlin/g3301_3400/s3367_maximize_sum_of_weights_after_edge_removals/Solution.kt +++ b/src/main/kotlin/g3301_3400/s3367_maximize_sum_of_weights_after_edge_removals/Solution.kt @@ -9,9 +9,9 @@ class Solution { fun maximizeSumOfWeights(edges: Array, k: Int): Long { val map = HashMap?>() for (edge in edges) { - map.computeIfAbsent(edge[0]) { t: Int? -> ArrayList() }!! + map.computeIfAbsent(edge[0]) { _: Int? -> ArrayList() }!! .add(intArrayOf(edge[1], edge[2])) - map.computeIfAbsent(edge[1]) { t: Int? -> ArrayList() }!! + map.computeIfAbsent(edge[1]) { _: Int? -> ArrayList() }!! .add(intArrayOf(edge[0], edge[2])) } return maximizeSumOfWeights(0, -1, k, map)[0] @@ -25,7 +25,7 @@ class Solution { ): LongArray { var sum: Long = 0 val queue = PriorityQueue() - for (i in map.get(v)!!) { + for (i in map[v]!!) { if (i[0] != from) { val next = maximizeSumOfWeights(i[0], v, k, map) next[1] += i[1].toLong() diff --git a/src/test/kotlin/g3301_3400/s3360_stone_removal_game/SolutionTest.kt b/src/test/kotlin/g3301_3400/s3360_stone_removal_game/SolutionTest.kt index 084229aab..1a1ce52cf 100644 --- a/src/test/kotlin/g3301_3400/s3360_stone_removal_game/SolutionTest.kt +++ b/src/test/kotlin/g3301_3400/s3360_stone_removal_game/SolutionTest.kt @@ -1,23 +1,22 @@ -package g3301_3400.s3360_stone_removal_game; +package g3301_3400.s3360_stone_removal_game -import static org.hamcrest.CoreMatchers.equalTo; -import static org.hamcrest.MatcherAssert.assertThat; +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test -import org.junit.jupiter.api.Test; - -class SolutionTest { +internal class SolutionTest { @Test - void canAliceWin() { - assertThat(new Solution().canAliceWin(12), equalTo(true)); + fun canAliceWin() { + assertThat(Solution().canAliceWin(12), equalTo(true)) } @Test - void canAliceWin2() { - assertThat(new Solution().canAliceWin(1), equalTo(false)); + fun canAliceWin2() { + assertThat(Solution().canAliceWin(1), equalTo(false)) } @Test - void canAliceWin3() { - assertThat(new Solution().canAliceWin(19), equalTo(false)); + fun canAliceWin3() { + assertThat(Solution().canAliceWin(19), equalTo(false)) } } diff --git a/src/test/kotlin/g3301_3400/s3361_shift_distance_between_two_strings/SolutionTest.kt b/src/test/kotlin/g3301_3400/s3361_shift_distance_between_two_strings/SolutionTest.kt index 39c2191e0..6c2685673 100644 --- a/src/test/kotlin/g3301_3400/s3361_shift_distance_between_two_strings/SolutionTest.kt +++ b/src/test/kotlin/g3301_3400/s3361_shift_distance_between_two_strings/SolutionTest.kt @@ -1,44 +1,47 @@ -package g3301_3400.s3361_shift_distance_between_two_strings; +package g3301_3400.s3361_shift_distance_between_two_strings -import static org.hamcrest.CoreMatchers.equalTo; -import static org.hamcrest.MatcherAssert.assertThat; +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test -import org.junit.jupiter.api.Test; - -class SolutionTest { +internal class SolutionTest { @Test - void shiftDistance() { - assertThat( - new Solution() - .shiftDistance( - "abab", - "baba", - new int[] { - 100, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0 - }, - new int[] { - 1, 100, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0 - }), - equalTo(2L)); + fun shiftDistance() { + assertThat( + Solution() + .shiftDistance( + "abab", + "baba", + intArrayOf( + 100, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, + ), + intArrayOf( + 1, 100, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, + ), + ), + equalTo(2L), + ) } @Test - void shiftDistance2() { - assertThat( - new Solution() - .shiftDistance( - "leet", - "code", - new int[] { - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1 - }, - new int[] { - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1 - }), - equalTo(31L)); + fun shiftDistance2() { + assertThat( + Solution() + .shiftDistance( + "leet", + "code", + intArrayOf( + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, + ), + intArrayOf( + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, + ), + ), + equalTo(31L), + ) } } diff --git a/src/test/kotlin/g3301_3400/s3362_zero_array_transformation_iii/SolutionTest.kt b/src/test/kotlin/g3301_3400/s3362_zero_array_transformation_iii/SolutionTest.kt index 1d5b4591e..27c57506b 100644 --- a/src/test/kotlin/g3301_3400/s3362_zero_array_transformation_iii/SolutionTest.kt +++ b/src/test/kotlin/g3301_3400/s3362_zero_array_transformation_iii/SolutionTest.kt @@ -1,33 +1,39 @@ -package g3301_3400.s3362_zero_array_transformation_iii; +package g3301_3400.s3362_zero_array_transformation_iii -import static org.hamcrest.CoreMatchers.equalTo; -import static org.hamcrest.MatcherAssert.assertThat; +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test -import org.junit.jupiter.api.Test; - -class SolutionTest { +internal class SolutionTest { @Test - void maxRemoval() { - assertThat( - new Solution() - .maxRemoval(new int[] {2, 0, 2}, new int[][] {{0, 2}, {0, 2}, {1, 1}}), - equalTo(1)); + fun maxRemoval() { + assertThat( + Solution() + .maxRemoval( + intArrayOf(2, 0, 2), + arrayOf(intArrayOf(0, 2), intArrayOf(0, 2), intArrayOf(1, 1)), + ), + equalTo(1), + ) } @Test - void maxRemoval2() { - assertThat( - new Solution() - .maxRemoval( - new int[] {1, 1, 1, 1}, - new int[][] {{1, 3}, {0, 2}, {1, 3}, {1, 2}}), - equalTo(2)); + fun maxRemoval2() { + assertThat( + Solution() + .maxRemoval( + intArrayOf(1, 1, 1, 1), + arrayOf(intArrayOf(1, 3), intArrayOf(0, 2), intArrayOf(1, 3), intArrayOf(1, 2)), + ), + equalTo(2), + ) } @Test - void maxRemoval3() { - assertThat( - new Solution().maxRemoval(new int[] {1, 2, 3, 4}, new int[][] {{0, 3}}), - equalTo(-1)); + fun maxRemoval3() { + assertThat( + Solution().maxRemoval(intArrayOf(1, 2, 3, 4), arrayOf(intArrayOf(0, 3))), + equalTo(-1), + ) } } diff --git a/src/test/kotlin/g3301_3400/s3363_find_the_maximum_number_of_fruits_collected/SolutionTest.kt b/src/test/kotlin/g3301_3400/s3363_find_the_maximum_number_of_fruits_collected/SolutionTest.kt index ad24df6fc..0800baff1 100644 --- a/src/test/kotlin/g3301_3400/s3363_find_the_maximum_number_of_fruits_collected/SolutionTest.kt +++ b/src/test/kotlin/g3301_3400/s3363_find_the_maximum_number_of_fruits_collected/SolutionTest.kt @@ -1,24 +1,36 @@ -package g3301_3400.s3363_find_the_maximum_number_of_fruits_collected; +package g3301_3400.s3363_find_the_maximum_number_of_fruits_collected -import static org.hamcrest.CoreMatchers.equalTo; -import static org.hamcrest.MatcherAssert.assertThat; +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test -import org.junit.jupiter.api.Test; - -class SolutionTest { +internal class SolutionTest { @Test - void maxCollectedFruits() { - assertThat( - new Solution() - .maxCollectedFruits( - new int[][] { - {1, 2, 3, 4}, {5, 6, 8, 7}, {9, 10, 11, 12}, {13, 14, 15, 16} - }), - equalTo(100)); + fun maxCollectedFruits() { + assertThat( + Solution() + .maxCollectedFruits( + arrayOf( + intArrayOf(1, 2, 3, 4), + intArrayOf(5, 6, 8, 7), + intArrayOf(9, 10, 11, 12), + intArrayOf(13, 14, 15, 16), + ), + ), + equalTo(100), + ) } @Test - void maxCollectedFruits2() { - assertThat(new Solution().maxCollectedFruits(new int[][] {{1, 1}, {1, 1}}), equalTo(4)); + fun maxCollectedFruits2() { + assertThat( + Solution().maxCollectedFruits( + arrayOf( + intArrayOf(1, 1), + intArrayOf(1, 1), + ), + ), + equalTo(4), + ) } } diff --git a/src/test/kotlin/g3301_3400/s3364_minimum_positive_sum_subarray/SolutionTest.kt b/src/test/kotlin/g3301_3400/s3364_minimum_positive_sum_subarray/SolutionTest.kt index 6778792c1..3157d79e2 100644 --- a/src/test/kotlin/g3301_3400/s3364_minimum_positive_sum_subarray/SolutionTest.kt +++ b/src/test/kotlin/g3301_3400/s3364_minimum_positive_sum_subarray/SolutionTest.kt @@ -1,24 +1,31 @@ -package g3301_3400.s3364_minimum_positive_sum_subarray; +package g3301_3400.s3364_minimum_positive_sum_subarray -import static org.hamcrest.CoreMatchers.equalTo; -import static org.hamcrest.MatcherAssert.assertThat; +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test -import java.util.List; -import org.junit.jupiter.api.Test; - -class SolutionTest { +internal class SolutionTest { @Test - void minimumSumSubarray() { - assertThat(new Solution().minimumSumSubarray(List.of(3, -2, 1, 4), 2, 3), equalTo(1)); + fun minimumSumSubarray() { + assertThat( + Solution().minimumSumSubarray(listOf(3, -2, 1, 4), 2, 3), + equalTo(1), + ) } @Test - void minimumSumSubarray2() { - assertThat(new Solution().minimumSumSubarray(List.of(-2, 2, -3, 1), 2, 3), equalTo(-1)); + fun minimumSumSubarray2() { + assertThat( + Solution().minimumSumSubarray(listOf(-2, 2, -3, 1), 2, 3), + equalTo(-1), + ) } @Test - void minimumSumSubarray3() { - assertThat(new Solution().minimumSumSubarray(List.of(1, 2, 3, 4), 2, 4), equalTo(3)); + fun minimumSumSubarray3() { + assertThat( + Solution().minimumSumSubarray(mutableListOf(1, 2, 3, 4), 2, 4), + equalTo(3), + ) } } diff --git a/src/test/kotlin/g3301_3400/s3365_rearrange_k_substrings_to_form_target_string/SolutionTest.kt b/src/test/kotlin/g3301_3400/s3365_rearrange_k_substrings_to_form_target_string/SolutionTest.kt index 5d14f5870..1ece00b4e 100644 --- a/src/test/kotlin/g3301_3400/s3365_rearrange_k_substrings_to_form_target_string/SolutionTest.kt +++ b/src/test/kotlin/g3301_3400/s3365_rearrange_k_substrings_to_form_target_string/SolutionTest.kt @@ -1,23 +1,34 @@ -package g3301_3400.s3365_rearrange_k_substrings_to_form_target_string; +package g3301_3400.s3365_rearrange_k_substrings_to_form_target_string -import static org.hamcrest.CoreMatchers.equalTo; -import static org.hamcrest.MatcherAssert.assertThat; +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test -import org.junit.jupiter.api.Test; - -class SolutionTest { +internal class SolutionTest { @Test - void isPossibleToRearrange() { - assertThat(new Solution().isPossibleToRearrange("abcd", "cdab", 2), equalTo(true)); + fun isPossibleToRearrange() { + assertThat( + Solution() + .isPossibleToRearrange("abcd", "cdab", 2), + equalTo(true), + ) } @Test - void isPossibleToRearrange2() { - assertThat(new Solution().isPossibleToRearrange("aabbcc", "bbaacc", 3), equalTo(true)); + fun isPossibleToRearrange2() { + assertThat( + Solution() + .isPossibleToRearrange("aabbcc", "bbaacc", 3), + equalTo(true), + ) } @Test - void isPossibleToRearrange3() { - assertThat(new Solution().isPossibleToRearrange("aabbcc", "bbaacc", 2), equalTo(false)); + fun isPossibleToRearrange3() { + assertThat( + Solution() + .isPossibleToRearrange("aabbcc", "bbaacc", 2), + equalTo(false), + ) } } diff --git a/src/test/kotlin/g3301_3400/s3366_minimum_array_sum/SolutionTest.kt b/src/test/kotlin/g3301_3400/s3366_minimum_array_sum/SolutionTest.kt index aed497bf9..080f58ef3 100644 --- a/src/test/kotlin/g3301_3400/s3366_minimum_array_sum/SolutionTest.kt +++ b/src/test/kotlin/g3301_3400/s3366_minimum_array_sum/SolutionTest.kt @@ -1,18 +1,23 @@ -package g3301_3400.s3366_minimum_array_sum; +package g3301_3400.s3366_minimum_array_sum -import static org.hamcrest.CoreMatchers.equalTo; -import static org.hamcrest.MatcherAssert.assertThat; +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test -import org.junit.jupiter.api.Test; - -class SolutionTest { +internal class SolutionTest { @Test - void minArraySum() { - assertThat(new Solution().minArraySum(new int[] {2, 8, 3, 19, 3}, 3, 1, 1), equalTo(23)); + fun minArraySum() { + assertThat( + Solution().minArraySum(intArrayOf(2, 8, 3, 19, 3), 3, 1, 1), + equalTo(23), + ) } @Test - void minArraySum2() { - assertThat(new Solution().minArraySum(new int[] {2, 4, 3}, 3, 2, 1), equalTo(3)); + fun minArraySum2() { + assertThat( + Solution().minArraySum(intArrayOf(2, 4, 3), 3, 2, 1), + equalTo(3), + ) } } diff --git a/src/test/kotlin/g3301_3400/s3367_maximize_sum_of_weights_after_edge_removals/SolutionTest.kt b/src/test/kotlin/g3301_3400/s3367_maximize_sum_of_weights_after_edge_removals/SolutionTest.kt index 412a05941..189a8ed9e 100644 --- a/src/test/kotlin/g3301_3400/s3367_maximize_sum_of_weights_after_edge_removals/SolutionTest.kt +++ b/src/test/kotlin/g3301_3400/s3367_maximize_sum_of_weights_after_edge_removals/SolutionTest.kt @@ -1,34 +1,43 @@ -package g3301_3400.s3367_maximize_sum_of_weights_after_edge_removals; +package g3301_3400.s3367_maximize_sum_of_weights_after_edge_removals -import static org.hamcrest.CoreMatchers.equalTo; -import static org.hamcrest.MatcherAssert.assertThat; +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test -import org.junit.jupiter.api.Test; - -class SolutionTest { +internal class SolutionTest { @Test - void maximizeSumOfWeights() { - assertThat( - new Solution() - .maximizeSumOfWeights( - new int[][] {{0, 1, 4}, {0, 2, 2}, {2, 3, 12}, {2, 4, 6}}, 2), - equalTo(22L)); + fun maximizeSumOfWeights() { + assertThat( + Solution() + .maximizeSumOfWeights( + arrayOf( + intArrayOf(0, 1, 4), + intArrayOf(0, 2, 2), + intArrayOf(2, 3, 12), + intArrayOf(2, 4, 6), + ), + 2, + ), + equalTo(22L), + ) } @Test - void maximizeSumOfWeights2() { - assertThat( - new Solution() - .maximizeSumOfWeights( - new int[][] { - {0, 1, 5}, - {1, 2, 10}, - {0, 3, 15}, - {3, 4, 20}, - {3, 5, 5}, - {0, 6, 10} - }, - 3), - equalTo(65L)); + fun maximizeSumOfWeights2() { + assertThat( + Solution() + .maximizeSumOfWeights( + arrayOf( + intArrayOf(0, 1, 5), + intArrayOf(1, 2, 10), + intArrayOf(0, 3, 15), + intArrayOf(3, 4, 20), + intArrayOf(3, 5, 5), + intArrayOf(0, 6, 10), + ), + 3, + ), + equalTo(65L), + ) } } From 66da5bf15f44268fa0b22b03d4469fa4c8a8940a Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Wed, 27 Nov 2024 11:33:53 +0200 Subject: [PATCH 4/8] Improved task 3367 --- .../Solution.kt | 59 +++++++++++-------- 1 file changed, 34 insertions(+), 25 deletions(-) diff --git a/src/main/kotlin/g3301_3400/s3367_maximize_sum_of_weights_after_edge_removals/Solution.kt b/src/main/kotlin/g3301_3400/s3367_maximize_sum_of_weights_after_edge_removals/Solution.kt index ab39fccba..216aaa10b 100644 --- a/src/main/kotlin/g3301_3400/s3367_maximize_sum_of_weights_after_edge_removals/Solution.kt +++ b/src/main/kotlin/g3301_3400/s3367_maximize_sum_of_weights_after_edge_removals/Solution.kt @@ -1,41 +1,50 @@ package g3301_3400.s3367_maximize_sum_of_weights_after_edge_removals -// #Hard #2024_11_24_Time_220_ms_(100.00%)_Space_154.5_MB_(100.00%) +// #Hard #2024_11_27_Time_118_ms_(100.00%)_Space_140.5_MB_(100.00%) import java.util.PriorityQueue import kotlin.math.max class Solution { + private lateinit var adj: Array> + private var k = 0 + fun maximizeSumOfWeights(edges: Array, k: Int): Long { - val map = HashMap?>() - for (edge in edges) { - map.computeIfAbsent(edge[0]) { _: Int? -> ArrayList() }!! - .add(intArrayOf(edge[1], edge[2])) - map.computeIfAbsent(edge[1]) { _: Int? -> ArrayList() }!! - .add(intArrayOf(edge[0], edge[2])) + val n = edges.size + 1 + adj = Array(n) { ArrayList() } + this.k = k + for (i in 0..() + } + for (e in edges) { + adj[e[0]].add(e) + adj[e[1]].add(e) } - return maximizeSumOfWeights(0, -1, k, map)[0] + return dfs(0, -1)[1] } - private fun maximizeSumOfWeights( - v: Int, - from: Int, - k: Int, - map: HashMap?>, - ): LongArray { + private fun dfs(v: Int, parent: Int): LongArray { var sum: Long = 0 - val queue = PriorityQueue() - for (i in map[v]!!) { - if (i[0] != from) { - val next = maximizeSumOfWeights(i[0], v, k, map) - next[1] += i[1].toLong() - sum = sum + max(next[0], next[1]) - if (next[0] < next[1]) { - queue.offer(next[1] - next[0]) - sum = sum - (if (queue.size > k) queue.poll() else 0) - } + val pq = PriorityQueue() + for (e in adj[v]) { + val w = if (e[0] == v) e[1] else e[0] + if (w == parent) { + continue } + val res = dfs(w, v) + val max = max((e[2] + res[0]), res[1]) + sum += max + pq.add(max - res[1]) + } + val res = LongArray(2) + while (pq.size > k) { + sum -= pq.poll()!! + } + res[1] = sum + while (pq.size > k - 1) { + sum -= pq.poll()!! } - return longArrayOf(sum, sum - (if (queue.size < k) 0 else queue.peek())!!) + res[0] = sum + return res } } From 5d5590857876b1b24b7136e38e9b6e4821514b46 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Tue, 3 Dec 2024 03:45:49 +0200 Subject: [PATCH 5/8] Added tasks 3370-3373 --- .../Solution.kt | 13 +++ .../readme.md | 43 ++++++++ .../Solution.kt | 30 ++++++ .../readme.md | 47 +++++++++ .../Solution.kt | 97 +++++++++++++++++++ .../readme.md | 54 +++++++++++ .../Solution.kt | 72 ++++++++++++++ .../readme.md | 53 ++++++++++ .../SolutionTest.kt | 22 +++++ .../SolutionTest.kt | 43 ++++++++ .../SolutionTest.kt | 41 ++++++++ .../SolutionTest.kt | 39 ++++++++ 12 files changed, 554 insertions(+) create mode 100644 src/main/kotlin/g3301_3400/s3370_smallest_number_with_all_set_bits/Solution.kt create mode 100644 src/main/kotlin/g3301_3400/s3370_smallest_number_with_all_set_bits/readme.md create mode 100644 src/main/kotlin/g3301_3400/s3371_identify_the_largest_outlier_in_an_array/Solution.kt create mode 100644 src/main/kotlin/g3301_3400/s3371_identify_the_largest_outlier_in_an_array/readme.md create mode 100644 src/main/kotlin/g3301_3400/s3372_maximize_the_number_of_target_nodes_after_connecting_trees_i/Solution.kt create mode 100644 src/main/kotlin/g3301_3400/s3372_maximize_the_number_of_target_nodes_after_connecting_trees_i/readme.md create mode 100644 src/main/kotlin/g3301_3400/s3373_maximize_the_number_of_target_nodes_after_connecting_trees_ii/Solution.kt create mode 100644 src/main/kotlin/g3301_3400/s3373_maximize_the_number_of_target_nodes_after_connecting_trees_ii/readme.md create mode 100644 src/test/kotlin/g3301_3400/s3370_smallest_number_with_all_set_bits/SolutionTest.kt create mode 100644 src/test/kotlin/g3301_3400/s3371_identify_the_largest_outlier_in_an_array/SolutionTest.kt create mode 100644 src/test/kotlin/g3301_3400/s3372_maximize_the_number_of_target_nodes_after_connecting_trees_i/SolutionTest.kt create mode 100644 src/test/kotlin/g3301_3400/s3373_maximize_the_number_of_target_nodes_after_connecting_trees_ii/SolutionTest.kt diff --git a/src/main/kotlin/g3301_3400/s3370_smallest_number_with_all_set_bits/Solution.kt b/src/main/kotlin/g3301_3400/s3370_smallest_number_with_all_set_bits/Solution.kt new file mode 100644 index 000000000..5342373a2 --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3370_smallest_number_with_all_set_bits/Solution.kt @@ -0,0 +1,13 @@ +package g3301_3400.s3370_smallest_number_with_all_set_bits + +// #Easy #Math #Bit_Manipulation #2024_12_03_Time_0_ms_(100.00%)_Space_41.1_MB_(45.50%) + +class Solution { + fun smallestNumber(n: Int): Int { + var res = 1 + while (res < n) { + res = res * 2 + 1 + } + return res + } +} diff --git a/src/main/kotlin/g3301_3400/s3370_smallest_number_with_all_set_bits/readme.md b/src/main/kotlin/g3301_3400/s3370_smallest_number_with_all_set_bits/readme.md new file mode 100644 index 000000000..ce5862a88 --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3370_smallest_number_with_all_set_bits/readme.md @@ -0,0 +1,43 @@ +3370\. Smallest Number With All Set Bits + +Easy + +You are given a _positive_ number `n`. + +Return the **smallest** number `x` **greater than** or **equal to** `n`, such that the binary representation of `x` contains only **set** bits. + +A **set** bit refers to a bit in the binary representation of a number that has a value of `1`. + +**Example 1:** + +**Input:** n = 5 + +**Output:** 7 + +**Explanation:** + +The binary representation of 7 is `"111"`. + +**Example 2:** + +**Input:** n = 10 + +**Output:** 15 + +**Explanation:** + +The binary representation of 15 is `"1111"`. + +**Example 3:** + +**Input:** n = 3 + +**Output:** 3 + +**Explanation:** + +The binary representation of 3 is `"11"`. + +**Constraints:** + +* `1 <= n <= 1000` \ No newline at end of file diff --git a/src/main/kotlin/g3301_3400/s3371_identify_the_largest_outlier_in_an_array/Solution.kt b/src/main/kotlin/g3301_3400/s3371_identify_the_largest_outlier_in_an_array/Solution.kt new file mode 100644 index 000000000..a2a141c44 --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3371_identify_the_largest_outlier_in_an_array/Solution.kt @@ -0,0 +1,30 @@ +package g3301_3400.s3371_identify_the_largest_outlier_in_an_array + +// #Medium #Array #Hash_Table #Counting #Enumeration +// #2024_12_03_Time_5_ms_(100.00%)_Space_60.6_MB_(33.40%) + +class Solution { + fun getLargestOutlier(nums: IntArray): Int { + val cnt = IntArray(2001) + var sum = 0 + for (i in nums) { + sum += i + cnt[i + 1000]++ + } + for (i in cnt.indices.reversed()) { + val j = i - 1000 + if (cnt[i] == 0) { + continue + } + sum -= j + val csum = (sum shr 1) + 1000 + cnt[i]-- + if (sum % 2 == 0 && csum >= 0 && csum < cnt.size && cnt[csum] > 0) { + return j + } + sum += j + cnt[i]++ + } + return 0 + } +} diff --git a/src/main/kotlin/g3301_3400/s3371_identify_the_largest_outlier_in_an_array/readme.md b/src/main/kotlin/g3301_3400/s3371_identify_the_largest_outlier_in_an_array/readme.md new file mode 100644 index 000000000..091cd961f --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3371_identify_the_largest_outlier_in_an_array/readme.md @@ -0,0 +1,47 @@ +3371\. Identify the Largest Outlier in an Array + +Medium + +You are given an integer array `nums`. This array contains `n` elements, where **exactly** `n - 2` elements are **special** **numbers**. One of the remaining **two** elements is the _sum_ of these **special numbers**, and the other is an **outlier**. + +An **outlier** is defined as a number that is _neither_ one of the original special numbers _nor_ the element representing the sum of those numbers. + +**Note** that special numbers, the sum element, and the outlier must have **distinct** indices, but _may_ share the **same** value. + +Return the **largest** potential **outlier** in `nums`. + +**Example 1:** + +**Input:** nums = [2,3,5,10] + +**Output:** 10 + +**Explanation:** + +The special numbers could be 2 and 3, thus making their sum 5 and the outlier 10. + +**Example 2:** + +**Input:** nums = [-2,-1,-3,-6,4] + +**Output:** 4 + +**Explanation:** + +The special numbers could be -2, -1, and -3, thus making their sum -6 and the outlier 4. + +**Example 3:** + +**Input:** nums = [1,1,1,1,1,5,5] + +**Output:** 5 + +**Explanation:** + +The special numbers could be 1, 1, 1, 1, and 1, thus making their sum 5 and the other 5 as the outlier. + +**Constraints:** + +* 3 <= nums.length <= 105 +* `-1000 <= nums[i] <= 1000` +* The input is generated such that at least **one** potential outlier exists in `nums`. \ No newline at end of file diff --git a/src/main/kotlin/g3301_3400/s3372_maximize_the_number_of_target_nodes_after_connecting_trees_i/Solution.kt b/src/main/kotlin/g3301_3400/s3372_maximize_the_number_of_target_nodes_after_connecting_trees_i/Solution.kt new file mode 100644 index 000000000..b5d7e8d25 --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3372_maximize_the_number_of_target_nodes_after_connecting_trees_i/Solution.kt @@ -0,0 +1,97 @@ +package g3301_3400.s3372_maximize_the_number_of_target_nodes_after_connecting_trees_i + +// #Medium #Tree #Depth_First_Search #Breadth_First_Search +// #2024_12_03_Time_50_ms_(99.49%)_Space_75.7_MB_(5.10%) + +import kotlin.math.max + +class Solution { + private fun getGraph(edges: Array): Array?> { + val n = edges.size + 1 + val graph: Array?> = arrayOfNulls?>(n) + for (i in 0..() + } + for (edge in edges) { + val u = edge[0] + val v = edge[1] + graph[u]!!.add(v) + graph[v]!!.add(u) + } + return graph + } + + private fun dfs(graph: Array?>, u: Int, pt: Int, dp: Array, k: Int) { + for (v in graph[u]!!) { + if (v == pt) { + continue + } + dfs(graph, v!!, u, dp, k) + for (i in 0..?>, + u: Int, + pt: Int, + ptv: IntArray, + fdp: Array, + dp: Array, + k: Int, + ) { + fdp[u]!![0] = dp[u]!![0] + for (i in 1..k) { + fdp[u]!![i] = (dp[u]!![i] + ptv[i - 1]) + } + for (v in graph[u]!!) { + if (v == pt) { + continue + } + val nptv = IntArray(k + 1) + for (i in 0.., k: Int): Array { + val graph = getGraph(edges) + val n = graph.size + val dp = Array(n) { IntArray(k + 1) } + val fdp = Array(n) { IntArray(k + 1) } + dfs(graph, 0, -1, dp, k) + dfs2(graph, 0, -1, IntArray(k + 1), fdp, dp, k) + for (i in 0.., edges2: Array, k: Int): IntArray { + val a = get(edges1, k) + val b = get(edges2, k) + val n = a.size + val m = b.size + val ans = IntArray(n) + var max = 0 + run { + var i = 0 + while (k != 0 && i < m) { + max = max(max.toDouble(), b[i]!![k - 1].toDouble()).toInt() + i++ + } + } + for (i in 0..edges1[i] = [ai, bi] indicates that there is an edge between nodes ai and bi in the first tree and edges2[i] = [ui, vi] indicates that there is an edge between nodes ui and vi in the second tree. You are also given an integer `k`. + +Node `u` is **target** to node `v` if the number of edges on the path from `u` to `v` is less than or equal to `k`. **Note** that a node is _always_ **target** to itself. + +Return an array of `n` integers `answer`, where `answer[i]` is the **maximum** possible number of nodes **target** to node `i` of the first tree if you have to connect one node from the first tree to another node in the second tree. + +**Note** that queries are independent from each other. That is, for every query you will remove the added edge before proceeding to the next query. + +**Example 1:** + +**Input:** edges1 = [[0,1],[0,2],[2,3],[2,4]], edges2 = [[0,1],[0,2],[0,3],[2,7],[1,4],[4,5],[4,6]], k = 2 + +**Output:** [9,7,9,8,8] + +**Explanation:** + +* For `i = 0`, connect node 0 from the first tree to node 0 from the second tree. +* For `i = 1`, connect node 1 from the first tree to node 0 from the second tree. +* For `i = 2`, connect node 2 from the first tree to node 4 from the second tree. +* For `i = 3`, connect node 3 from the first tree to node 4 from the second tree. +* For `i = 4`, connect node 4 from the first tree to node 4 from the second tree. + +![](https://assets.leetcode.com/uploads/2024/09/24/3982-1.png) + +**Example 2:** + +**Input:** edges1 = [[0,1],[0,2],[0,3],[0,4]], edges2 = [[0,1],[1,2],[2,3]], k = 1 + +**Output:** [6,3,3,3,3] + +**Explanation:** + +For every `i`, connect node `i` of the first tree with any node of the second tree. + +![](https://assets.leetcode.com/uploads/2024/09/24/3928-2.png) + +**Constraints:** + +* `2 <= n, m <= 1000` +* `edges1.length == n - 1` +* `edges2.length == m - 1` +* `edges1[i].length == edges2[i].length == 2` +* edges1[i] = [ai, bi] +* 0 <= ai, bi < n +* edges2[i] = [ui, vi] +* 0 <= ui, vi < m +* The input is generated such that `edges1` and `edges2` represent valid trees. +* `0 <= k <= 1000` \ No newline at end of file diff --git a/src/main/kotlin/g3301_3400/s3373_maximize_the_number_of_target_nodes_after_connecting_trees_ii/Solution.kt b/src/main/kotlin/g3301_3400/s3373_maximize_the_number_of_target_nodes_after_connecting_trees_ii/Solution.kt new file mode 100644 index 000000000..431a4c7d7 --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3373_maximize_the_number_of_target_nodes_after_connecting_trees_ii/Solution.kt @@ -0,0 +1,72 @@ +package g3301_3400.s3373_maximize_the_number_of_target_nodes_after_connecting_trees_ii + +// #Hard #Tree #Depth_First_Search #Breadth_First_Search +// #2024_12_03_Time_26_ms_(98.75%)_Space_114.7_MB_(80.00%) + +import kotlin.math.max + +class Solution { + fun maxTargetNodes(edges1: Array, edges2: Array): IntArray { + val n = edges1.size + 1 + val g1 = packU(n, edges1) + val m = edges2.size + 1 + val g2 = packU(m, edges2) + val p2 = parents(g2) + val eo2 = IntArray(2) + for (i in 0..): Array { + val n = g.size + val par = IntArray(n) + par.fill(-1) + val depth = IntArray(n) + depth[0] = 0 + val q = IntArray(n) + q[0] = 0 + var p = 0 + var r = 1 + while (p < r) { + val cur = q[p] + for (nex in g[cur]!!) { + if (par[cur] != nex) { + q[r++] = nex + par[nex] = cur + depth[nex] = depth[cur] + 1 + } + } + p++ + } + return arrayOf(par, q, depth) + } + + private fun packU(n: Int, ft: Array): Array { + val g = arrayOfNulls(n) + val p = IntArray(n) + for (u in ft) { + p[u[0]]++ + p[u[1]]++ + } + for (i in 0..edges1[i] = [ai, bi] indicates that there is an edge between nodes ai and bi in the first tree and edges2[i] = [ui, vi] indicates that there is an edge between nodes ui and vi in the second tree. + +Node `u` is **target** to node `v` if the number of edges on the path from `u` to `v` is even. **Note** that a node is _always_ **target** to itself. + +Return an array of `n` integers `answer`, where `answer[i]` is the **maximum** possible number of nodes that are **target** to node `i` of the first tree if you had to connect one node from the first tree to another node in the second tree. + +**Note** that queries are independent from each other. That is, for every query you will remove the added edge before proceeding to the next query. + +**Example 1:** + +**Input:** edges1 = [[0,1],[0,2],[2,3],[2,4]], edges2 = [[0,1],[0,2],[0,3],[2,7],[1,4],[4,5],[4,6]] + +**Output:** [8,7,7,8,8] + +**Explanation:** + +* For `i = 0`, connect node 0 from the first tree to node 0 from the second tree. +* For `i = 1`, connect node 1 from the first tree to node 4 from the second tree. +* For `i = 2`, connect node 2 from the first tree to node 7 from the second tree. +* For `i = 3`, connect node 3 from the first tree to node 0 from the second tree. +* For `i = 4`, connect node 4 from the first tree to node 4 from the second tree. + +![](https://assets.leetcode.com/uploads/2024/09/24/3982-1.png) + +**Example 2:** + +**Input:** edges1 = [[0,1],[0,2],[0,3],[0,4]], edges2 = [[0,1],[1,2],[2,3]] + +**Output:** [3,6,6,6,6] + +**Explanation:** + +For every `i`, connect node `i` of the first tree with any node of the second tree. + +![](https://assets.leetcode.com/uploads/2024/09/24/3928-2.png) + +**Constraints:** + +* 2 <= n, m <= 105 +* `edges1.length == n - 1` +* `edges2.length == m - 1` +* `edges1[i].length == edges2[i].length == 2` +* edges1[i] = [ai, bi] +* 0 <= ai, bi < n +* edges2[i] = [ui, vi] +* 0 <= ui, vi < m +* The input is generated such that `edges1` and `edges2` represent valid trees. \ No newline at end of file diff --git a/src/test/kotlin/g3301_3400/s3370_smallest_number_with_all_set_bits/SolutionTest.kt b/src/test/kotlin/g3301_3400/s3370_smallest_number_with_all_set_bits/SolutionTest.kt new file mode 100644 index 000000000..136f10459 --- /dev/null +++ b/src/test/kotlin/g3301_3400/s3370_smallest_number_with_all_set_bits/SolutionTest.kt @@ -0,0 +1,22 @@ +package g3301_3400.s3370_smallest_number_with_all_set_bits + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun smallestNumber() { + assertThat(Solution().smallestNumber(5), equalTo(7)) + } + + @Test + fun smallestNumber2() { + assertThat(Solution().smallestNumber(10), equalTo(15)) + } + + @Test + fun smallestNumber3() { + assertThat(Solution().smallestNumber(3), equalTo(3)) + } +} diff --git a/src/test/kotlin/g3301_3400/s3371_identify_the_largest_outlier_in_an_array/SolutionTest.kt b/src/test/kotlin/g3301_3400/s3371_identify_the_largest_outlier_in_an_array/SolutionTest.kt new file mode 100644 index 000000000..cf36644cc --- /dev/null +++ b/src/test/kotlin/g3301_3400/s3371_identify_the_largest_outlier_in_an_array/SolutionTest.kt @@ -0,0 +1,43 @@ +package g3301_3400.s3371_identify_the_largest_outlier_in_an_array + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun largestOutlier() { + assertThat( + Solution() + .getLargestOutlier(intArrayOf(2, 3, 5, 10)), + equalTo(10), + ) + } + + @Test + fun largestOutlier2() { + assertThat( + Solution() + .getLargestOutlier(intArrayOf(-2, -1, -3, -6, 4)), + equalTo(4), + ) + } + + @Test + fun largestOutlier3() { + assertThat( + Solution() + .getLargestOutlier(intArrayOf(1, 1, 1, 1, 1, 5, 5)), + equalTo(5), + ) + } + + @Test + fun largestOutlier4() { + assertThat( + Solution() + .getLargestOutlier(intArrayOf(-108, -108, -517)), + equalTo(-517), + ) + } +} diff --git a/src/test/kotlin/g3301_3400/s3372_maximize_the_number_of_target_nodes_after_connecting_trees_i/SolutionTest.kt b/src/test/kotlin/g3301_3400/s3372_maximize_the_number_of_target_nodes_after_connecting_trees_i/SolutionTest.kt new file mode 100644 index 000000000..c4c87ab0d --- /dev/null +++ b/src/test/kotlin/g3301_3400/s3372_maximize_the_number_of_target_nodes_after_connecting_trees_i/SolutionTest.kt @@ -0,0 +1,41 @@ +package g3301_3400.s3372_maximize_the_number_of_target_nodes_after_connecting_trees_i + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun maxTargetNodes() { + assertThat( + Solution() + .maxTargetNodes( + arrayOf(intArrayOf(0, 1), intArrayOf(0, 2), intArrayOf(2, 3), intArrayOf(2, 4)), + arrayOf( + intArrayOf(0, 1), + intArrayOf(0, 2), + intArrayOf(0, 3), + intArrayOf(2, 7), + intArrayOf(1, 4), + intArrayOf(4, 5), + intArrayOf(4, 6), + ), + 2, + ), + equalTo(intArrayOf(9, 7, 9, 8, 8)), + ) + } + + @Test + fun maxTargetNodes2() { + assertThat( + Solution() + .maxTargetNodes( + arrayOf(intArrayOf(0, 1), intArrayOf(0, 2), intArrayOf(0, 3), intArrayOf(0, 4)), + arrayOf(intArrayOf(0, 1), intArrayOf(1, 2), intArrayOf(2, 3)), + 1, + ), + equalTo(intArrayOf(6, 3, 3, 3, 3)), + ) + } +} diff --git a/src/test/kotlin/g3301_3400/s3373_maximize_the_number_of_target_nodes_after_connecting_trees_ii/SolutionTest.kt b/src/test/kotlin/g3301_3400/s3373_maximize_the_number_of_target_nodes_after_connecting_trees_ii/SolutionTest.kt new file mode 100644 index 000000000..65af994be --- /dev/null +++ b/src/test/kotlin/g3301_3400/s3373_maximize_the_number_of_target_nodes_after_connecting_trees_ii/SolutionTest.kt @@ -0,0 +1,39 @@ +package g3301_3400.s3373_maximize_the_number_of_target_nodes_after_connecting_trees_ii + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun maxTargetNodes() { + assertThat( + Solution() + .maxTargetNodes( + arrayOf(intArrayOf(0, 1), intArrayOf(0, 2), intArrayOf(2, 3), intArrayOf(2, 4)), + arrayOf( + intArrayOf(0, 1), + intArrayOf(0, 2), + intArrayOf(0, 3), + intArrayOf(2, 7), + intArrayOf(1, 4), + intArrayOf(4, 5), + intArrayOf(4, 6), + ), + ), + equalTo(intArrayOf(8, 7, 7, 8, 8)), + ) + } + + @Test + fun maxTargetNodes2() { + assertThat( + Solution() + .maxTargetNodes( + arrayOf(intArrayOf(0, 1), intArrayOf(0, 2), intArrayOf(0, 3), intArrayOf(0, 4)), + arrayOf(intArrayOf(0, 1), intArrayOf(1, 2), intArrayOf(2, 3)), + ), + equalTo(intArrayOf(3, 6, 6, 6, 6)), + ) + } +} From 6fa1a6e0f45df5e250a3e9811764139f32ae520f Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Tue, 3 Dec 2024 03:55:30 +0200 Subject: [PATCH 6/8] Updated tags --- .../kotlin/g3301_3400/s3360_stone_removal_game/Solution.kt | 2 +- .../s3361_shift_distance_between_two_strings/Solution.kt | 2 +- .../g3301_3400/s3362_zero_array_transformation_iii/Solution.kt | 3 ++- .../Solution.kt | 2 +- .../g3301_3400/s3364_minimum_positive_sum_subarray/Solution.kt | 2 +- .../Solution.kt | 2 +- src/main/kotlin/g3301_3400/s3366_minimum_array_sum/Solution.kt | 2 +- .../Solution.kt | 3 ++- .../Solution.kt | 2 +- .../Solution.kt | 2 +- 10 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/main/kotlin/g3301_3400/s3360_stone_removal_game/Solution.kt b/src/main/kotlin/g3301_3400/s3360_stone_removal_game/Solution.kt index b775c1e99..cb4358dd0 100644 --- a/src/main/kotlin/g3301_3400/s3360_stone_removal_game/Solution.kt +++ b/src/main/kotlin/g3301_3400/s3360_stone_removal_game/Solution.kt @@ -1,6 +1,6 @@ package g3301_3400.s3360_stone_removal_game -// #Easy #2024_11_24_Time_0_ms_(100.00%)_Space_33.5_MB_(100.00%) +// #Easy #Math #Simulation #2024_11_24_Time_0_ms_(100.00%)_Space_33.5_MB_(100.00%) class Solution { fun canAliceWin(n: Int): Boolean { diff --git a/src/main/kotlin/g3301_3400/s3361_shift_distance_between_two_strings/Solution.kt b/src/main/kotlin/g3301_3400/s3361_shift_distance_between_two_strings/Solution.kt index 2d20b3d22..4c53501c0 100644 --- a/src/main/kotlin/g3301_3400/s3361_shift_distance_between_two_strings/Solution.kt +++ b/src/main/kotlin/g3301_3400/s3361_shift_distance_between_two_strings/Solution.kt @@ -1,6 +1,6 @@ package g3301_3400.s3361_shift_distance_between_two_strings -// #Medium #2024_11_24_Time_425_ms_(100.00%)_Space_41_MB_(100.00%) +// #Medium #Array #String #Prefix_Sum #2024_11_24_Time_425_ms_(100.00%)_Space_41_MB_(100.00%) import kotlin.math.min diff --git a/src/main/kotlin/g3301_3400/s3362_zero_array_transformation_iii/Solution.kt b/src/main/kotlin/g3301_3400/s3362_zero_array_transformation_iii/Solution.kt index 4e5d07cdf..fcc5887e1 100644 --- a/src/main/kotlin/g3301_3400/s3362_zero_array_transformation_iii/Solution.kt +++ b/src/main/kotlin/g3301_3400/s3362_zero_array_transformation_iii/Solution.kt @@ -1,6 +1,7 @@ package g3301_3400.s3362_zero_array_transformation_iii -// #Medium #2024_11_24_Time_217_ms_(100.00%)_Space_105.7_MB_(100.00%) +// #Medium #Array #Sorting #Greedy #Heap_Priority_Queue #Prefix_Sum +// #2024_11_24_Time_217_ms_(100.00%)_Space_105.7_MB_(100.00%) import java.util.PriorityQueue diff --git a/src/main/kotlin/g3301_3400/s3363_find_the_maximum_number_of_fruits_collected/Solution.kt b/src/main/kotlin/g3301_3400/s3363_find_the_maximum_number_of_fruits_collected/Solution.kt index 6eee12bb8..2970904f0 100644 --- a/src/main/kotlin/g3301_3400/s3363_find_the_maximum_number_of_fruits_collected/Solution.kt +++ b/src/main/kotlin/g3301_3400/s3363_find_the_maximum_number_of_fruits_collected/Solution.kt @@ -1,6 +1,6 @@ package g3301_3400.s3363_find_the_maximum_number_of_fruits_collected -// #Hard #2024_11_24_Time_53_ms_(100.00%)_Space_162_MB_(100.00%) +// #Hard #Array #Dynamic_Programming #Matrix #2024_11_24_Time_53_ms_(100.00%)_Space_162_MB_(100.00%) import kotlin.math.max diff --git a/src/main/kotlin/g3301_3400/s3364_minimum_positive_sum_subarray/Solution.kt b/src/main/kotlin/g3301_3400/s3364_minimum_positive_sum_subarray/Solution.kt index 910013e57..59958b2db 100644 --- a/src/main/kotlin/g3301_3400/s3364_minimum_positive_sum_subarray/Solution.kt +++ b/src/main/kotlin/g3301_3400/s3364_minimum_positive_sum_subarray/Solution.kt @@ -1,6 +1,6 @@ package g3301_3400.s3364_minimum_positive_sum_subarray -// #Easy #2024_11_24_Time_24_ms_(100.00%)_Space_38_MB_(100.00%) +// #Easy #Array #Prefix_Sum #Sliding_Window #2024_11_24_Time_24_ms_(100.00%)_Space_38_MB_(100.00%) class Solution { fun minimumSumSubarray(nums: List, l: Int, r: Int): Int { diff --git a/src/main/kotlin/g3301_3400/s3365_rearrange_k_substrings_to_form_target_string/Solution.kt b/src/main/kotlin/g3301_3400/s3365_rearrange_k_substrings_to_form_target_string/Solution.kt index b23cf3ab4..86099171a 100644 --- a/src/main/kotlin/g3301_3400/s3365_rearrange_k_substrings_to_form_target_string/Solution.kt +++ b/src/main/kotlin/g3301_3400/s3365_rearrange_k_substrings_to_form_target_string/Solution.kt @@ -1,6 +1,6 @@ package g3301_3400.s3365_rearrange_k_substrings_to_form_target_string -// #Medium #2024_11_24_Time_490_ms_(100.00%)_Space_50.5_MB_(100.00%) +// #Medium #String #Hash_Table #Sorting #2024_11_24_Time_490_ms_(100.00%)_Space_50.5_MB_(100.00%) class Solution { fun isPossibleToRearrange(s: String, t: String, k: Int): Boolean { diff --git a/src/main/kotlin/g3301_3400/s3366_minimum_array_sum/Solution.kt b/src/main/kotlin/g3301_3400/s3366_minimum_array_sum/Solution.kt index 97732ab2d..3135d5149 100644 --- a/src/main/kotlin/g3301_3400/s3366_minimum_array_sum/Solution.kt +++ b/src/main/kotlin/g3301_3400/s3366_minimum_array_sum/Solution.kt @@ -1,6 +1,6 @@ package g3301_3400.s3366_minimum_array_sum -// #Medium #2024_11_24_Time_59_ms_(100.00%)_Space_53.4_MB_(100.00%) +// #Medium #Array #Dynamic_Programming #2024_11_24_Time_59_ms_(100.00%)_Space_53.4_MB_(100.00%) import kotlin.math.ceil import kotlin.math.min diff --git a/src/main/kotlin/g3301_3400/s3367_maximize_sum_of_weights_after_edge_removals/Solution.kt b/src/main/kotlin/g3301_3400/s3367_maximize_sum_of_weights_after_edge_removals/Solution.kt index 216aaa10b..b815a1fea 100644 --- a/src/main/kotlin/g3301_3400/s3367_maximize_sum_of_weights_after_edge_removals/Solution.kt +++ b/src/main/kotlin/g3301_3400/s3367_maximize_sum_of_weights_after_edge_removals/Solution.kt @@ -1,6 +1,7 @@ package g3301_3400.s3367_maximize_sum_of_weights_after_edge_removals -// #Hard #2024_11_27_Time_118_ms_(100.00%)_Space_140.5_MB_(100.00%) +// #Hard #Dynamic_Programming #Depth_First_Search #Tree +// #2024_11_27_Time_118_ms_(100.00%)_Space_140.5_MB_(100.00%) import java.util.PriorityQueue import kotlin.math.max diff --git a/src/main/kotlin/g3301_3400/s3372_maximize_the_number_of_target_nodes_after_connecting_trees_i/Solution.kt b/src/main/kotlin/g3301_3400/s3372_maximize_the_number_of_target_nodes_after_connecting_trees_i/Solution.kt index b5d7e8d25..a5f627c96 100644 --- a/src/main/kotlin/g3301_3400/s3372_maximize_the_number_of_target_nodes_after_connecting_trees_i/Solution.kt +++ b/src/main/kotlin/g3301_3400/s3372_maximize_the_number_of_target_nodes_after_connecting_trees_i/Solution.kt @@ -1,6 +1,6 @@ package g3301_3400.s3372_maximize_the_number_of_target_nodes_after_connecting_trees_i -// #Medium #Tree #Depth_First_Search #Breadth_First_Search +// #Medium #Depth_First_Search #Breadth_First_Search #Tree // #2024_12_03_Time_50_ms_(99.49%)_Space_75.7_MB_(5.10%) import kotlin.math.max diff --git a/src/main/kotlin/g3301_3400/s3373_maximize_the_number_of_target_nodes_after_connecting_trees_ii/Solution.kt b/src/main/kotlin/g3301_3400/s3373_maximize_the_number_of_target_nodes_after_connecting_trees_ii/Solution.kt index 431a4c7d7..42494a4d7 100644 --- a/src/main/kotlin/g3301_3400/s3373_maximize_the_number_of_target_nodes_after_connecting_trees_ii/Solution.kt +++ b/src/main/kotlin/g3301_3400/s3373_maximize_the_number_of_target_nodes_after_connecting_trees_ii/Solution.kt @@ -1,6 +1,6 @@ package g3301_3400.s3373_maximize_the_number_of_target_nodes_after_connecting_trees_ii -// #Hard #Tree #Depth_First_Search #Breadth_First_Search +// #Hard #Depth_First_Search #Breadth_First_Search #Tree // #2024_12_03_Time_26_ms_(98.75%)_Space_114.7_MB_(80.00%) import kotlin.math.max From 6ff72325f3fab7f988220bb1664b667511ac46fa Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Tue, 3 Dec 2024 04:26:07 +0200 Subject: [PATCH 7/8] Improved tasks 3360-3367 --- .../s3360_stone_removal_game/Solution.kt | 2 +- .../Solution.kt | 46 +++++---- .../Solution.kt | 4 +- .../Solution.kt | 3 +- .../Solution.kt | 33 ++++--- .../Solution.kt | 2 +- .../s3366_minimum_array_sum/Solution.kt | 95 +++++++++++++------ .../Solution.kt | 4 +- .../SolutionTest.kt | 2 +- .../s3366_minimum_array_sum/SolutionTest.kt | 16 ++++ .../SolutionTest.kt | 13 ++- 11 files changed, 154 insertions(+), 66 deletions(-) diff --git a/src/main/kotlin/g3301_3400/s3360_stone_removal_game/Solution.kt b/src/main/kotlin/g3301_3400/s3360_stone_removal_game/Solution.kt index cb4358dd0..03d32e9a3 100644 --- a/src/main/kotlin/g3301_3400/s3360_stone_removal_game/Solution.kt +++ b/src/main/kotlin/g3301_3400/s3360_stone_removal_game/Solution.kt @@ -1,6 +1,6 @@ package g3301_3400.s3360_stone_removal_game -// #Easy #Math #Simulation #2024_11_24_Time_0_ms_(100.00%)_Space_33.5_MB_(100.00%) +// #Easy #Math #Simulation #2024_12_03_Time_0_ms_(100.00%)_Space_34.3_MB_(6.00%) class Solution { fun canAliceWin(n: Int): Boolean { diff --git a/src/main/kotlin/g3301_3400/s3361_shift_distance_between_two_strings/Solution.kt b/src/main/kotlin/g3301_3400/s3361_shift_distance_between_two_strings/Solution.kt index 4c53501c0..171fc37ae 100644 --- a/src/main/kotlin/g3301_3400/s3361_shift_distance_between_two_strings/Solution.kt +++ b/src/main/kotlin/g3301_3400/s3361_shift_distance_between_two_strings/Solution.kt @@ -1,28 +1,42 @@ package g3301_3400.s3361_shift_distance_between_two_strings -// #Medium #Array #String #Prefix_Sum #2024_11_24_Time_425_ms_(100.00%)_Space_41_MB_(100.00%) +// #Medium #Array #String #Prefix_Sum #2024_12_03_Time_350_ms_(82.50%)_Space_41.7_MB_(57.50%) import kotlin.math.min class Solution { fun shiftDistance(s: String, t: String, nextCost: IntArray, previousCost: IntArray): Long { - var sum: Long = 0 - val n = s.length - for (i in 0..(26) { LongArray(26) } + var cost: Long + for (i in 0..25) { + cost = nextCost[i].toLong() + var j = if (i == 25) 0 else i + 1 + while (j != i) { + costs[i]!![j] = cost + cost += nextCost[j].toLong() + if (j == 25) { + j = -1 + } + j++ } - val reverseDiff = (26 + ch1 - ch2) % 26 - var backwardCost: Long = 0 - for (j in 0..): Int { - queries.sortWith { a: IntArray, b: IntArray -> a[0] - b[0] } + queries.sortWith { a: IntArray?, b: IntArray? -> a!![0] - b!![0] } val last = PriorityQueue(Comparator { a: Int?, b: Int? -> b!! - a!! }) val diffs = IntArray(nums.size + 1) var idx = 0 diff --git a/src/main/kotlin/g3301_3400/s3363_find_the_maximum_number_of_fruits_collected/Solution.kt b/src/main/kotlin/g3301_3400/s3363_find_the_maximum_number_of_fruits_collected/Solution.kt index 2970904f0..c843e3896 100644 --- a/src/main/kotlin/g3301_3400/s3363_find_the_maximum_number_of_fruits_collected/Solution.kt +++ b/src/main/kotlin/g3301_3400/s3363_find_the_maximum_number_of_fruits_collected/Solution.kt @@ -1,6 +1,7 @@ package g3301_3400.s3363_find_the_maximum_number_of_fruits_collected -// #Hard #Array #Dynamic_Programming #Matrix #2024_11_24_Time_53_ms_(100.00%)_Space_162_MB_(100.00%) +// #Hard #Array #Dynamic_Programming #Matrix +// #2024_12_03_Time_39_ms_(88.89%)_Space_161.2_MB_(100.00%) import kotlin.math.max diff --git a/src/main/kotlin/g3301_3400/s3364_minimum_positive_sum_subarray/Solution.kt b/src/main/kotlin/g3301_3400/s3364_minimum_positive_sum_subarray/Solution.kt index 59958b2db..b0dc06cbe 100644 --- a/src/main/kotlin/g3301_3400/s3364_minimum_positive_sum_subarray/Solution.kt +++ b/src/main/kotlin/g3301_3400/s3364_minimum_positive_sum_subarray/Solution.kt @@ -1,22 +1,29 @@ package g3301_3400.s3364_minimum_positive_sum_subarray -// #Easy #Array #Prefix_Sum #Sliding_Window #2024_11_24_Time_24_ms_(100.00%)_Space_38_MB_(100.00%) +// #Easy #Array #Prefix_Sum #Sliding_Window #2024_12_03_Time_3_ms_(98.15%)_Space_38.1_MB_(33.33%) + +import kotlin.math.min class Solution { - fun minimumSumSubarray(nums: List, l: Int, r: Int): Int { - val size = nums.size - var res = -1 - for (s in l..r) { - for (i in 0..size - s) { - var sum = 0 - for (j in i.. 0 && (res == -1 || res > sum)) { - res = sum + fun minimumSumSubarray(li: List, l: Int, r: Int): Int { + val n = li.size + var min = Int.Companion.MAX_VALUE + val a = IntArray(n + 1) + for (i in 1..n) { + a[i] = a[i - 1] + li[i - 1] + } + for (size in l..r) { + for (i in size - 1.. 0) { + min = min(min, sum) } } } - return res + return if (min == Int.Companion.MAX_VALUE) { + -1 + } else { + min + } } } diff --git a/src/main/kotlin/g3301_3400/s3365_rearrange_k_substrings_to_form_target_string/Solution.kt b/src/main/kotlin/g3301_3400/s3365_rearrange_k_substrings_to_form_target_string/Solution.kt index 86099171a..7d30add83 100644 --- a/src/main/kotlin/g3301_3400/s3365_rearrange_k_substrings_to_form_target_string/Solution.kt +++ b/src/main/kotlin/g3301_3400/s3365_rearrange_k_substrings_to_form_target_string/Solution.kt @@ -1,6 +1,6 @@ package g3301_3400.s3365_rearrange_k_substrings_to_form_target_string -// #Medium #String #Hash_Table #Sorting #2024_11_24_Time_490_ms_(100.00%)_Space_50.5_MB_(100.00%) +// #Medium #String #Hash_Table #Sorting #2024_12_03_Time_457_ms_(100.00%)_Space_51.5_MB_(81.40%) class Solution { fun isPossibleToRearrange(s: String, t: String, k: Int): Boolean { diff --git a/src/main/kotlin/g3301_3400/s3366_minimum_array_sum/Solution.kt b/src/main/kotlin/g3301_3400/s3366_minimum_array_sum/Solution.kt index 3135d5149..6ce986d69 100644 --- a/src/main/kotlin/g3301_3400/s3366_minimum_array_sum/Solution.kt +++ b/src/main/kotlin/g3301_3400/s3366_minimum_array_sum/Solution.kt @@ -1,41 +1,80 @@ package g3301_3400.s3366_minimum_array_sum -// #Medium #Array #Dynamic_Programming #2024_11_24_Time_59_ms_(100.00%)_Space_53.4_MB_(100.00%) - -import kotlin.math.ceil -import kotlin.math.min +// #Medium #Array #Dynamic_Programming #2024_12_03_Time_15_ms_(100.00%)_Space_39.5_MB_(92.86%) class Solution { fun minArraySum(nums: IntArray, k: Int, op1: Int, op2: Int): Int { - val dp = Array?>?>(nums.size) { Array?>(op1 + 1) { arrayOfNulls(op2 + 1) } } - return sub(dp, nums, 0, k, op1, op2) - } - - private fun sub(dp: Array?>?>, nums: IntArray, i: Int, k: Int, op1: Int, op2: Int): Int { - if (i == nums.size) { - return 0 - } - if (dp[i]!![op1]!![op2] != null) { - return dp[i]!![op1]!![op2]!! + var op1 = op1 + var op2 = op2 + nums.sort() + val high = lowerBound(nums, k * 2 - 1) + val low = lowerBound(nums, k) + val n = nums.size + for (i in n - 1 downTo high) { + if (op1 > 0) { + nums[i] = (nums[i] + 1) / 2 + op1-- + } + if (op2 > 0) { + nums[i] -= k + op2-- + } } - var res = sub(dp, nums, i + 1, k, op1, op2) + nums[i] - if (nums[i] >= k && op2 > 0) { - res = min(res, (sub(dp, nums, i + 1, k, op1, op2 - 1) + nums[i] - k)) - var v = ceil(nums[i] / 2.0) - if (v < k) { - v = ceil((nums[i] - k) / 2.0) + val count: MutableMap = HashMap() + var odd = 0 + for (i in low.. 0) { + nums[i] -= k + if (k % 2 > 0 && nums[i] % 2 > 0) { + count.merge(nums[i], 1) { a: Int?, b: Int? -> Integer.sum(a!!, b!!) } + } + op2-- } else { - v -= k + odd += nums[i] % 2 } - if (op1 > 0) { - res = min(res, (sub(dp, nums, i + 1, k, op1 - 1, op2 - 1) + v).toInt()) + } + nums.sort(0, high) + var ans = 0 + if (k % 2 > 0) { + var i = high - op1 + while (i < high && odd > 0) { + val x = nums[i] + if (count.containsKey(x)) { + if (count.merge(x, -1) { a: Int?, b: Int? -> + Integer.sum(a!!, b!!) + } == 0 + ) { + count.remove(x) + } + odd-- + ans-- + } + i++ } } - if (op1 > 0) { - val v = ceil(nums[i] / 2.0) - res = min(res, (sub(dp, nums, i + 1, k, op1 - 1, op2) + v).toInt()) + var i = high - 1 + while (i >= 0 && op1 > 0) { + nums[i] = (nums[i] + 1) / 2 + i-- + op1-- + } + for (x in nums) { + ans += x + } + return ans + } + + private fun lowerBound(nums: IntArray, target: Int): Int { + var left = -1 + var right = nums.size + while (left + 1 < right) { + val mid = (left + right) ushr 1 + if (nums[mid] >= target) { + right = mid + } else { + left = mid + } } - dp[i]!![op1]!![op2] = res - return res + return right } } diff --git a/src/main/kotlin/g3301_3400/s3367_maximize_sum_of_weights_after_edge_removals/Solution.kt b/src/main/kotlin/g3301_3400/s3367_maximize_sum_of_weights_after_edge_removals/Solution.kt index b815a1fea..8beb4e525 100644 --- a/src/main/kotlin/g3301_3400/s3367_maximize_sum_of_weights_after_edge_removals/Solution.kt +++ b/src/main/kotlin/g3301_3400/s3367_maximize_sum_of_weights_after_edge_removals/Solution.kt @@ -1,7 +1,7 @@ package g3301_3400.s3367_maximize_sum_of_weights_after_edge_removals // #Hard #Dynamic_Programming #Depth_First_Search #Tree -// #2024_11_27_Time_118_ms_(100.00%)_Space_140.5_MB_(100.00%) +// #2024_12_03_Time_113_ms_(100.00%)_Space_141.5_MB_(81.82%) import java.util.PriorityQueue import kotlin.math.max @@ -33,7 +33,7 @@ class Solution { continue } val res = dfs(w, v) - val max = max((e[2] + res[0]), res[1]) + val max = max(e[2] + res[0], res[1]) sum += max pq.add(max - res[1]) } diff --git a/src/test/kotlin/g3301_3400/s3364_minimum_positive_sum_subarray/SolutionTest.kt b/src/test/kotlin/g3301_3400/s3364_minimum_positive_sum_subarray/SolutionTest.kt index 3157d79e2..c6110c125 100644 --- a/src/test/kotlin/g3301_3400/s3364_minimum_positive_sum_subarray/SolutionTest.kt +++ b/src/test/kotlin/g3301_3400/s3364_minimum_positive_sum_subarray/SolutionTest.kt @@ -24,7 +24,7 @@ internal class SolutionTest { @Test fun minimumSumSubarray3() { assertThat( - Solution().minimumSumSubarray(mutableListOf(1, 2, 3, 4), 2, 4), + Solution().minimumSumSubarray(listOf(1, 2, 3, 4), 2, 4), equalTo(3), ) } diff --git a/src/test/kotlin/g3301_3400/s3366_minimum_array_sum/SolutionTest.kt b/src/test/kotlin/g3301_3400/s3366_minimum_array_sum/SolutionTest.kt index 080f58ef3..d2d8a5c78 100644 --- a/src/test/kotlin/g3301_3400/s3366_minimum_array_sum/SolutionTest.kt +++ b/src/test/kotlin/g3301_3400/s3366_minimum_array_sum/SolutionTest.kt @@ -20,4 +20,20 @@ internal class SolutionTest { equalTo(3), ) } + + @Test + fun minArraySum3() { + assertThat( + Solution() + .minArraySum( + intArrayOf( + 1, 3, 5, 7, 9, 12, 12, 12, 13, 15, 15, 15, 16, 17, 19, 20, + ), + 11, + 15, + 4, + ), + equalTo(77), + ) + } } diff --git a/src/test/kotlin/g3301_3400/s3367_maximize_sum_of_weights_after_edge_removals/SolutionTest.kt b/src/test/kotlin/g3301_3400/s3367_maximize_sum_of_weights_after_edge_removals/SolutionTest.kt index 189a8ed9e..afda61f02 100644 --- a/src/test/kotlin/g3301_3400/s3367_maximize_sum_of_weights_after_edge_removals/SolutionTest.kt +++ b/src/test/kotlin/g3301_3400/s3367_maximize_sum_of_weights_after_edge_removals/SolutionTest.kt @@ -37,7 +37,18 @@ internal class SolutionTest { ), 3, ), - equalTo(65L), + equalTo(65L), + ) + } + + @Test + fun maximizeSumOfWeights3() { + assertThat( + Solution().maximizeSumOfWeights( + arrayOf(intArrayOf(0, 1, 34), intArrayOf(0, 2, 17)), + 1, + ), + equalTo(34L), ) } } From 339942c1a80b3f26812b108159429b276f4c3b47 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Tue, 3 Dec 2024 04:29:06 +0200 Subject: [PATCH 8/8] Improved task 3362 --- .../g3301_3400/s3362_zero_array_transformation_iii/Solution.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/kotlin/g3301_3400/s3362_zero_array_transformation_iii/Solution.kt b/src/main/kotlin/g3301_3400/s3362_zero_array_transformation_iii/Solution.kt index fb6bd74f8..ebd432a9e 100644 --- a/src/main/kotlin/g3301_3400/s3362_zero_array_transformation_iii/Solution.kt +++ b/src/main/kotlin/g3301_3400/s3362_zero_array_transformation_iii/Solution.kt @@ -7,7 +7,7 @@ import java.util.PriorityQueue class Solution { fun maxRemoval(nums: IntArray, queries: Array): Int { - queries.sortWith { a: IntArray?, b: IntArray? -> a!![0] - b!![0] } + queries.sortWith { a: IntArray, b: IntArray -> a[0] - b[0] } val last = PriorityQueue(Comparator { a: Int?, b: Int? -> b!! - a!! }) val diffs = IntArray(nums.size + 1) var idx = 0