From 6a7942a3b27b13005a12e6eab14bca9b176dac5a Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Sun, 4 May 2025 17:07:27 +0300 Subject: [PATCH 1/5] Added tasks 3536-3539 --- .../Solution.kt | 28 ++++++ .../readme.md | 49 ++++++++++ .../s3537_fill_a_special_grid/Solution.kt | 33 +++++++ .../s3537_fill_a_special_grid/readme.md | 67 ++++++++++++++ .../Solution.kt | 48 ++++++++++ .../readme.md | 71 +++++++++++++++ .../Solution.kt | 91 +++++++++++++++++++ .../readme.md | 55 +++++++++++ .../SolutionTest.kt | 22 +++++ .../s3537_fill_a_special_grid/SolutionTest.kt | 38 ++++++++ .../SolutionTest.kt | 31 +++++++ .../SolutionTest.kt | 28 ++++++ 12 files changed, 561 insertions(+) create mode 100644 src/main/kotlin/g3501_3600/s3536_maximum_product_of_two_digits/Solution.kt create mode 100644 src/main/kotlin/g3501_3600/s3536_maximum_product_of_two_digits/readme.md create mode 100644 src/main/kotlin/g3501_3600/s3537_fill_a_special_grid/Solution.kt create mode 100644 src/main/kotlin/g3501_3600/s3537_fill_a_special_grid/readme.md create mode 100644 src/main/kotlin/g3501_3600/s3538_merge_operations_for_minimum_travel_time/Solution.kt create mode 100644 src/main/kotlin/g3501_3600/s3538_merge_operations_for_minimum_travel_time/readme.md create mode 100644 src/main/kotlin/g3501_3600/s3539_find_sum_of_array_product_of_magical_sequences/Solution.kt create mode 100644 src/main/kotlin/g3501_3600/s3539_find_sum_of_array_product_of_magical_sequences/readme.md create mode 100644 src/test/kotlin/g3501_3600/s3536_maximum_product_of_two_digits/SolutionTest.kt create mode 100644 src/test/kotlin/g3501_3600/s3537_fill_a_special_grid/SolutionTest.kt create mode 100644 src/test/kotlin/g3501_3600/s3538_merge_operations_for_minimum_travel_time/SolutionTest.kt create mode 100644 src/test/kotlin/g3501_3600/s3539_find_sum_of_array_product_of_magical_sequences/SolutionTest.kt diff --git a/src/main/kotlin/g3501_3600/s3536_maximum_product_of_two_digits/Solution.kt b/src/main/kotlin/g3501_3600/s3536_maximum_product_of_two_digits/Solution.kt new file mode 100644 index 000000000..01dfff875 --- /dev/null +++ b/src/main/kotlin/g3501_3600/s3536_maximum_product_of_two_digits/Solution.kt @@ -0,0 +1,28 @@ +package g3501_3600.s3536_maximum_product_of_two_digits + +// #Easy #2025_05_04_Time_1_ms_(100.00%)_Space_40.93_MB_(100.00%) + +class Solution { + fun maxProduct(n: Int): Int { + var n = n + var m1 = n % 10 + n /= 10 + var m2 = n % 10 + n /= 10 + while (n > 0) { + val a = n % 10 + if (a > m1) { + if (m1 > m2) { + m2 = m1 + } + m1 = a + } else { + if (a > m2) { + m2 = a + } + } + n /= 10 + } + return m1 * m2 + } +} diff --git a/src/main/kotlin/g3501_3600/s3536_maximum_product_of_two_digits/readme.md b/src/main/kotlin/g3501_3600/s3536_maximum_product_of_two_digits/readme.md new file mode 100644 index 000000000..11f945253 --- /dev/null +++ b/src/main/kotlin/g3501_3600/s3536_maximum_product_of_two_digits/readme.md @@ -0,0 +1,49 @@ +3536\. Maximum Product of Two Digits + +Easy + +You are given a positive integer `n`. + +Return the **maximum** product of any two digits in `n`. + +**Note:** You may use the **same** digit twice if it appears more than once in `n`. + +**Example 1:** + +**Input:** n = 31 + +**Output:** 3 + +**Explanation:** + +* The digits of `n` are `[3, 1]`. +* The possible products of any two digits are: `3 * 1 = 3`. +* The maximum product is 3. + +**Example 2:** + +**Input:** n = 22 + +**Output:** 4 + +**Explanation:** + +* The digits of `n` are `[2, 2]`. +* The possible products of any two digits are: `2 * 2 = 4`. +* The maximum product is 4. + +**Example 3:** + +**Input:** n = 124 + +**Output:** 8 + +**Explanation:** + +* The digits of `n` are `[1, 2, 4]`. +* The possible products of any two digits are: `1 * 2 = 2`, `1 * 4 = 4`, `2 * 4 = 8`. +* The maximum product is 8. + +**Constraints:** + +* 10 <= n <= 109 \ No newline at end of file diff --git a/src/main/kotlin/g3501_3600/s3537_fill_a_special_grid/Solution.kt b/src/main/kotlin/g3501_3600/s3537_fill_a_special_grid/Solution.kt new file mode 100644 index 000000000..a5ff7d47e --- /dev/null +++ b/src/main/kotlin/g3501_3600/s3537_fill_a_special_grid/Solution.kt @@ -0,0 +1,33 @@ +package g3501_3600.s3537_fill_a_special_grid + +// #Medium #2025_05_04_Time_2_ms_(100.00%)_Space_88.71_MB_(61.54%) + +import kotlin.math.pow + +class Solution { + fun specialGrid(n: Int): Array { + if (n == 0) { + return arrayOf(intArrayOf(0)) + } + val len = 2.0.pow(n.toDouble()).toInt() + val ans = Array(len) { IntArray(len) } + val num = intArrayOf(2.0.pow(2.0 * n).toInt() - 1) + backtrack(ans, len, len, 0, 0, num) + return ans + } + + private fun backtrack(ans: Array, m: Int, n: Int, x: Int, y: Int, num: IntArray) { + if (m == 2 && n == 2) { + ans[x][y] = num[0] + ans[x + 1][y] = num[0] - 1 + ans[x + 1][y + 1] = num[0] - 2 + ans[x][y + 1] = num[0] - 3 + num[0] -= 4 + return + } + backtrack(ans, m / 2, n / 2, x, y, num) + backtrack(ans, m / 2, n / 2, x + m / 2, y, num) + backtrack(ans, m / 2, n / 2, x + m / 2, y + n / 2, num) + backtrack(ans, m / 2, n / 2, x, y + n / 2, num) + } +} diff --git a/src/main/kotlin/g3501_3600/s3537_fill_a_special_grid/readme.md b/src/main/kotlin/g3501_3600/s3537_fill_a_special_grid/readme.md new file mode 100644 index 000000000..e0ee432a9 --- /dev/null +++ b/src/main/kotlin/g3501_3600/s3537_fill_a_special_grid/readme.md @@ -0,0 +1,67 @@ +3537\. Fill a Special Grid + +Medium + +You are given a non-negative integer `n` representing a 2n x 2n grid. You must fill the grid with integers from 0 to 22n - 1 to make it **special**. A grid is **special** if it satisfies **all** the following conditions: + +* All numbers in the top-right quadrant are smaller than those in the bottom-right quadrant. +* All numbers in the bottom-right quadrant are smaller than those in the bottom-left quadrant. +* All numbers in the bottom-left quadrant are smaller than those in the top-left quadrant. +* Each of its quadrants is also a special grid. + +Return the **special** 2n x 2n grid. + +**Note**: Any 1x1 grid is special. + +**Example 1:** + +**Input:** n = 0 + +**Output:** [[0]] + +**Explanation:** + +The only number that can be placed is 0, and there is only one possible position in the grid. + +**Example 2:** + +**Input:** n = 1 + +**Output:** [[3,0],[2,1]] + +**Explanation:** + +The numbers in each quadrant are: + +* Top-right: 0 +* Bottom-right: 1 +* Bottom-left: 2 +* Top-left: 3 + +Since `0 < 1 < 2 < 3`, this satisfies the given constraints. + +**Example 3:** + +**Input:** n = 2 + +**Output:** [[15,12,3,0],[14,13,2,1],[11,8,7,4],[10,9,6,5]] + +**Explanation:** + +![](https://assets.leetcode.com/uploads/2025/03/05/4123example3p1drawio.png) + +The numbers in each quadrant are: + +* Top-right: 3, 0, 2, 1 +* Bottom-right: 7, 4, 6, 5 +* Bottom-left: 11, 8, 10, 9 +* Top-left: 15, 12, 14, 13 +* `max(3, 0, 2, 1) < min(7, 4, 6, 5)` +* `max(7, 4, 6, 5) < min(11, 8, 10, 9)` +* `max(11, 8, 10, 9) < min(15, 12, 14, 13)` + +This satisfies the first three requirements. Additionally, each quadrant is also a special grid. Thus, this is a special grid. + +**Constraints:** + +* `0 <= n <= 10` \ No newline at end of file diff --git a/src/main/kotlin/g3501_3600/s3538_merge_operations_for_minimum_travel_time/Solution.kt b/src/main/kotlin/g3501_3600/s3538_merge_operations_for_minimum_travel_time/Solution.kt new file mode 100644 index 000000000..30fdabcdf --- /dev/null +++ b/src/main/kotlin/g3501_3600/s3538_merge_operations_for_minimum_travel_time/Solution.kt @@ -0,0 +1,48 @@ +package g3501_3600.s3538_merge_operations_for_minimum_travel_time + +// #Hard #2025_05_04_Time_10_ms_(100.00%)_Space_46.96_MB_(100.00%) + +import kotlin.math.min + +@Suppress("unused") +class Solution { + fun minTravelTime(l: Int, n: Int, k: Int, position: IntArray, time: IntArray): Int { + val dp = Array>(n) { Array(k + 1) { IntArray(k + 1) } } + for (i in 0.. 0` and `i + 1 < n`) and: + +* Update the sign at index `i + 1` so that its time becomes `time[i] + time[i + 1]`. +* Remove the sign at index `i`. + +Return the **minimum** **total** **travel time** (in minutes) to travel from 0 to `l` after **exactly** `k` merges. + +**Example 1:** + +**Input:** l = 10, n = 4, k = 1, position = [0,3,8,10], time = [5,8,3,6] + +**Output:** 62 + +**Explanation:** + +* Merge the signs at indices 1 and 2. Remove the sign at index 1, and change the time at index 2 to `8 + 3 = 11`. + +* After the merge: + * `position` array: `[0, 8, 10]` + * `time` array: `[5, 11, 6]` + +| Segment | Distance (km) | Time per km (min) | Segment Travel Time (min) | +|-----------|---------------|-------------------|----------------------------| +| 0 → 8 | 8 | 5 | 8 × 5 = 40 | +| 8 → 10 | 2 | 11 | 2 × 11 = 22 | + + +* Total Travel Time: `40 + 22 = 62`, which is the minimum possible time after exactly 1 merge. + +**Example 2:** + +**Input:** l = 5, n = 5, k = 1, position = [0,1,2,3,5], time = [8,3,9,3,3] + +**Output:** 34 + +**Explanation:** + +* Merge the signs at indices 1 and 2. Remove the sign at index 1, and change the time at index 2 to `3 + 9 = 12`. +* After the merge: + * `position` array: `[0, 2, 3, 5]` + * `time` array: `[8, 12, 3, 3]` + +| Segment | Distance (km) | Time per km (min) | Segment Travel Time (min) | +|-----------|---------------|-------------------|----------------------------| +| 0 → 2 | 2 | 8 | 2 × 8 = 16 | +| 2 → 3 | 1 | 12 | 1 × 12 = 12 | +| 3 → 5 | 2 | 3 | 2 × 3 = 6 | + +* Total Travel Time: `16 + 12 + 6 = 34`**,** which is the minimum possible time after exactly 1 merge. + +**Constraints:** + +* 1 <= l <= 105 +* `2 <= n <= min(l + 1, 50)` +* `0 <= k <= min(n - 2, 10)` +* `position.length == n` +* `position[0] = 0` and `position[n - 1] = l` +* `position` is sorted in strictly increasing order. +* `time.length == n` +* `1 <= time[i] <= 100` +* `1 <= sum(time) <= 100` \ No newline at end of file diff --git a/src/main/kotlin/g3501_3600/s3539_find_sum_of_array_product_of_magical_sequences/Solution.kt b/src/main/kotlin/g3501_3600/s3539_find_sum_of_array_product_of_magical_sequences/Solution.kt new file mode 100644 index 000000000..23efcf986 --- /dev/null +++ b/src/main/kotlin/g3501_3600/s3539_find_sum_of_array_product_of_magical_sequences/Solution.kt @@ -0,0 +1,91 @@ +package g3501_3600.s3539_find_sum_of_array_product_of_magical_sequences + +// #Hard #2025_05_04_Time_58_ms_(_%)_Space_49.73_MB_(_%) + +class Solution { + fun magicalSum(m: Int, k: Int, nums: IntArray): Int { + val n = nums.size + val pow = Array(n) { LongArray(m + 1) } + for (j in 0..>(m + 1) { Array(k + 1) { LongArray(m + 1) } } + var next = Array>(m + 1) { Array(k + 1) { LongArray(m + 1) } } + dp[0][0][0] = 1L + for (i in 0.. k) { + continue + } + next[t + cc][o + (total and 1)][total ushr 1] = + ( + ( + next[t + cc][o + (total and 1)][total ushr 1] + + dp[t][o][c] * + C[m - t][cc] % + MOD + * pow[i][cc] % + MOD + ) % + MOD + ) + } + } + } + } + val tmp = dp + dp = next + next = tmp + } + var res: Long = 0 + for (o in 0..k) { + for (c in 0..m) { + if (o + P[c] == k) { + res = (res + dp[m][o][c]) % MOD + } + } + } + return res.toInt() + } + + companion object { + private const val MOD = 1000000007 + private val C: Array = precomputeBinom(31) + private val P: IntArray = precomputePop(31) + + private fun precomputeBinom(max: Int): Array { + val res = Array(max) { IntArray(max) } + for (i in 0..2seq[0] + 2seq[1] + ... + 2seq[m - 1] has `k` **set bits**. + +The **array product** of this sequence is defined as `prod(seq) = (nums[seq[0]] * nums[seq[1]] * ... * nums[seq[m - 1]])`. + +Return the **sum** of the **array products** for all valid **magical** sequences. + +Since the answer may be large, return it **modulo** 109 + 7. + +A **set bit** refers to a bit in the binary representation of a number that has a value of 1. + +**Example 1:** + +**Input:** m = 5, k = 5, nums = [1,10,100,10000,1000000] + +**Output:** 991600007 + +**Explanation:** + +All permutations of `[0, 1, 2, 3, 4]` are magical sequences, each with an array product of 1013. + +**Example 2:** + +**Input:** m = 2, k = 2, nums = [5,4,3,2,1] + +**Output:** 170 + +**Explanation:** + +The magical sequences are `[0, 1]`, `[0, 2]`, `[0, 3]`, `[0, 4]`, `[1, 0]`, `[1, 2]`, `[1, 3]`, `[1, 4]`, `[2, 0]`, `[2, 1]`, `[2, 3]`, `[2, 4]`, `[3, 0]`, `[3, 1]`, `[3, 2]`, `[3, 4]`, `[4, 0]`, `[4, 1]`, `[4, 2]`, and `[4, 3]`. + +**Example 3:** + +**Input:** m = 1, k = 1, nums = [28] + +**Output:** 28 + +**Explanation:** + +The only magical sequence is `[0]`. + +**Constraints:** + +* `1 <= k <= m <= 30` +* `1 <= nums.length <= 50` +* 1 <= nums[i] <= 108 \ No newline at end of file diff --git a/src/test/kotlin/g3501_3600/s3536_maximum_product_of_two_digits/SolutionTest.kt b/src/test/kotlin/g3501_3600/s3536_maximum_product_of_two_digits/SolutionTest.kt new file mode 100644 index 000000000..fd6d8d943 --- /dev/null +++ b/src/test/kotlin/g3501_3600/s3536_maximum_product_of_two_digits/SolutionTest.kt @@ -0,0 +1,22 @@ +package g3501_3600.s3536_maximum_product_of_two_digits + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun maxProduct() { + assertThat(Solution().maxProduct(31), equalTo(3)) + } + + @Test + fun maxProduct2() { + assertThat(Solution().maxProduct(22), equalTo(4)) + } + + @Test + fun maxProduct3() { + assertThat(Solution().maxProduct(124), equalTo(8)) + } +} diff --git a/src/test/kotlin/g3501_3600/s3537_fill_a_special_grid/SolutionTest.kt b/src/test/kotlin/g3501_3600/s3537_fill_a_special_grid/SolutionTest.kt new file mode 100644 index 000000000..61faf1519 --- /dev/null +++ b/src/test/kotlin/g3501_3600/s3537_fill_a_special_grid/SolutionTest.kt @@ -0,0 +1,38 @@ +package g3501_3600.s3537_fill_a_special_grid + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun specialGrid() { + assertThat>( + Solution().specialGrid(0), + equalTo>(arrayOf(intArrayOf(0))), + ) + } + + @Test + fun specialGrid2() { + assertThat>( + Solution().specialGrid(1), + equalTo>(arrayOf(intArrayOf(3, 0), intArrayOf(2, 1))), + ) + } + + @Test + fun specialGrid3() { + assertThat>( + Solution().specialGrid(2), + equalTo>( + arrayOf( + intArrayOf(15, 12, 3, 0), + intArrayOf(14, 13, 2, 1), + intArrayOf(11, 8, 7, 4), + intArrayOf(10, 9, 6, 5), + ), + ), + ) + } +} diff --git a/src/test/kotlin/g3501_3600/s3538_merge_operations_for_minimum_travel_time/SolutionTest.kt b/src/test/kotlin/g3501_3600/s3538_merge_operations_for_minimum_travel_time/SolutionTest.kt new file mode 100644 index 000000000..00be368ff --- /dev/null +++ b/src/test/kotlin/g3501_3600/s3538_merge_operations_for_minimum_travel_time/SolutionTest.kt @@ -0,0 +1,31 @@ +package g3501_3600.s3538_merge_operations_for_minimum_travel_time + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun minTravelTime() { + assertThat( + Solution() + .minTravelTime(10, 4, 1, intArrayOf(0, 3, 8, 10), intArrayOf(5, 8, 3, 6)), + equalTo(62), + ) + } + + @Test + fun minTravelTime2() { + assertThat( + Solution() + .minTravelTime( + 5, + 5, + 1, + intArrayOf(0, 1, 2, 3, 5), + intArrayOf(8, 3, 9, 3, 3), + ), + equalTo(34), + ) + } +} diff --git a/src/test/kotlin/g3501_3600/s3539_find_sum_of_array_product_of_magical_sequences/SolutionTest.kt b/src/test/kotlin/g3501_3600/s3539_find_sum_of_array_product_of_magical_sequences/SolutionTest.kt new file mode 100644 index 000000000..dd57b7af2 --- /dev/null +++ b/src/test/kotlin/g3501_3600/s3539_find_sum_of_array_product_of_magical_sequences/SolutionTest.kt @@ -0,0 +1,28 @@ +package g3501_3600.s3539_find_sum_of_array_product_of_magical_sequences + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun magicalSum() { + assertThat( + Solution().magicalSum(5, 5, intArrayOf(1, 10, 100, 10000, 1000000)), + equalTo(991600007), + ) + } + + @Test + fun magicalSum2() { + assertThat( + Solution().magicalSum(2, 2, intArrayOf(5, 4, 3, 2, 1)), + equalTo(170), + ) + } + + @Test + fun magicalSum3() { + assertThat(Solution().magicalSum(1, 1, intArrayOf(28)), equalTo(28)) + } +} From 619442a8be8363d77908c44548ea20a2cd9d9089 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Sun, 4 May 2025 18:03:31 +0300 Subject: [PATCH 2/5] Added tests --- .../SolutionTest.kt | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/test/kotlin/g3501_3600/s3536_maximum_product_of_two_digits/SolutionTest.kt b/src/test/kotlin/g3501_3600/s3536_maximum_product_of_two_digits/SolutionTest.kt index fd6d8d943..561984792 100644 --- a/src/test/kotlin/g3501_3600/s3536_maximum_product_of_two_digits/SolutionTest.kt +++ b/src/test/kotlin/g3501_3600/s3536_maximum_product_of_two_digits/SolutionTest.kt @@ -19,4 +19,19 @@ internal class SolutionTest { fun maxProduct3() { assertThat(Solution().maxProduct(124), equalTo(8)) } + + @Test + fun maxProduct4() { + assertThat(Solution().maxProduct(453), equalTo(20)) + } + + @Test + fun maxProduct5() { + assertThat(Solution().maxProduct(437), equalTo(28)) + } + + @Test + fun maxProduct6() { + assertThat(Solution().maxProduct(724), equalTo(28)) + } } From 9abc5aa3a3294b223fc75cfac672b93544a29724 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Tue, 6 May 2025 08:47:02 +0300 Subject: [PATCH 3/5] Updated tags --- .../g3501_3600/s3536_maximum_product_of_two_digits/Solution.kt | 2 +- .../kotlin/g3501_3600/s3537_fill_a_special_grid/Solution.kt | 3 ++- .../s3538_merge_operations_for_minimum_travel_time/Solution.kt | 3 ++- .../Solution.kt | 3 ++- 4 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/main/kotlin/g3501_3600/s3536_maximum_product_of_two_digits/Solution.kt b/src/main/kotlin/g3501_3600/s3536_maximum_product_of_two_digits/Solution.kt index 01dfff875..ca91a3beb 100644 --- a/src/main/kotlin/g3501_3600/s3536_maximum_product_of_two_digits/Solution.kt +++ b/src/main/kotlin/g3501_3600/s3536_maximum_product_of_two_digits/Solution.kt @@ -1,6 +1,6 @@ package g3501_3600.s3536_maximum_product_of_two_digits -// #Easy #2025_05_04_Time_1_ms_(100.00%)_Space_40.93_MB_(100.00%) +// #Easy #Math #Sorting #2025_05_04_Time_1_ms_(100.00%)_Space_40.93_MB_(100.00%) class Solution { fun maxProduct(n: Int): Int { diff --git a/src/main/kotlin/g3501_3600/s3537_fill_a_special_grid/Solution.kt b/src/main/kotlin/g3501_3600/s3537_fill_a_special_grid/Solution.kt index a5ff7d47e..a2612ba3c 100644 --- a/src/main/kotlin/g3501_3600/s3537_fill_a_special_grid/Solution.kt +++ b/src/main/kotlin/g3501_3600/s3537_fill_a_special_grid/Solution.kt @@ -1,6 +1,7 @@ package g3501_3600.s3537_fill_a_special_grid -// #Medium #2025_05_04_Time_2_ms_(100.00%)_Space_88.71_MB_(61.54%) +// #Medium #Array #Matrix #Divide_and_Conquer +// #2025_05_04_Time_2_ms_(100.00%)_Space_88.71_MB_(61.54%) import kotlin.math.pow diff --git a/src/main/kotlin/g3501_3600/s3538_merge_operations_for_minimum_travel_time/Solution.kt b/src/main/kotlin/g3501_3600/s3538_merge_operations_for_minimum_travel_time/Solution.kt index 30fdabcdf..8d84a2522 100644 --- a/src/main/kotlin/g3501_3600/s3538_merge_operations_for_minimum_travel_time/Solution.kt +++ b/src/main/kotlin/g3501_3600/s3538_merge_operations_for_minimum_travel_time/Solution.kt @@ -1,6 +1,7 @@ package g3501_3600.s3538_merge_operations_for_minimum_travel_time -// #Hard #2025_05_04_Time_10_ms_(100.00%)_Space_46.96_MB_(100.00%) +// #Hard #Array #Dynamic_Programming #Prefix_Sum +// #2025_05_04_Time_10_ms_(100.00%)_Space_46.96_MB_(100.00%) import kotlin.math.min diff --git a/src/main/kotlin/g3501_3600/s3539_find_sum_of_array_product_of_magical_sequences/Solution.kt b/src/main/kotlin/g3501_3600/s3539_find_sum_of_array_product_of_magical_sequences/Solution.kt index 23efcf986..9ec80549d 100644 --- a/src/main/kotlin/g3501_3600/s3539_find_sum_of_array_product_of_magical_sequences/Solution.kt +++ b/src/main/kotlin/g3501_3600/s3539_find_sum_of_array_product_of_magical_sequences/Solution.kt @@ -1,6 +1,7 @@ package g3501_3600.s3539_find_sum_of_array_product_of_magical_sequences -// #Hard #2025_05_04_Time_58_ms_(_%)_Space_49.73_MB_(_%) +// #Hard #Array #Dynamic_Programming #Math #Bit_Manipulation #Bitmask #Combinatorics +// #2025_05_04_Time_58_ms_(_%)_Space_49.73_MB_(_%) class Solution { fun magicalSum(m: Int, k: Int, nums: IntArray): Int { From c0f06b1f0ed432883528361eaf1119391fe9d47a Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Tue, 6 May 2025 08:59:37 +0300 Subject: [PATCH 4/5] Improved task --- .../Solution.kt | 54 +++++++++---------- 1 file changed, 26 insertions(+), 28 deletions(-) diff --git a/src/main/kotlin/g3501_3600/s3539_find_sum_of_array_product_of_magical_sequences/Solution.kt b/src/main/kotlin/g3501_3600/s3539_find_sum_of_array_product_of_magical_sequences/Solution.kt index 9ec80549d..b2c990054 100644 --- a/src/main/kotlin/g3501_3600/s3539_find_sum_of_array_product_of_magical_sequences/Solution.kt +++ b/src/main/kotlin/g3501_3600/s3539_find_sum_of_array_product_of_magical_sequences/Solution.kt @@ -1,16 +1,20 @@ package g3501_3600.s3539_find_sum_of_array_product_of_magical_sequences // #Hard #Array #Dynamic_Programming #Math #Bit_Manipulation #Bitmask #Combinatorics -// #2025_05_04_Time_58_ms_(_%)_Space_49.73_MB_(_%) +// #2025_05_06_Time_60_ms_(100.00%)_Space_48.98_MB_(100.00%) class Solution { + private val mod = 1000000007 + private val c: Array = precomputeBinom(31) + private val p: IntArray = precomputePop(31) + fun magicalSum(m: Int, k: Int, nums: IntArray): Int { val n = nums.size val pow = Array(n) { LongArray(m + 1) } for (j in 0..>(m + 1) { Array(k + 1) { LongArray(m + 1) } } @@ -38,12 +42,12 @@ class Solution { ( next[t + cc][o + (total and 1)][total ushr 1] + dp[t][o][c] * - C[m - t][cc] % - MOD + this@Solution.c[m - t][cc] % + mod * pow[i][cc] % - MOD + mod ) % - MOD + mod ) } } @@ -56,37 +60,31 @@ class Solution { var res: Long = 0 for (o in 0..k) { for (c in 0..m) { - if (o + P[c] == k) { - res = (res + dp[m][o][c]) % MOD + if (o + p[c] == k) { + res = (res + dp[m][o][c]) % mod } } } return res.toInt() } - companion object { - private const val MOD = 1000000007 - private val C: Array = precomputeBinom(31) - private val P: IntArray = precomputePop(31) - - private fun precomputeBinom(max: Int): Array { - val res = Array(max) { IntArray(max) } - for (i in 0.. { + val res = Array(max) { IntArray(max) } + for (i in 0.. Date: Tue, 6 May 2025 09:03:09 +0300 Subject: [PATCH 5/5] Improved task --- .../Solution.kt | 52 ++++++++++--------- 1 file changed, 27 insertions(+), 25 deletions(-) diff --git a/src/main/kotlin/g3501_3600/s3539_find_sum_of_array_product_of_magical_sequences/Solution.kt b/src/main/kotlin/g3501_3600/s3539_find_sum_of_array_product_of_magical_sequences/Solution.kt index b2c990054..255af8e89 100644 --- a/src/main/kotlin/g3501_3600/s3539_find_sum_of_array_product_of_magical_sequences/Solution.kt +++ b/src/main/kotlin/g3501_3600/s3539_find_sum_of_array_product_of_magical_sequences/Solution.kt @@ -4,17 +4,13 @@ package g3501_3600.s3539_find_sum_of_array_product_of_magical_sequences // #2025_05_06_Time_60_ms_(100.00%)_Space_48.98_MB_(100.00%) class Solution { - private val mod = 1000000007 - private val c: Array = precomputeBinom(31) - private val p: IntArray = precomputePop(31) - fun magicalSum(m: Int, k: Int, nums: IntArray): Int { val n = nums.size val pow = Array(n) { LongArray(m + 1) } for (j in 0..>(m + 1) { Array(k + 1) { LongArray(m + 1) } } @@ -42,12 +38,12 @@ class Solution { ( next[t + cc][o + (total and 1)][total ushr 1] + dp[t][o][c] * - this@Solution.c[m - t][cc] % - mod + C[m - t][cc] % + MOD * pow[i][cc] % - mod + MOD ) % - mod + MOD ) } } @@ -60,31 +56,37 @@ class Solution { var res: Long = 0 for (o in 0..k) { for (c in 0..m) { - if (o + p[c] == k) { - res = (res + dp[m][o][c]) % mod + if (o + P[c] == k) { + res = (res + dp[m][o][c]) % MOD } } } return res.toInt() } - private fun precomputeBinom(max: Int): Array { - val res = Array(max) { IntArray(max) } - for (i in 0.. = precomputeBinom(31) + private val P: IntArray = precomputePop(31) + + private fun precomputeBinom(max: Int): Array { + val res = Array(max) { IntArray(max) } + for (i in 0..