From 48ab9f4afdaacbb7c0352e03495485c08365704a Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Tue, 28 Oct 2025 11:50:19 +0200 Subject: [PATCH 1/2] Added tasks 3722-3729 --- .../Solution.kt | 70 ++++++++++++++++++ .../readme.md | 52 ++++++++++++++ .../Solution.kt | 27 +++++++ .../readme.md | 60 ++++++++++++++++ .../Solution.kt | 32 +++++++++ .../readme.md | 71 +++++++++++++++++++ .../Solution.kt | 54 ++++++++++++++ .../readme.md | 42 +++++++++++ .../Solution.kt | 16 +++++ .../readme.md | 31 ++++++++ .../Solution.kt | 32 +++++++++ .../readme.md | 44 ++++++++++++ .../Solution.kt | 28 ++++++++ .../readme.md | 48 +++++++++++++ .../Solution.kt | 33 +++++++++ .../readme.md | 40 +++++++++++ .../SolutionTest.kt | 22 ++++++ .../SolutionTest.kt | 70 ++++++++++++++++++ .../SolutionTest.kt | 31 ++++++++ .../SolutionTest.kt | 23 ++++++ .../SolutionTest.kt | 17 +++++ .../SolutionTest.kt | 23 ++++++ .../SolutionTest.kt | 31 ++++++++ .../SolutionTest.kt | 23 ++++++ 24 files changed, 920 insertions(+) create mode 100644 src/main/kotlin/g3701_3800/s3722_lexicographically_smallest_string_after_reverse/Solution.kt create mode 100644 src/main/kotlin/g3701_3800/s3722_lexicographically_smallest_string_after_reverse/readme.md create mode 100644 src/main/kotlin/g3701_3800/s3723_maximize_sum_of_squares_of_digits/Solution.kt create mode 100644 src/main/kotlin/g3701_3800/s3723_maximize_sum_of_squares_of_digits/readme.md create mode 100644 src/main/kotlin/g3701_3800/s3724_minimum_operations_to_transform_array/Solution.kt create mode 100644 src/main/kotlin/g3701_3800/s3724_minimum_operations_to_transform_array/readme.md create mode 100644 src/main/kotlin/g3701_3800/s3725_count_ways_to_choose_coprime_integers_from_rows/Solution.kt create mode 100644 src/main/kotlin/g3701_3800/s3725_count_ways_to_choose_coprime_integers_from_rows/readme.md create mode 100644 src/main/kotlin/g3701_3800/s3726_remove_zeros_in_decimal_representation/Solution.kt create mode 100644 src/main/kotlin/g3701_3800/s3726_remove_zeros_in_decimal_representation/readme.md create mode 100644 src/main/kotlin/g3701_3800/s3727_maximum_alternating_sum_of_squares/Solution.kt create mode 100644 src/main/kotlin/g3701_3800/s3727_maximum_alternating_sum_of_squares/readme.md create mode 100644 src/main/kotlin/g3701_3800/s3728_stable_subarrays_with_equal_boundary_and_interior_sum/Solution.kt create mode 100644 src/main/kotlin/g3701_3800/s3728_stable_subarrays_with_equal_boundary_and_interior_sum/readme.md create mode 100644 src/main/kotlin/g3701_3800/s3729_count_distinct_subarrays_divisible_by_k_in_sorted_array/Solution.kt create mode 100644 src/main/kotlin/g3701_3800/s3729_count_distinct_subarrays_divisible_by_k_in_sorted_array/readme.md create mode 100644 src/test/kotlin/g3701_3800/s3722_lexicographically_smallest_string_after_reverse/SolutionTest.kt create mode 100644 src/test/kotlin/g3701_3800/s3723_maximize_sum_of_squares_of_digits/SolutionTest.kt create mode 100644 src/test/kotlin/g3701_3800/s3724_minimum_operations_to_transform_array/SolutionTest.kt create mode 100644 src/test/kotlin/g3701_3800/s3725_count_ways_to_choose_coprime_integers_from_rows/SolutionTest.kt create mode 100644 src/test/kotlin/g3701_3800/s3726_remove_zeros_in_decimal_representation/SolutionTest.kt create mode 100644 src/test/kotlin/g3701_3800/s3727_maximum_alternating_sum_of_squares/SolutionTest.kt create mode 100644 src/test/kotlin/g3701_3800/s3728_stable_subarrays_with_equal_boundary_and_interior_sum/SolutionTest.kt create mode 100644 src/test/kotlin/g3701_3800/s3729_count_distinct_subarrays_divisible_by_k_in_sorted_array/SolutionTest.kt diff --git a/src/main/kotlin/g3701_3800/s3722_lexicographically_smallest_string_after_reverse/Solution.kt b/src/main/kotlin/g3701_3800/s3722_lexicographically_smallest_string_after_reverse/Solution.kt new file mode 100644 index 00000000..314db8f7 --- /dev/null +++ b/src/main/kotlin/g3701_3800/s3722_lexicographically_smallest_string_after_reverse/Solution.kt @@ -0,0 +1,70 @@ +package g3701_3800.s3722_lexicographically_smallest_string_after_reverse + +// #Medium #Biweekly_Contest_168 #2025_10_28_Time_8_ms_(100.00%)_Space_45.74_MB_(100.00%) + +class Solution { + fun lexSmallest(s: String): String { + val n = s.length + val arr = s.toCharArray() + val best = arr.clone() + // Check all reverse first k operations + for (k in 1..n) { + if (isBetterReverseFirstK(arr, k, best)) { + updateBestReverseFirstK(arr, k, best) + } + } + // Check all reverse last k operations + for (k in 1..n) { + if (isBetterReverseLastK(arr, k, best)) { + updateBestReverseLastK(arr, k, best) + } + } + return String(best) + } + + private fun isBetterReverseFirstK(arr: CharArray, k: Int, best: CharArray): Boolean { + for (i in arr.indices) { + val currentChar = if (i < k) arr[k - 1 - i] else arr[i] + if (currentChar < best[i]) { + return true + } + if (currentChar > best[i]) { + return false + } + } + return false + } + + private fun isBetterReverseLastK(arr: CharArray, k: Int, best: CharArray): Boolean { + val n = arr.size + for (i in 0.. best[i]) { + return false + } + } + return false + } + + private fun updateBestReverseFirstK(arr: CharArray, k: Int, best: CharArray) { + for (i in 0..= 0) { + System.arraycopy(arr, k, best, k, arr.size - k) + } + } + + private fun updateBestReverseLastK(arr: CharArray, k: Int, best: CharArray) { + val n = arr.size + if (n - k >= 0) { + System.arraycopy(arr, 0, best, 0, n - k) + } + for (i in 0.. 0) { + return ans + } + ans = "9".repeat(nines) + } else { + val remSum = sum - nines * 9 + ans = "9".repeat(nines) + remSum + val extra = places - ans.length + if (extra > 0) { + ans = ans + ("0".repeat(extra)) + } + } + return ans + } +} diff --git a/src/main/kotlin/g3701_3800/s3723_maximize_sum_of_squares_of_digits/readme.md b/src/main/kotlin/g3701_3800/s3723_maximize_sum_of_squares_of_digits/readme.md new file mode 100644 index 00000000..8355d7c6 --- /dev/null +++ b/src/main/kotlin/g3701_3800/s3723_maximize_sum_of_squares_of_digits/readme.md @@ -0,0 +1,60 @@ +3723\. Maximize Sum of Squares of Digits + +Medium + +You are given two **positive** integers `num` and `sum`. + +A positive integer `n` is **good** if it satisfies both of the following: + +* The number of digits in `n` is **exactly** `num`. +* The sum of digits in `n` is **exactly** `sum`. + +The **score** of a **good** integer `n` is the sum of the squares of digits in `n`. + +Return a **string** denoting the **good** integer `n` that achieves the **maximum** **score**. If there are multiple possible integers, return the **maximum** one. If no such integer exists, return an empty string. + +**Example 1:** + +**Input:** num = 2, sum = 3 + +**Output:** "30" + +**Explanation:** + +There are 3 good integers: 12, 21, and 30. + +* The score of 12 is 12 + 22 = 5. +* The score of 21 is 22 + 12 = 5. +* The score of 30 is 32 + 02 = 9. + +The maximum score is 9, which is achieved by the good integer 30. Therefore, the answer is `"30"`. + +**Example 2:** + +**Input:** num = 2, sum = 17 + +**Output:** "98" + +**Explanation:** + +There are 2 good integers: 89 and 98. + +* The score of 89 is 82 + 92 = 145. +* The score of 98 is 92 + 82 = 145. + +The maximum score is 145. The maximum good integer that achieves this score is 98. Therefore, the answer is `"98"`. + +**Example 3:** + +**Input:** num = 1, sum = 10 + +**Output:** "" + +**Explanation:** + +There are no integers that have exactly 1 digit and whose digits sum to 10. Therefore, the answer is `""`. + +**Constraints:** + +* 1 <= num <= 2 * 105 +* 1 <= sum <= 2 * 106 \ No newline at end of file diff --git a/src/main/kotlin/g3701_3800/s3724_minimum_operations_to_transform_array/Solution.kt b/src/main/kotlin/g3701_3800/s3724_minimum_operations_to_transform_array/Solution.kt new file mode 100644 index 00000000..8ab3c205 --- /dev/null +++ b/src/main/kotlin/g3701_3800/s3724_minimum_operations_to_transform_array/Solution.kt @@ -0,0 +1,32 @@ +package g3701_3800.s3724_minimum_operations_to_transform_array + +// #Medium #Biweekly_Contest_168 #2025_10_28_Time_10_ms_(83.33%)_Space_66.99_MB_(100.00%) + +import kotlin.math.abs +import kotlin.math.max +import kotlin.math.min + +class Solution { + fun minOperations(nums1: IntArray, nums2: IntArray): Long { + val n = nums1.size + val last = nums2[n] + var steps: Long = 1 + var minDiffFromLast = Long.MAX_VALUE + for (i in 0.. 0) { + if (min <= last && last <= max) { + minDiffFromLast = 0 + } else { + minDiffFromLast = min( + minDiffFromLast, + min(abs(min - last), abs(max - last)).toLong(), + ) + } + } + } + return steps + minDiffFromLast + } +} diff --git a/src/main/kotlin/g3701_3800/s3724_minimum_operations_to_transform_array/readme.md b/src/main/kotlin/g3701_3800/s3724_minimum_operations_to_transform_array/readme.md new file mode 100644 index 00000000..3aa5f51b --- /dev/null +++ b/src/main/kotlin/g3701_3800/s3724_minimum_operations_to_transform_array/readme.md @@ -0,0 +1,71 @@ +3724\. Minimum Operations to Transform Array + +Medium + +You are given two integer arrays `nums1` of length `n` and `nums2` of length `n + 1`. + +You want to transform `nums1` into `nums2` using the **minimum** number of operations. + +You may perform the following operations **any** number of times, each time choosing an index `i`: + +* **Increase** `nums1[i]` by 1. +* **Decrease** `nums1[i]` by 1. +* **Append** `nums1[i]` to the **end** of the array. + +Return the **minimum** number of operations required to transform `nums1` into `nums2`. + +**Example 1:** + +**Input:** nums1 = [2,8], nums2 = [1,7,3] + +**Output:** 4 + +**Explanation:** + +| Step | `i` | Operation | `nums1[i]` | Updated `nums1` | +|------|------|------------|-------------|----------------| +| 1 | 0 | Append | - | [2, 8, 2] | +| 2 | 0 | Decrement | Decreases to 1 | [1, 8, 2] | +| 3 | 1 | Decrement | Decreases to 7 | [1, 7, 2] | +| 4 | 2 | Increment | Increases to 3 | [1, 7, 3] | + +Thus, after 4 operations `nums1` is transformed into `nums2`. + +**Example 2:** + +**Input:** nums1 = [1,3,6], nums2 = [2,4,5,3] + +**Output:** 4 + +**Explanation:** + +| Step | `i` | Operation | `nums1[i]` | Updated `nums1` | +|------|------|------------|-------------|----------------| +| 1 | 1 | Append | - | [1, 3, 6, 3] | +| 2 | 0 | Increment | Increases to 2 | [2, 3, 6, 3] | +| 3 | 1 | Increment | Increases to 4 | [2, 4, 6, 3] | +| 4 | 2 | Decrement | Decreases to 5 | [2, 4, 5, 3] | + +Thus, after 4 operations `nums1` is transformed into `nums2`. + +**Example 3:** + +**Input:** nums1 = [2], nums2 = [3,4] + +**Output:** 3 + +**Explanation:** + +| Step | `i` | Operation | `nums1[i]` | Updated `nums1` | +|------|------|------------|-------------|----------------| +| 1 | 0 | Increment | Increases to 3 | [3] | +| 2 | 0 | Append | - | [3, 3] | +| 3 | 1 | Increment | Increases to 4 | [3, 4] | + +Thus, after 3 operations `nums1` is transformed into `nums2`. + +**Constraints:** + +* 1 <= n == nums1.length <= 105 +* `nums2.length == n + 1` +* 1 <= nums1[i], nums2[i] <= 105 \ No newline at end of file diff --git a/src/main/kotlin/g3701_3800/s3725_count_ways_to_choose_coprime_integers_from_rows/Solution.kt b/src/main/kotlin/g3701_3800/s3725_count_ways_to_choose_coprime_integers_from_rows/Solution.kt new file mode 100644 index 00000000..1a6963d6 --- /dev/null +++ b/src/main/kotlin/g3701_3800/s3725_count_ways_to_choose_coprime_integers_from_rows/Solution.kt @@ -0,0 +1,54 @@ +package g3701_3800.s3725_count_ways_to_choose_coprime_integers_from_rows + +// #Hard #Biweekly_Contest_168 #2025_10_28_Time_29_ms_(100.00%)_Space_51.80_MB_(100.00%) + +import kotlin.math.max + +class Solution { + fun countCoprime(mat: Array): Int { + val m = mat.size + val n = mat[0].size + var maxVal = 0 + for (ints in mat) { + for (j in 0.. = HashMap() + for (g in maxVal downTo 1) { + var ways = countWaysWithDivisor(mat, g, m, n) + if (ways > 0) { + var multiple = 2 * g + while (multiple <= maxVal) { + if (gcdWays.containsKey(multiple)) { + ways = (ways - gcdWays[multiple]!! + MOD) % MOD + } + multiple += g + } + gcdWays[g] = ways + } + } + return gcdWays.getOrDefault(1, 0L).toInt() + } + + private fun countWaysWithDivisor(matrix: Array, divisor: Int, rows: Int, cols: Int): Long { + var totalWays: Long = 1 + for (row in 0..109 + 7. + +**Example 1:** + +**Input:** mat = [[1,2],[3,4]] + +**Output:** 3 + +**Explanation:** + +| Chosen integer in the first row | Chosen integer in the second row | Greatest common divisor of chosen integers | +|---------------------------------|----------------------------------|--------------------------------------------| +| 1 | 3 | 1 | +| 1 | 4 | 1 | +| 2 | 3 | 1 | +| 2 | 4 | 2 | + +3 of these combinations have a greatest common divisor of 1. Therefore, the answer is 3. + +**Example 2:** + +**Input:** mat = [[2,2],[2,2]] + +**Output:** 0 + +**Explanation:** + +Every combination has a greatest common divisor of 2. Therefore, the answer is 0. + +**Constraints:** + +* `1 <= m == mat.length <= 150` +* `1 <= n == mat[i].length <= 150` +* `1 <= mat[i][j] <= 150` \ No newline at end of file diff --git a/src/main/kotlin/g3701_3800/s3726_remove_zeros_in_decimal_representation/Solution.kt b/src/main/kotlin/g3701_3800/s3726_remove_zeros_in_decimal_representation/Solution.kt new file mode 100644 index 00000000..4fc991a8 --- /dev/null +++ b/src/main/kotlin/g3701_3800/s3726_remove_zeros_in_decimal_representation/Solution.kt @@ -0,0 +1,16 @@ +package g3701_3800.s3726_remove_zeros_in_decimal_representation + +// #Easy #Weekly_Contest_473 #2025_10_28_Time_2_ms_(73.91%)_Space_41.33_MB_(78.26%) + +class Solution { + fun removeZeros(n: Long): Long { + val x = StringBuilder() + val s = n.toString() + for (i in 0..0**2**00**3**0**, we get 123. + +**Example 2:** + +**Input:** n = 1 + +**Output:** 1 + +**Explanation:** + +1 has no zero in its decimal representation. Therefore, the answer is 1. + +**Constraints:** + +* 1 <= n <= 1015 \ No newline at end of file diff --git a/src/main/kotlin/g3701_3800/s3727_maximum_alternating_sum_of_squares/Solution.kt b/src/main/kotlin/g3701_3800/s3727_maximum_alternating_sum_of_squares/Solution.kt new file mode 100644 index 00000000..714198ab --- /dev/null +++ b/src/main/kotlin/g3701_3800/s3727_maximum_alternating_sum_of_squares/Solution.kt @@ -0,0 +1,32 @@ +package g3701_3800.s3727_maximum_alternating_sum_of_squares + +// #Medium #Weekly_Contest_473 #2025_10_28_Time_9_ms_(100.00%)_Space_90.79_MB_(13.04%) + +import kotlin.math.abs +import kotlin.math.min + +class Solution { + fun maxAlternatingSum(nums: IntArray): Long { + val n = nums.size + var need = n / 2 + val maxa = 40000 + val freq = IntArray(maxa + 1) + var total: Long = 0 + for (x in nums) { + val a = abs(x) + freq[a]++ + total += 1L * a * a + } + var small: Long = 0 + var a = 0 + while (a <= maxa && need > 0) { + val take = min(freq[a], need) + if (take > 0) { + small += 1L * a * a * take + need -= take + } + a++ + } + return total - 2 * small + } +} diff --git a/src/main/kotlin/g3701_3800/s3727_maximum_alternating_sum_of_squares/readme.md b/src/main/kotlin/g3701_3800/s3727_maximum_alternating_sum_of_squares/readme.md new file mode 100644 index 00000000..7dc31573 --- /dev/null +++ b/src/main/kotlin/g3701_3800/s3727_maximum_alternating_sum_of_squares/readme.md @@ -0,0 +1,44 @@ +3727\. Maximum Alternating Sum of Squares + +Medium + +You are given an integer array `nums`. You may **rearrange the elements** in any order. + +The **alternating score** of an array `arr` is defined as: + +* score = arr[0]2 - arr[1]2 + arr[2]2 - arr[3]2 + ... + +Return an integer denoting the **maximum possible alternating score** of `nums` after rearranging its elements. + +**Example 1:** + +**Input:** nums = [1,2,3] + +**Output:** 12 + +**Explanation:** + +A possible rearrangement for `nums` is `[2,1,3]`, which gives the maximum alternating score among all possible rearrangements. + +The alternating score is calculated as: + +score = 22 - 12 + 32 = 4 - 1 + 9 = 12 + +**Example 2:** + +**Input:** nums = [1,-1,2,-2,3,-3] + +**Output:** 16 + +**Explanation:** + +A possible rearrangement for `nums` is `[-3,-1,-2,1,3,2]`, which gives the maximum alternating score among all possible rearrangements. + +The alternating score is calculated as: + +score = (-3)2 - (-1)2 + (-2)2 - (1)2 + (3)2 - (2)2 = 9 - 1 + 4 - 1 + 9 - 4 = 16 + +**Constraints:** + +* 1 <= nums.length <= 105 +* -4 * 104 <= nums[i] <= 4 * 104 \ No newline at end of file diff --git a/src/main/kotlin/g3701_3800/s3728_stable_subarrays_with_equal_boundary_and_interior_sum/Solution.kt b/src/main/kotlin/g3701_3800/s3728_stable_subarrays_with_equal_boundary_and_interior_sum/Solution.kt new file mode 100644 index 00000000..4bca07c8 --- /dev/null +++ b/src/main/kotlin/g3701_3800/s3728_stable_subarrays_with_equal_boundary_and_interior_sum/Solution.kt @@ -0,0 +1,28 @@ +package g3701_3800.s3728_stable_subarrays_with_equal_boundary_and_interior_sum + +// #Medium #Weekly_Contest_473 #2025_10_28_Time_146_ms_(60.00%)_Space_89.72_MB_(80.00%) + +class Solution { + fun countStableSubarrays(capacity: IntArray): Long { + val n = capacity.size + var res: Long = 0 + var pre: Long = 0 + val mpp: MutableMap> = HashMap() + for (i in 0.. = mpp[capacity[i].toLong()]!! + val cnt = t[pre - capacity[i]] + if (cnt != null) { + res += cnt + } + } + pre += capacity[i].toLong() + val t = mpp.computeIfAbsent(capacity[i].toLong()) { _: Long -> HashMap() } + t[pre] = t.getOrDefault(pre, 0L) + 1L + if (i > 0 && capacity[i] == 0 && capacity[i - 1] == 0) { + res-- + } + } + return res + } +} diff --git a/src/main/kotlin/g3701_3800/s3728_stable_subarrays_with_equal_boundary_and_interior_sum/readme.md b/src/main/kotlin/g3701_3800/s3728_stable_subarrays_with_equal_boundary_and_interior_sum/readme.md new file mode 100644 index 00000000..da434b7f --- /dev/null +++ b/src/main/kotlin/g3701_3800/s3728_stable_subarrays_with_equal_boundary_and_interior_sum/readme.md @@ -0,0 +1,48 @@ +3728\. Stable Subarrays With Equal Boundary and Interior Sum + +Medium + +You are given an integer array `capacity`. + +A **non-empty subarrays** `capacity[l..r]` is considered **stable** if: + +* Its length is **at least** 3. +* The **first** and **last** elements are each equal to the **sum** of all elements **strictly between** them (i.e., `capacity[l] = capacity[r] = capacity[l + 1] + capacity[l + 2] + ... + capacity[r - 1]`). + +Return an integer denoting the number of **stable subarrays**. + +**Example 1:** + +**Input:** capacity = [9,3,3,3,9] + +**Output:** 2 + +**Explanation:** + +* `[9,3,3,3,9]` is stable because the first and last elements are both 9, and the sum of the elements strictly between them is `3 + 3 + 3 = 9`. +* `[3,3,3]` is stable because the first and last elements are both 3, and the sum of the elements strictly between them is 3. + +**Example 2:** + +**Input:** capacity = [1,2,3,4,5] + +**Output:** 0 + +**Explanation:** + +No subarray of length at least 3 has equal first and last elements, so the answer is 0. + +**Example 3:** + +**Input:** capacity = [-4,4,0,0,-8,-4] + +**Output:** 1 + +**Explanation:** + +`[-4,4,0,0,-8,-4]` is stable because the first and last elements are both -4, and the sum of the elements strictly between them is `4 + 0 + 0 + (-8) = -4` + +**Constraints:** + +* 3 <= capacity.length <= 105 +* -109 <= capacity[i] <= 109 \ No newline at end of file diff --git a/src/main/kotlin/g3701_3800/s3729_count_distinct_subarrays_divisible_by_k_in_sorted_array/Solution.kt b/src/main/kotlin/g3701_3800/s3729_count_distinct_subarrays_divisible_by_k_in_sorted_array/Solution.kt new file mode 100644 index 00000000..e842b362 --- /dev/null +++ b/src/main/kotlin/g3701_3800/s3729_count_distinct_subarrays_divisible_by_k_in_sorted_array/Solution.kt @@ -0,0 +1,33 @@ +package g3701_3800.s3729_count_distinct_subarrays_divisible_by_k_in_sorted_array + +// #Hard #Weekly_Contest_473 #2025_10_28_Time_76_ms_(100.00%)_Space_80.47_MB_(100.00%) + +class Solution { + fun numGoodSubarrays(nums: IntArray, k: Int): Long { + val cnt: MutableMap = HashMap() + cnt[0] = 1L + var pre = 0 + val n = nums.size + var res: Long = 0 + for (a in nums) { + pre = (pre + a) % k + res += cnt.getOrDefault(pre, 0L) + cnt[pre] = cnt.getOrDefault(pre, 0L) + 1L + } + var i = 0 + while (i < n) { + var j = i + while (j < n && nums[j] == nums[i]) { + ++j + } + val l = j - i + for (ll in 1..1 <= nums.length <= 105 +* 1 <= nums[i] <= 109 +* `nums` is sorted in non-descending order. +* 1 <= k <= 109 \ No newline at end of file diff --git a/src/test/kotlin/g3701_3800/s3722_lexicographically_smallest_string_after_reverse/SolutionTest.kt b/src/test/kotlin/g3701_3800/s3722_lexicographically_smallest_string_after_reverse/SolutionTest.kt new file mode 100644 index 00000000..9405a81d --- /dev/null +++ b/src/test/kotlin/g3701_3800/s3722_lexicographically_smallest_string_after_reverse/SolutionTest.kt @@ -0,0 +1,22 @@ +package g3701_3800.s3722_lexicographically_smallest_string_after_reverse + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun lexSmallest() { + assertThat(Solution().lexSmallest("dcab"), equalTo("acdb")) + } + + @Test + fun lexSmallest2() { + assertThat(Solution().lexSmallest("abba"), equalTo("aabb")) + } + + @Test + fun lexSmallest3() { + assertThat(Solution().lexSmallest("zxy"), equalTo("xzy")) + } +} diff --git a/src/test/kotlin/g3701_3800/s3723_maximize_sum_of_squares_of_digits/SolutionTest.kt b/src/test/kotlin/g3701_3800/s3723_maximize_sum_of_squares_of_digits/SolutionTest.kt new file mode 100644 index 00000000..f951f371 --- /dev/null +++ b/src/test/kotlin/g3701_3800/s3723_maximize_sum_of_squares_of_digits/SolutionTest.kt @@ -0,0 +1,70 @@ +package g3701_3800.s3723_maximize_sum_of_squares_of_digits + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun maxSumOfSquares() { + assertThat(Solution().maxSumOfSquares(2, 3), equalTo("30")) + } + + @Test + fun maxSumOfSquares2() { + assertThat(Solution().maxSumOfSquares(2, 17), equalTo("98")) + } + + @Test + fun maxSumOfSquares3() { + assertThat(Solution().maxSumOfSquares(1, 10), equalTo("")) + } + + @Test + fun maxSumOfSquares4() { + // sum = 27 → nines = 3 + // places = 2 < 3 → should return "" + val result = Solution().maxSumOfSquares(2, 27) + assertThat(result, equalTo("")) + } + + @Test + fun maxSumOfSquares5() { + // sum = 28 → nines = 3, remSum = 1 + // places = 3 == nines and remSum > 0 → should return "" + val result = Solution().maxSumOfSquares(3, 28) + assertThat(result, equalTo("")) + } + + @Test + fun maxSumOfSquares6() { + // sum = 27 → nines = 3, remSum = 0 + // places = 3 == nines and remSum == 0 → should return "999" + val result = Solution().maxSumOfSquares(3, 27) + assertThat(result, equalTo("999")) + } + + @Test + fun maxSumOfSquares7() { + // sum = 10 → nines = 1, remSum = 1 + // ans = "9" + "1" = "91", length = 2, places = 2 → no padding + val result = Solution().maxSumOfSquares(2, 10) + assertThat(result, equalTo("91")) + } + + @Test + fun maxSumOfSquares8() { + // sum = 10 → nines = 1, remSum = 1 + // ans = "9" + "1" = "91", length = 2, places = 4 → add two zeros + val result = Solution().maxSumOfSquares(4, 10) + assertThat(result, equalTo("9100")) + } + + @Test + fun maxSumOfSquares9() { + // sum = 5 → nines = 0, remSum = 5 → ans = "5" + // places = 3 → add two zeros + val result = Solution().maxSumOfSquares(3, 5) + assertThat(result, equalTo("500")) + } +} diff --git a/src/test/kotlin/g3701_3800/s3724_minimum_operations_to_transform_array/SolutionTest.kt b/src/test/kotlin/g3701_3800/s3724_minimum_operations_to_transform_array/SolutionTest.kt new file mode 100644 index 00000000..c3d55cb8 --- /dev/null +++ b/src/test/kotlin/g3701_3800/s3724_minimum_operations_to_transform_array/SolutionTest.kt @@ -0,0 +1,31 @@ +package g3701_3800.s3724_minimum_operations_to_transform_array + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun minOperations() { + assertThat( + Solution().minOperations(intArrayOf(2, 8), intArrayOf(1, 7, 3)), + equalTo(4L), + ) + } + + @Test + fun minOperations2() { + assertThat( + Solution().minOperations(intArrayOf(1, 3, 6), intArrayOf(2, 4, 5, 3)), + equalTo(4L), + ) + } + + @Test + fun minOperations3() { + assertThat( + Solution().minOperations(intArrayOf(2), intArrayOf(3, 4)), + equalTo(3L), + ) + } +} diff --git a/src/test/kotlin/g3701_3800/s3725_count_ways_to_choose_coprime_integers_from_rows/SolutionTest.kt b/src/test/kotlin/g3701_3800/s3725_count_ways_to_choose_coprime_integers_from_rows/SolutionTest.kt new file mode 100644 index 00000000..30af29cd --- /dev/null +++ b/src/test/kotlin/g3701_3800/s3725_count_ways_to_choose_coprime_integers_from_rows/SolutionTest.kt @@ -0,0 +1,23 @@ +package g3701_3800.s3725_count_ways_to_choose_coprime_integers_from_rows + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun countCoprime() { + assertThat( + Solution().countCoprime(arrayOf(intArrayOf(1, 2), intArrayOf(3, 4))), + equalTo(3), + ) + } + + @Test + fun countCoprime2() { + assertThat( + Solution().countCoprime(arrayOf(intArrayOf(2, 2), intArrayOf(2, 2))), + equalTo(0), + ) + } +} diff --git a/src/test/kotlin/g3701_3800/s3726_remove_zeros_in_decimal_representation/SolutionTest.kt b/src/test/kotlin/g3701_3800/s3726_remove_zeros_in_decimal_representation/SolutionTest.kt new file mode 100644 index 00000000..72bd3f87 --- /dev/null +++ b/src/test/kotlin/g3701_3800/s3726_remove_zeros_in_decimal_representation/SolutionTest.kt @@ -0,0 +1,17 @@ +package g3701_3800.s3726_remove_zeros_in_decimal_representation + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun removeZeros() { + assertThat(Solution().removeZeros(1020030L), equalTo(123L)) + } + + @Test + fun removeZeros2() { + assertThat(Solution().removeZeros(1L), equalTo(1L)) + } +} diff --git a/src/test/kotlin/g3701_3800/s3727_maximum_alternating_sum_of_squares/SolutionTest.kt b/src/test/kotlin/g3701_3800/s3727_maximum_alternating_sum_of_squares/SolutionTest.kt new file mode 100644 index 00000000..9d9041b1 --- /dev/null +++ b/src/test/kotlin/g3701_3800/s3727_maximum_alternating_sum_of_squares/SolutionTest.kt @@ -0,0 +1,23 @@ +package g3701_3800.s3727_maximum_alternating_sum_of_squares + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun maxAlternatingSum() { + assertThat( + Solution().maxAlternatingSum(intArrayOf(1, 2, 3)), + equalTo(12L), + ) + } + + @Test + fun maxAlternatingSum2() { + assertThat( + Solution().maxAlternatingSum(intArrayOf(1, -1, 2, -2, 3, -3)), + equalTo(16L), + ) + } +} diff --git a/src/test/kotlin/g3701_3800/s3728_stable_subarrays_with_equal_boundary_and_interior_sum/SolutionTest.kt b/src/test/kotlin/g3701_3800/s3728_stable_subarrays_with_equal_boundary_and_interior_sum/SolutionTest.kt new file mode 100644 index 00000000..cda14fa9 --- /dev/null +++ b/src/test/kotlin/g3701_3800/s3728_stable_subarrays_with_equal_boundary_and_interior_sum/SolutionTest.kt @@ -0,0 +1,31 @@ +package g3701_3800.s3728_stable_subarrays_with_equal_boundary_and_interior_sum + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun countStableSubarrays() { + assertThat( + Solution().countStableSubarrays(intArrayOf(9, 3, 3, 3, 9)), + equalTo(2L), + ) + } + + @Test + fun countStableSubarrays2() { + assertThat( + Solution().countStableSubarrays(intArrayOf(1, 2, 3, 4, 5)), + equalTo(0L), + ) + } + + @Test + fun countStableSubarrays3() { + assertThat( + Solution().countStableSubarrays(intArrayOf(-4, 4, 0, 0, -8, -4)), + equalTo(1L), + ) + } +} diff --git a/src/test/kotlin/g3701_3800/s3729_count_distinct_subarrays_divisible_by_k_in_sorted_array/SolutionTest.kt b/src/test/kotlin/g3701_3800/s3729_count_distinct_subarrays_divisible_by_k_in_sorted_array/SolutionTest.kt new file mode 100644 index 00000000..65f62afb --- /dev/null +++ b/src/test/kotlin/g3701_3800/s3729_count_distinct_subarrays_divisible_by_k_in_sorted_array/SolutionTest.kt @@ -0,0 +1,23 @@ +package g3701_3800.s3729_count_distinct_subarrays_divisible_by_k_in_sorted_array + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun numGoodSubarrays() { + assertThat( + Solution().numGoodSubarrays(intArrayOf(1, 2, 3), 3), + equalTo(3L), + ) + } + + @Test + fun numGoodSubarrays2() { + assertThat( + Solution().numGoodSubarrays(intArrayOf(2, 2, 2, 2, 2, 2), 6), + equalTo(2L), + ) + } +} From 321b781ca9583c1fa08504f928a97892fe8b2876 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Wed, 29 Oct 2025 08:22:15 +0200 Subject: [PATCH 2/2] Updated tags --- .../Solution.kt | 3 +- .../Solution.kt | 3 +- .../Solution.kt | 3 +- .../Solution.kt | 3 +- .../Solution.kt | 3 +- .../Solution.kt | 3 +- .../Solution.kt | 39 ++++++++++-------- .../Solution.kt | 41 ++++++++----------- 8 files changed, 51 insertions(+), 47 deletions(-) diff --git a/src/main/kotlin/g3701_3800/s3722_lexicographically_smallest_string_after_reverse/Solution.kt b/src/main/kotlin/g3701_3800/s3722_lexicographically_smallest_string_after_reverse/Solution.kt index 314db8f7..ab3a095f 100644 --- a/src/main/kotlin/g3701_3800/s3722_lexicographically_smallest_string_after_reverse/Solution.kt +++ b/src/main/kotlin/g3701_3800/s3722_lexicographically_smallest_string_after_reverse/Solution.kt @@ -1,6 +1,7 @@ package g3701_3800.s3722_lexicographically_smallest_string_after_reverse -// #Medium #Biweekly_Contest_168 #2025_10_28_Time_8_ms_(100.00%)_Space_45.74_MB_(100.00%) +// #Medium #Binary_Search #Two_Pointers #Enumeration #Biweekly_Contest_168 +// #2025_10_28_Time_8_ms_(100.00%)_Space_45.74_MB_(100.00%) class Solution { fun lexSmallest(s: String): String { diff --git a/src/main/kotlin/g3701_3800/s3723_maximize_sum_of_squares_of_digits/Solution.kt b/src/main/kotlin/g3701_3800/s3723_maximize_sum_of_squares_of_digits/Solution.kt index 66aaf5ef..95fe5a05 100644 --- a/src/main/kotlin/g3701_3800/s3723_maximize_sum_of_squares_of_digits/Solution.kt +++ b/src/main/kotlin/g3701_3800/s3723_maximize_sum_of_squares_of_digits/Solution.kt @@ -1,6 +1,7 @@ package g3701_3800.s3723_maximize_sum_of_squares_of_digits -// #Medium #Biweekly_Contest_168 #2025_10_28_Time_16_ms_(94.44%)_Space_47.62_MB_(61.11%) +// #Medium #Math #Greedy #Biweekly_Contest_168 +// #2025_10_28_Time_16_ms_(94.44%)_Space_47.62_MB_(61.11%) class Solution { fun maxSumOfSquares(places: Int, sum: Int): String { diff --git a/src/main/kotlin/g3701_3800/s3724_minimum_operations_to_transform_array/Solution.kt b/src/main/kotlin/g3701_3800/s3724_minimum_operations_to_transform_array/Solution.kt index 8ab3c205..fa33dcfc 100644 --- a/src/main/kotlin/g3701_3800/s3724_minimum_operations_to_transform_array/Solution.kt +++ b/src/main/kotlin/g3701_3800/s3724_minimum_operations_to_transform_array/Solution.kt @@ -1,6 +1,7 @@ package g3701_3800.s3724_minimum_operations_to_transform_array -// #Medium #Biweekly_Contest_168 #2025_10_28_Time_10_ms_(83.33%)_Space_66.99_MB_(100.00%) +// #Medium #Array #Greedy #Biweekly_Contest_168 +// #2025_10_28_Time_10_ms_(83.33%)_Space_66.99_MB_(100.00%) import kotlin.math.abs import kotlin.math.max diff --git a/src/main/kotlin/g3701_3800/s3725_count_ways_to_choose_coprime_integers_from_rows/Solution.kt b/src/main/kotlin/g3701_3800/s3725_count_ways_to_choose_coprime_integers_from_rows/Solution.kt index 1a6963d6..cf73d6c8 100644 --- a/src/main/kotlin/g3701_3800/s3725_count_ways_to_choose_coprime_integers_from_rows/Solution.kt +++ b/src/main/kotlin/g3701_3800/s3725_count_ways_to_choose_coprime_integers_from_rows/Solution.kt @@ -1,6 +1,7 @@ package g3701_3800.s3725_count_ways_to_choose_coprime_integers_from_rows -// #Hard #Biweekly_Contest_168 #2025_10_28_Time_29_ms_(100.00%)_Space_51.80_MB_(100.00%) +// #Hard #Array #Dynamic_Programming #Math #Matrix #Number_Theory #Combinatorics +// #Biweekly_Contest_168 #2025_10_28_Time_29_ms_(100.00%)_Space_51.80_MB_(100.00%) import kotlin.math.max diff --git a/src/main/kotlin/g3701_3800/s3726_remove_zeros_in_decimal_representation/Solution.kt b/src/main/kotlin/g3701_3800/s3726_remove_zeros_in_decimal_representation/Solution.kt index 4fc991a8..e9307f46 100644 --- a/src/main/kotlin/g3701_3800/s3726_remove_zeros_in_decimal_representation/Solution.kt +++ b/src/main/kotlin/g3701_3800/s3726_remove_zeros_in_decimal_representation/Solution.kt @@ -1,6 +1,7 @@ package g3701_3800.s3726_remove_zeros_in_decimal_representation -// #Easy #Weekly_Contest_473 #2025_10_28_Time_2_ms_(73.91%)_Space_41.33_MB_(78.26%) +// #Easy #Math #Simulation #Weekly_Contest_473 +// #2025_10_28_Time_2_ms_(73.91%)_Space_41.33_MB_(78.26%) class Solution { fun removeZeros(n: Long): Long { diff --git a/src/main/kotlin/g3701_3800/s3727_maximum_alternating_sum_of_squares/Solution.kt b/src/main/kotlin/g3701_3800/s3727_maximum_alternating_sum_of_squares/Solution.kt index 714198ab..4e38303b 100644 --- a/src/main/kotlin/g3701_3800/s3727_maximum_alternating_sum_of_squares/Solution.kt +++ b/src/main/kotlin/g3701_3800/s3727_maximum_alternating_sum_of_squares/Solution.kt @@ -1,6 +1,7 @@ package g3701_3800.s3727_maximum_alternating_sum_of_squares -// #Medium #Weekly_Contest_473 #2025_10_28_Time_9_ms_(100.00%)_Space_90.79_MB_(13.04%) +// #Medium #Array #Sorting #Greedy #Weekly_Contest_473 +// #2025_10_28_Time_9_ms_(100.00%)_Space_90.79_MB_(13.04%) import kotlin.math.abs import kotlin.math.min diff --git a/src/main/kotlin/g3701_3800/s3728_stable_subarrays_with_equal_boundary_and_interior_sum/Solution.kt b/src/main/kotlin/g3701_3800/s3728_stable_subarrays_with_equal_boundary_and_interior_sum/Solution.kt index 4bca07c8..e1cd3450 100644 --- a/src/main/kotlin/g3701_3800/s3728_stable_subarrays_with_equal_boundary_and_interior_sum/Solution.kt +++ b/src/main/kotlin/g3701_3800/s3728_stable_subarrays_with_equal_boundary_and_interior_sum/Solution.kt @@ -1,28 +1,31 @@ package g3701_3800.s3728_stable_subarrays_with_equal_boundary_and_interior_sum -// #Medium #Weekly_Contest_473 #2025_10_28_Time_146_ms_(60.00%)_Space_89.72_MB_(80.00%) +// #Medium #Array #Hash_Table #Prefix_Sum #Weekly_Contest_473 +// #2025_10_29_Time_109_ms_(100.00%)_Space_82.32_MB_(88.89%) class Solution { fun countStableSubarrays(capacity: IntArray): Long { - val n = capacity.size - var res: Long = 0 - var pre: Long = 0 - val mpp: MutableMap> = HashMap() - for (i in 0.. = mpp[capacity[i].toLong()]!! - val cnt = t[pre - capacity[i]] - if (cnt != null) { - res += cnt + var sum: Long = 0 + val map: MutableMap> = HashMap() + var index = 0 + var ans: Long = 0 + for (c in capacity) { + sum += c.toLong() + var elementMap = map[c] + if (elementMap == null) { + elementMap = HashMap() + map[c] = elementMap + elementMap[sum] = 1 + } else { + var orDefault = elementMap.getOrDefault(sum - 2 * c, 0) + elementMap[sum] = elementMap.getOrDefault(sum, 0) + 1 + if (c == 0 && capacity[index - 1] == 0) { + orDefault-- } + ans += orDefault.toLong() } - pre += capacity[i].toLong() - val t = mpp.computeIfAbsent(capacity[i].toLong()) { _: Long -> HashMap() } - t[pre] = t.getOrDefault(pre, 0L) + 1L - if (i > 0 && capacity[i] == 0 && capacity[i - 1] == 0) { - res-- - } + index++ } - return res + return ans } } diff --git a/src/main/kotlin/g3701_3800/s3729_count_distinct_subarrays_divisible_by_k_in_sorted_array/Solution.kt b/src/main/kotlin/g3701_3800/s3729_count_distinct_subarrays_divisible_by_k_in_sorted_array/Solution.kt index e842b362..68884c1d 100644 --- a/src/main/kotlin/g3701_3800/s3729_count_distinct_subarrays_divisible_by_k_in_sorted_array/Solution.kt +++ b/src/main/kotlin/g3701_3800/s3729_count_distinct_subarrays_divisible_by_k_in_sorted_array/Solution.kt @@ -1,33 +1,28 @@ package g3701_3800.s3729_count_distinct_subarrays_divisible_by_k_in_sorted_array -// #Hard #Weekly_Contest_473 #2025_10_28_Time_76_ms_(100.00%)_Space_80.47_MB_(100.00%) +// #Hard #Array #Hash_Table #Prefix_Sum #Weekly_Contest_473 +// #2025_10_29_Time_61_ms_(100.00%)_Space_80.76_MB_(100.00%) class Solution { fun numGoodSubarrays(nums: IntArray, k: Int): Long { - val cnt: MutableMap = HashMap() - cnt[0] = 1L - var pre = 0 - val n = nums.size - var res: Long = 0 - for (a in nums) { - pre = (pre + a) % k - res += cnt.getOrDefault(pre, 0L) - cnt[pre] = cnt.getOrDefault(pre, 0L) + 1L - } - var i = 0 - while (i < n) { - var j = i - while (j < n && nums[j] == nums[i]) { - ++j - } - val l = j - i - for (ll in 1.. = HashMap(nums.size, 1f) + cnt[0] = 1 + var sum: Long = 0 + var lastStart = 0 + var ans: Long = 0 + for (i in nums.indices) { + val x = nums[i] + if (i > 0 && x != nums[i - 1]) { + var s = sum + for (t in i - lastStart downTo 1) { + cnt.merge((s % k).toInt(), 1) { a: Int?, b: Int? -> Integer.sum(a!!, b!!) } + s -= nums[i - 1].toLong() } + lastStart = i } - i = j + sum += x.toLong() + ans += cnt.getOrDefault((sum % k).toInt(), 0).toLong() } - return res + return ans } }