From 7aa800a602902c7d6bb1bc0bd3a9aa0c09bc8ea7 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Sun, 23 Feb 2025 09:26:36 +0200 Subject: [PATCH 1/5] Added tasks 3461-3464 --- .../Solution.kt | 20 ++++ .../readme.md | 49 ++++++++ .../Solution.kt | 34 ++++++ .../readme.md | 42 +++++++ .../Solution.kt | 57 +++++++++ .../readme.md | 49 ++++++++ .../Solution.kt | 108 ++++++++++++++++++ .../readme.md | 59 ++++++++++ .../SolutionTest.kt | 17 +++ .../SolutionTest.kt | 31 +++++ .../SolutionTest.kt | 22 ++++ .../SolutionTest.kt | 59 ++++++++++ 12 files changed, 547 insertions(+) create mode 100644 src/main/kotlin/g3401_3500/s3461_check_if_digits_are_equal_in_string_after_operations_i/Solution.kt create mode 100644 src/main/kotlin/g3401_3500/s3461_check_if_digits_are_equal_in_string_after_operations_i/readme.md create mode 100644 src/main/kotlin/g3401_3500/s3462_maximum_sum_with_at_most_k_elements/Solution.kt create mode 100644 src/main/kotlin/g3401_3500/s3462_maximum_sum_with_at_most_k_elements/readme.md create mode 100644 src/main/kotlin/g3401_3500/s3463_check_if_digits_are_equal_in_string_after_operations_ii/Solution.kt create mode 100644 src/main/kotlin/g3401_3500/s3463_check_if_digits_are_equal_in_string_after_operations_ii/readme.md create mode 100644 src/main/kotlin/g3401_3500/s3464_maximize_the_distance_between_points_on_a_square/Solution.kt create mode 100644 src/main/kotlin/g3401_3500/s3464_maximize_the_distance_between_points_on_a_square/readme.md create mode 100644 src/test/kotlin/g3401_3500/s3461_check_if_digits_are_equal_in_string_after_operations_i/SolutionTest.kt create mode 100644 src/test/kotlin/g3401_3500/s3462_maximum_sum_with_at_most_k_elements/SolutionTest.kt create mode 100644 src/test/kotlin/g3401_3500/s3463_check_if_digits_are_equal_in_string_after_operations_ii/SolutionTest.kt create mode 100644 src/test/kotlin/g3401_3500/s3464_maximize_the_distance_between_points_on_a_square/SolutionTest.kt diff --git a/src/main/kotlin/g3401_3500/s3461_check_if_digits_are_equal_in_string_after_operations_i/Solution.kt b/src/main/kotlin/g3401_3500/s3461_check_if_digits_are_equal_in_string_after_operations_i/Solution.kt new file mode 100644 index 000000000..9de4a917e --- /dev/null +++ b/src/main/kotlin/g3401_3500/s3461_check_if_digits_are_equal_in_string_after_operations_i/Solution.kt @@ -0,0 +1,20 @@ +package g3401_3500.s3461_check_if_digits_are_equal_in_string_after_operations_i + +// #Easy #2025_02_23_Time_8_ms_(100.00%)_Space_37.90_MB_(100.00%) + +class Solution { + fun hasSameDigits(s: String): Boolean { + var s = s + var n = s.length + while (n > 2) { + val nstr = StringBuilder() + for (i in 1.., limits: IntArray, k: Int): Long { + if (grid.isEmpty()) { + return 0 + } + val pq = PriorityQueue(Collections.reverseOrder()) + var temp: PriorityQueue? + for (i in grid.indices) { + temp = PriorityQueue(Collections.reverseOrder()) + for (j in grid[i].indices) { + temp.add(grid[i][j]) + } + var cnt = 0 + while (temp.isNotEmpty() && cnt < limits[i]) { + pq.add(temp.poll()) + cnt += 1 + } + } + var result: Long = 0 + var count: Long = 0 + while (pq.isNotEmpty() && count < k) { + result += pq.poll()!!.toLong() + count += 1 + } + return result + } +} diff --git a/src/main/kotlin/g3401_3500/s3462_maximum_sum_with_at_most_k_elements/readme.md b/src/main/kotlin/g3401_3500/s3462_maximum_sum_with_at_most_k_elements/readme.md new file mode 100644 index 000000000..4b9177359 --- /dev/null +++ b/src/main/kotlin/g3401_3500/s3462_maximum_sum_with_at_most_k_elements/readme.md @@ -0,0 +1,42 @@ +3462\. Maximum Sum With at Most K Elements + +Medium + +You are given a 2D integer matrix `grid` of size `n x m`, an integer array `limits` of length `n`, and an integer `k`. The task is to find the **maximum sum** of **at most** `k` elements from the matrix `grid` such that: + +* The number of elements taken from the ith row of `grid` does not exceed `limits[i]`. + + +Return the **maximum sum**. + +**Example 1:** + +**Input:** grid = [[1,2],[3,4]], limits = [1,2], k = 2 + +**Output:** 7 + +**Explanation:** + +* From the second row, we can take at most 2 elements. The elements taken are 4 and 3. +* The maximum possible sum of at most 2 selected elements is `4 + 3 = 7`. + +**Example 2:** + +**Input:** grid = [[5,3,7],[8,2,6]], limits = [2,2], k = 3 + +**Output:** 21 + +**Explanation:** + +* From the first row, we can take at most 2 elements. The element taken is 7. +* From the second row, we can take at most 2 elements. The elements taken are 8 and 6. +* The maximum possible sum of at most 3 selected elements is `7 + 8 + 6 = 21`. + +**Constraints:** + +* `n == grid.length == limits.length` +* `m == grid[i].length` +* `1 <= n, m <= 500` +* 0 <= grid[i][j] <= 105 +* `0 <= limits[i] <= m` +* `0 <= k <= min(n * m, sum(limits))` \ No newline at end of file diff --git a/src/main/kotlin/g3401_3500/s3463_check_if_digits_are_equal_in_string_after_operations_ii/Solution.kt b/src/main/kotlin/g3401_3500/s3463_check_if_digits_are_equal_in_string_after_operations_ii/Solution.kt new file mode 100644 index 000000000..4a511cc1e --- /dev/null +++ b/src/main/kotlin/g3401_3500/s3463_check_if_digits_are_equal_in_string_after_operations_ii/Solution.kt @@ -0,0 +1,57 @@ +package g3401_3500.s3463_check_if_digits_are_equal_in_string_after_operations_ii + +// #Hard #2025_02_23_Time_63_ms_(100.00%)_Space_44.31_MB_(100.00%) + +class Solution { + fun hasSameDigits(s: String): Boolean { + val n = s.length + val nMunus2 = n - 2 + var f0 = 0 + var f1 = 0 + for (j in 0..nMunus2) { + val c = binomMod10(nMunus2, j) + f0 = (f0 + c * (s[j].code - '0'.code)) % 10 + f1 = (f1 + c * (s[j + 1].code - '0'.code)) % 10 + } + return f0 == f1 + } + + private fun binomMod10(n: Int, k: Int): Int { + val r2 = binomMod2(n, k) + val r5 = binomMod5(n, k) + for (x in 0..9) { + if (x % 2 == r2 && x % 5 == r5) { + return x + } + } + return 0 + } + + private fun binomMod2(n: Int, k: Int): Int { + return if ((n and k) == k) 1 else 0 + } + + private fun binomMod5(n: Int, k: Int): Int { + var n = n + var k = k + val t = arrayOf( + intArrayOf(1), + intArrayOf(1, 1), + intArrayOf(1, 2, 1), + intArrayOf(1, 3, 3, 1), + intArrayOf(1, 4, 1, 4, 1), + ) + var res = 1 + while (n > 0 || k > 0) { + val nd = n % 5 + val kd = k % 5 + if (kd > nd) { + return 0 + } + res = (res * t[nd]!![kd]) % 5 + n /= 5 + k /= 5 + } + return res + } +} diff --git a/src/main/kotlin/g3401_3500/s3463_check_if_digits_are_equal_in_string_after_operations_ii/readme.md b/src/main/kotlin/g3401_3500/s3463_check_if_digits_are_equal_in_string_after_operations_ii/readme.md new file mode 100644 index 000000000..fe4e2de15 --- /dev/null +++ b/src/main/kotlin/g3401_3500/s3463_check_if_digits_are_equal_in_string_after_operations_ii/readme.md @@ -0,0 +1,49 @@ +3463\. Check If Digits Are Equal in String After Operations II + +Hard + +You are given a string `s` consisting of digits. Perform the following operation repeatedly until the string has **exactly** two digits: + +* For each pair of consecutive digits in `s`, starting from the first digit, calculate a new digit as the sum of the two digits **modulo** 10. +* Replace `s` with the sequence of newly calculated digits, _maintaining the order_ in which they are computed. + +Return `true` if the final two digits in `s` are the **same**; otherwise, return `false`. + +**Example 1:** + +**Input:** s = "3902" + +**Output:** true + +**Explanation:** + +* Initially, `s = "3902"` +* First operation: + * `(s[0] + s[1]) % 10 = (3 + 9) % 10 = 2` + * `(s[1] + s[2]) % 10 = (9 + 0) % 10 = 9` + * `(s[2] + s[3]) % 10 = (0 + 2) % 10 = 2` + * `s` becomes `"292"` +* Second operation: + * `(s[0] + s[1]) % 10 = (2 + 9) % 10 = 1` + * `(s[1] + s[2]) % 10 = (9 + 2) % 10 = 1` + * `s` becomes `"11"` +* Since the digits in `"11"` are the same, the output is `true`. + +**Example 2:** + +**Input:** s = "34789" + +**Output:** false + +**Explanation:** + +* Initially, `s = "34789"`. +* After the first operation, `s = "7157"`. +* After the second operation, `s = "862"`. +* After the third operation, `s = "48"`. +* Since `'4' != '8'`, the output is `false`. + +**Constraints:** + +* 3 <= s.length <= 105 +* `s` consists of only digits. \ No newline at end of file diff --git a/src/main/kotlin/g3401_3500/s3464_maximize_the_distance_between_points_on_a_square/Solution.kt b/src/main/kotlin/g3401_3500/s3464_maximize_the_distance_between_points_on_a_square/Solution.kt new file mode 100644 index 000000000..b54eceb8e --- /dev/null +++ b/src/main/kotlin/g3401_3500/s3464_maximize_the_distance_between_points_on_a_square/Solution.kt @@ -0,0 +1,108 @@ +package g3401_3500.s3464_maximize_the_distance_between_points_on_a_square + +// #Hard #2025_02_23_Time_159_ms_(100.00%)_Space_67.20_MB_(100.00%) + +class Solution { + fun maxDistance(sideLength: Int, points: Array, requiredPoints: Int): Int { + val perimeter = 4L * sideLength + val numPoints = points.size + val mappedPositions = LongArray(numPoints) + for (i in 0..= right) { + return false + } + selectedCount++ + previousPosition = extendedPositions[nextIndex] + currentIndex = nextIndex + } + return selectedCount == requiredPoints && + (previousPosition - mappedPositions[startIndex] <= perimeter - minDistance) + } + + private fun lowerBound(arr: LongArray, left: Int, right: Int, target: Long): Int { + var left = left + var right = right + while (left < right) { + val mid = left + (right - left) / 2 + if (arr[mid] >= target) { + right = mid + } else { + left = mid + 1 + } + } + return left + } +} diff --git a/src/main/kotlin/g3401_3500/s3464_maximize_the_distance_between_points_on_a_square/readme.md b/src/main/kotlin/g3401_3500/s3464_maximize_the_distance_between_points_on_a_square/readme.md new file mode 100644 index 000000000..2fc14a006 --- /dev/null +++ b/src/main/kotlin/g3401_3500/s3464_maximize_the_distance_between_points_on_a_square/readme.md @@ -0,0 +1,59 @@ +3464\. Maximize the Distance Between Points on a Square + +Hard + +You are given an integer `side`, representing the edge length of a square with corners at `(0, 0)`, `(0, side)`, `(side, 0)`, and `(side, side)` on a Cartesian plane. + +You are also given a **positive** integer `k` and a 2D integer array `points`, where points[i] = [xi, yi] represents the coordinate of a point lying on the **boundary** of the square. + +You need to select `k` elements among `points` such that the **minimum** Manhattan distance between any two points is **maximized**. + +Return the **maximum** possible **minimum** Manhattan distance between the selected `k` points. + +The Manhattan Distance between two cells (xi, yi) and (xj, yj) is |xi - xj| + |yi - yj|. + +**Example 1:** + +**Input:** side = 2, points = [[0,2],[2,0],[2,2],[0,0]], k = 4 + +**Output:** 2 + +**Explanation:** + +![](https://assets.leetcode.com/uploads/2025/01/28/4080_example0_revised.png) + +Select all four points. + +**Example 2:** + +**Input:** side = 2, points = [[0,0],[1,2],[2,0],[2,2],[2,1]], k = 4 + +**Output:** 1 + +**Explanation:** + +![](https://assets.leetcode.com/uploads/2025/01/28/4080_example1_revised.png) + +Select the points `(0, 0)`, `(2, 0)`, `(2, 2)`, and `(2, 1)`. + +**Example 3:** + +**Input:** side = 2, points = [[0,0],[0,1],[0,2],[1,2],[2,0],[2,2],[2,1]], k = 5 + +**Output:** 1 + +**Explanation:** + +![](https://assets.leetcode.com/uploads/2025/01/28/4080_example2_revised.png) + +Select the points `(0, 0)`, `(0, 1)`, `(0, 2)`, `(1, 2)`, and `(2, 2)`. + +**Constraints:** + +* 1 <= side <= 109 +* 4 <= points.length <= min(4 * side, 15 * 103) +* `points[i] == [xi, yi]` +* The input is generated such that: + * `points[i]` lies on the boundary of the square. + * All `points[i]` are **unique**. +* `4 <= k <= min(25, points.length)` \ No newline at end of file diff --git a/src/test/kotlin/g3401_3500/s3461_check_if_digits_are_equal_in_string_after_operations_i/SolutionTest.kt b/src/test/kotlin/g3401_3500/s3461_check_if_digits_are_equal_in_string_after_operations_i/SolutionTest.kt new file mode 100644 index 000000000..f5472b652 --- /dev/null +++ b/src/test/kotlin/g3401_3500/s3461_check_if_digits_are_equal_in_string_after_operations_i/SolutionTest.kt @@ -0,0 +1,17 @@ +package g3401_3500.s3461_check_if_digits_are_equal_in_string_after_operations_i + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun hasSameDigits() { + assertThat(Solution().hasSameDigits("3902"), equalTo(true)) + } + + @Test + fun hasSameDigits2() { + assertThat(Solution().hasSameDigits("34789"), equalTo(false)) + } +} diff --git a/src/test/kotlin/g3401_3500/s3462_maximum_sum_with_at_most_k_elements/SolutionTest.kt b/src/test/kotlin/g3401_3500/s3462_maximum_sum_with_at_most_k_elements/SolutionTest.kt new file mode 100644 index 000000000..9f2ede2b3 --- /dev/null +++ b/src/test/kotlin/g3401_3500/s3462_maximum_sum_with_at_most_k_elements/SolutionTest.kt @@ -0,0 +1,31 @@ +package g3401_3500.s3462_maximum_sum_with_at_most_k_elements + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun maxSum() { + assertThat( + Solution().maxSum(arrayOf(intArrayOf(1, 2), intArrayOf(3, 4)), intArrayOf(1, 2), 2), + equalTo(7L), + ) + } + + @Test + fun maxSum2() { + assertThat( + Solution().maxSum(arrayOf(intArrayOf(5, 3, 7), intArrayOf(8, 2, 6)), intArrayOf(2, 2), 3), + equalTo(21L), + ) + } + + @Test + fun maxSum3() { + assertThat( + Solution().maxSum(arrayOf(), intArrayOf(2, 2), 3), + equalTo(0L), + ) + } +} diff --git a/src/test/kotlin/g3401_3500/s3463_check_if_digits_are_equal_in_string_after_operations_ii/SolutionTest.kt b/src/test/kotlin/g3401_3500/s3463_check_if_digits_are_equal_in_string_after_operations_ii/SolutionTest.kt new file mode 100644 index 000000000..885d9373d --- /dev/null +++ b/src/test/kotlin/g3401_3500/s3463_check_if_digits_are_equal_in_string_after_operations_ii/SolutionTest.kt @@ -0,0 +1,22 @@ +package g3401_3500.s3463_check_if_digits_are_equal_in_string_after_operations_ii + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun hasSameDigits() { + assertThat(Solution().hasSameDigits("3902"), equalTo(true)) + } + + @Test + fun hasSameDigits2() { + assertThat(Solution().hasSameDigits("34789"), equalTo(false)) + } + + @Test + fun hasSameDigits3() { + assertThat(Solution().hasSameDigits("3506677"), equalTo(false)) + } +} diff --git a/src/test/kotlin/g3401_3500/s3464_maximize_the_distance_between_points_on_a_square/SolutionTest.kt b/src/test/kotlin/g3401_3500/s3464_maximize_the_distance_between_points_on_a_square/SolutionTest.kt new file mode 100644 index 000000000..a1ab74e13 --- /dev/null +++ b/src/test/kotlin/g3401_3500/s3464_maximize_the_distance_between_points_on_a_square/SolutionTest.kt @@ -0,0 +1,59 @@ +package g3401_3500.s3464_maximize_the_distance_between_points_on_a_square + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun maxDistance() { + assertThat( + Solution().maxDistance( + 2, + arrayOf(intArrayOf(0, 2), intArrayOf(2, 0), intArrayOf(2, 2), intArrayOf(0, 0)), + 4, + ), + equalTo(2), + ) + } + + @Test + fun maxDistance2() { + assertThat( + Solution() + .maxDistance( + 2, + arrayOf( + intArrayOf(0, 0), + intArrayOf(1, 2), + intArrayOf(2, 0), + intArrayOf(2, 2), + intArrayOf(2, 1), + ), + 4, + ), + equalTo(1), + ) + } + + @Test + fun maxDistance3() { + assertThat( + Solution() + .maxDistance( + 2, + arrayOf( + intArrayOf(0, 0), + intArrayOf(0, 1), + intArrayOf(0, 2), + intArrayOf(1, 2), + intArrayOf(2, 0), + intArrayOf(2, 2), + intArrayOf(2, 1), + ), + 5, + ), + equalTo(1), + ) + } +} From d8bb548a9516e622ea3dd097af0a56a1d8cf0a07 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Sun, 23 Feb 2025 09:35:34 +0200 Subject: [PATCH 2/5] Improved task --- .../Solution.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/kotlin/g3401_3500/s3463_check_if_digits_are_equal_in_string_after_operations_ii/Solution.kt b/src/main/kotlin/g3401_3500/s3463_check_if_digits_are_equal_in_string_after_operations_ii/Solution.kt index 4a511cc1e..8924e5081 100644 --- a/src/main/kotlin/g3401_3500/s3463_check_if_digits_are_equal_in_string_after_operations_ii/Solution.kt +++ b/src/main/kotlin/g3401_3500/s3463_check_if_digits_are_equal_in_string_after_operations_ii/Solution.kt @@ -34,7 +34,7 @@ class Solution { private fun binomMod5(n: Int, k: Int): Int { var n = n var k = k - val t = arrayOf( + val t = arrayOf( intArrayOf(1), intArrayOf(1, 1), intArrayOf(1, 2, 1), @@ -48,7 +48,7 @@ class Solution { if (kd > nd) { return 0 } - res = (res * t[nd]!![kd]) % 5 + res = (res * t[nd][kd]) % 5 n /= 5 k /= 5 } From 3c5fbae28602ea26612920f3cd4a15bf08a7c89e Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Tue, 25 Feb 2025 03:49:14 +0200 Subject: [PATCH 3/5] Improved tasks --- .../s3436_find_valid_emails/script.sql | 2 +- .../Solution.kt | 24 +-- .../Solution.kt | 5 +- .../Solution.kt | 105 +++++++----- .../Solution.kt | 149 +++++++----------- 5 files changed, 138 insertions(+), 147 deletions(-) diff --git a/src/main/kotlin/g3401_3500/s3436_find_valid_emails/script.sql b/src/main/kotlin/g3401_3500/s3436_find_valid_emails/script.sql index 200e7bd50..26f7d4f8d 100644 --- a/src/main/kotlin/g3401_3500/s3436_find_valid_emails/script.sql +++ b/src/main/kotlin/g3401_3500/s3436_find_valid_emails/script.sql @@ -1,5 +1,5 @@ # Write your MySQL query statement below -# #Easy #2025_02_04_Time_451_ms_(70.84%)_Space_0.0_MB_(100.00%) +# #Easy #Database #2025_02_04_Time_451_ms_(70.84%)_Space_0.0_MB_(100.00%) select user_id, email from users where email regexp '^[A-Za-z0-9_]+@[A-Za-z][A-Za-z0-9_]*\.com$' order by user_id diff --git a/src/main/kotlin/g3401_3500/s3461_check_if_digits_are_equal_in_string_after_operations_i/Solution.kt b/src/main/kotlin/g3401_3500/s3461_check_if_digits_are_equal_in_string_after_operations_i/Solution.kt index 9de4a917e..271369539 100644 --- a/src/main/kotlin/g3401_3500/s3461_check_if_digits_are_equal_in_string_after_operations_i/Solution.kt +++ b/src/main/kotlin/g3401_3500/s3461_check_if_digits_are_equal_in_string_after_operations_i/Solution.kt @@ -1,20 +1,22 @@ package g3401_3500.s3461_check_if_digits_are_equal_in_string_after_operations_i -// #Easy #2025_02_23_Time_8_ms_(100.00%)_Space_37.90_MB_(100.00%) +// #Easy #String #Math #Simulation #Number_Theory #Combinatorics +// #2025_02_25_Time_3_ms_(100.00%)_Space_35.54_MB_(100.00%) class Solution { fun hasSameDigits(s: String): Boolean { - var s = s - var n = s.length - while (n > 2) { - val nstr = StringBuilder() - for (i in 1..(Collections.reverseOrder()) - var temp: PriorityQueue? + var temp: PriorityQueue for (i in grid.indices) { temp = PriorityQueue(Collections.reverseOrder()) for (j in grid[i].indices) { diff --git a/src/main/kotlin/g3401_3500/s3463_check_if_digits_are_equal_in_string_after_operations_ii/Solution.kt b/src/main/kotlin/g3401_3500/s3463_check_if_digits_are_equal_in_string_after_operations_ii/Solution.kt index 8924e5081..ac23bb66a 100644 --- a/src/main/kotlin/g3401_3500/s3463_check_if_digits_are_equal_in_string_after_operations_ii/Solution.kt +++ b/src/main/kotlin/g3401_3500/s3463_check_if_digits_are_equal_in_string_after_operations_ii/Solution.kt @@ -1,57 +1,76 @@ package g3401_3500.s3463_check_if_digits_are_equal_in_string_after_operations_ii -// #Hard #2025_02_23_Time_63_ms_(100.00%)_Space_44.31_MB_(100.00%) +// #Hard #String #Math #Number_Theory #Combinatorics +// #2025_02_25_Time_38_ms_(100.00%)_Space_45.90_MB_(11.11%) class Solution { - fun hasSameDigits(s: String): Boolean { - val n = s.length - val nMunus2 = n - 2 - var f0 = 0 - var f1 = 0 - for (j in 0..nMunus2) { - val c = binomMod10(nMunus2, j) - f0 = (f0 + c * (s[j].code - '0'.code)) % 10 - f1 = (f1 + c * (s[j + 1].code - '0'.code)) % 10 + private fun powMod10(a: Int, n: Int): Int { + var a = a + var n = n + var x = 1 + while (n >= 1) { + if (n % 2 == 1) { + x = (x * a) % 10 + } + a = (a * a) % 10 + n /= 2 } - return f0 == f1 + return x } - private fun binomMod10(n: Int, k: Int): Int { - val r2 = binomMod2(n, k) - val r5 = binomMod5(n, k) - for (x in 0..9) { - if (x % 2 == r2 && x % 5 == r5) { - return x + private fun f(n: Int): IntArray { + val ns = IntArray(n + 1) + val n2 = IntArray(n + 1) + val n5 = IntArray(n + 1) + ns[0] = 1 + for (i in 1..n) { + var m = i + n2[i] = n2[i - 1] + n5[i] = n5[i - 1] + while (m % 2 == 0) { + m /= 2 + n2[i]++ } + while (m % 5 == 0) { + m /= 5 + n5[i]++ + } + ns[i] = (ns[i - 1] * m) % 10 } - return 0 - } - - private fun binomMod2(n: Int, k: Int): Int { - return if ((n and k) == k) 1 else 0 + val inv = IntArray(10) + for (i in 1..9) { + for (j in 0..9) { + if (i * j % 10 == 1) { + inv[i] = j + } + } + } + val xs = IntArray(n + 1) + for (k in 0..n) { + var a = 0 + val s2 = n2[n] - n2[n - k] - n2[k] + val s5 = n5[n] - n5[n - k] - n5[k] + if (s2 == 0 || s5 == 0) { + a = (ns[n] * inv[ns[n - k]] * inv[ns[k]] * powMod10(2, s2) * powMod10(5, s5)) % 10 + } + xs[k] = a + } + return xs } - private fun binomMod5(n: Int, k: Int): Int { - var n = n - var k = k - val t = arrayOf( - intArrayOf(1), - intArrayOf(1, 1), - intArrayOf(1, 2, 1), - intArrayOf(1, 3, 3, 1), - intArrayOf(1, 4, 1, 4, 1), - ) - var res = 1 - while (n > 0 || k > 0) { - val nd = n % 5 - val kd = k % 5 - if (kd > nd) { - return 0 - } - res = (res * t[nd][kd]) % 5 - n /= 5 - k /= 5 + fun hasSameDigits(s: String): Boolean { + val n = s.length + val xs = f(n - 2) + val arr = IntArray(n) + for (i in 0.., requiredPoints: Int): Int { - val perimeter = 4L * sideLength - val numPoints = points.size - val mappedPositions = LongArray(numPoints) - for (i in 0.., k: Int): Int { + val n = points.size + val p = LongArray(n) + for (i in 0..= right) { - return false + private fun check(d: Int, dArr: LongArray, n: Int, k: Int, c: Long): Boolean { + val len = dArr.size + val nxt = IntArray(len) + var j = 0 + for (i in 0..= target) { - right = mid - } else { - left = mid + 1 + for (i in 0..= i + n) { + break + } + cur = nx + cnt++ + } + if (cnt == k && (dArr[i] + c - dArr[cur]) >= d) { + return true } } - return left + return false } } From 832d035a1dbf3693e45a15da5e64b7b1bc1e772a Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Tue, 25 Feb 2025 03:56:53 +0200 Subject: [PATCH 4/5] Fixed sonar --- .../Solution.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/kotlin/g3401_3500/s3463_check_if_digits_are_equal_in_string_after_operations_ii/Solution.kt b/src/main/kotlin/g3401_3500/s3463_check_if_digits_are_equal_in_string_after_operations_ii/Solution.kt index ac23bb66a..a6dfde70a 100644 --- a/src/main/kotlin/g3401_3500/s3463_check_if_digits_are_equal_in_string_after_operations_ii/Solution.kt +++ b/src/main/kotlin/g3401_3500/s3463_check_if_digits_are_equal_in_string_after_operations_ii/Solution.kt @@ -63,7 +63,7 @@ class Solution { val xs = f(n - 2) val arr = IntArray(n) for (i in 0.. Date: Tue, 25 Feb 2025 04:00:46 +0200 Subject: [PATCH 5/5] Improved task --- .../Solution.kt | 38 ++++++++----------- 1 file changed, 16 insertions(+), 22 deletions(-) diff --git a/src/main/kotlin/g3401_3500/s3462_maximum_sum_with_at_most_k_elements/Solution.kt b/src/main/kotlin/g3401_3500/s3462_maximum_sum_with_at_most_k_elements/Solution.kt index a5029e8c5..2ac8e7d3a 100644 --- a/src/main/kotlin/g3401_3500/s3462_maximum_sum_with_at_most_k_elements/Solution.kt +++ b/src/main/kotlin/g3401_3500/s3462_maximum_sum_with_at_most_k_elements/Solution.kt @@ -1,35 +1,29 @@ package g3401_3500.s3462_maximum_sum_with_at_most_k_elements // #Medium #Array #Sorting #Greedy #Matrix #Heap_(Priority_Queue) -// #2025_02_25_Time_197_ms_(86.21%)_Space_106.34_MB_(6.90%) - -import java.util.Collections -import java.util.PriorityQueue +// #2025_02_25_Time_139_ms_(100.00%)_Space_88.84_MB_(79.31%) class Solution { fun maxSum(grid: Array, limits: IntArray, k: Int): Long { - if (grid.isEmpty()) { - return 0 + var l = 0 + for (i in limits.indices) { + l += limits[i] } - val pq = PriorityQueue(Collections.reverseOrder()) - var temp: PriorityQueue + val dp = IntArray(l) + var a = 0 for (i in grid.indices) { - temp = PriorityQueue(Collections.reverseOrder()) - for (j in grid[i].indices) { - temp.add(grid[i][j]) - } - var cnt = 0 - while (temp.isNotEmpty() && cnt < limits[i]) { - pq.add(temp.poll()) - cnt += 1 + val lim = limits[i] + grid[i].sort() + for (j in grid[i].size - lim..