From 594a15cabafa955b0ff2f12a8f800decc47675a9 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Wed, 28 Aug 2024 18:45:39 +0300 Subject: [PATCH] Added tasks 3264-3267 --- .../Solution.kt | 22 +++++ .../readme.md | 49 ++++++++++ .../Solution.kt | 50 +++++++++++ .../readme.md | 51 +++++++++++ .../Solution.kt | 89 +++++++++++++++++++ .../readme.md | 52 +++++++++++ .../Solution.kt | 57 ++++++++++++ .../readme.md | 49 ++++++++++ .../SolutionTest.kt | 22 +++++ .../SolutionTest.kt | 22 +++++ .../SolutionTest.kt | 23 +++++ .../SolutionTest.kt | 17 ++++ 12 files changed, 503 insertions(+) create mode 100644 src/main/kotlin/g3201_3300/s3264_final_array_state_after_k_multiplication_operations_i/Solution.kt create mode 100644 src/main/kotlin/g3201_3300/s3264_final_array_state_after_k_multiplication_operations_i/readme.md create mode 100644 src/main/kotlin/g3201_3300/s3265_count_almost_equal_pairs_i/Solution.kt create mode 100644 src/main/kotlin/g3201_3300/s3265_count_almost_equal_pairs_i/readme.md create mode 100644 src/main/kotlin/g3201_3300/s3266_final_array_state_after_k_multiplication_operations_ii/Solution.kt create mode 100644 src/main/kotlin/g3201_3300/s3266_final_array_state_after_k_multiplication_operations_ii/readme.md create mode 100644 src/main/kotlin/g3201_3300/s3267_count_almost_equal_pairs_ii/Solution.kt create mode 100644 src/main/kotlin/g3201_3300/s3267_count_almost_equal_pairs_ii/readme.md create mode 100644 src/test/kotlin/g3201_3300/s3264_final_array_state_after_k_multiplication_operations_i/SolutionTest.kt create mode 100644 src/test/kotlin/g3201_3300/s3265_count_almost_equal_pairs_i/SolutionTest.kt create mode 100644 src/test/kotlin/g3201_3300/s3266_final_array_state_after_k_multiplication_operations_ii/SolutionTest.kt create mode 100644 src/test/kotlin/g3201_3300/s3267_count_almost_equal_pairs_ii/SolutionTest.kt diff --git a/src/main/kotlin/g3201_3300/s3264_final_array_state_after_k_multiplication_operations_i/Solution.kt b/src/main/kotlin/g3201_3300/s3264_final_array_state_after_k_multiplication_operations_i/Solution.kt new file mode 100644 index 00000000..8124904b --- /dev/null +++ b/src/main/kotlin/g3201_3300/s3264_final_array_state_after_k_multiplication_operations_i/Solution.kt @@ -0,0 +1,22 @@ +package g3201_3300.s3264_final_array_state_after_k_multiplication_operations_i + +// #Easy #2024_08_28_Time_226_ms_(68.00%)_Space_38.5_MB_(66.00%) + +@Suppress("NAME_SHADOWING") +class Solution { + fun getFinalState(nums: IntArray, k: Int, multiplier: Int): IntArray { + var k = k + while (k-- > 0) { + var min = nums[0] + var index = 0 + for (i in nums.indices) { + if (min > nums[i]) { + min = nums[i] + index = i + } + } + nums[index] = nums[index] * multiplier + } + return nums + } +} diff --git a/src/main/kotlin/g3201_3300/s3264_final_array_state_after_k_multiplication_operations_i/readme.md b/src/main/kotlin/g3201_3300/s3264_final_array_state_after_k_multiplication_operations_i/readme.md new file mode 100644 index 00000000..70d68467 --- /dev/null +++ b/src/main/kotlin/g3201_3300/s3264_final_array_state_after_k_multiplication_operations_i/readme.md @@ -0,0 +1,49 @@ +3264\. Final Array State After K Multiplication Operations I + +Easy + +You are given an integer array `nums`, an integer `k`, and an integer `multiplier`. + +You need to perform `k` operations on `nums`. In each operation: + +* Find the **minimum** value `x` in `nums`. If there are multiple occurrences of the minimum value, select the one that appears **first**. +* Replace the selected minimum value `x` with `x * multiplier`. + +Return an integer array denoting the _final state_ of `nums` after performing all `k` operations. + +**Example 1:** + +**Input:** nums = [2,1,3,5,6], k = 5, multiplier = 2 + +**Output:** [8,4,6,5,6] + +**Explanation:** + +| Operation | Result | +|---------------------|------------------| +| After operation 1 | [2, 2, 3, 5, 6] | +| After operation 2 | [4, 2, 3, 5, 6] | +| After operation 3 | [4, 4, 3, 5, 6] | +| After operation 4 | [4, 4, 6, 5, 6] | +| After operation 5 | [8, 4, 6, 5, 6] | + +**Example 2:** + +**Input:** nums = [1,2], k = 3, multiplier = 4 + +**Output:** [16,8] + +**Explanation:** + +| Operation | Result | +|---------------------|-------------| +| After operation 1 | [4, 2] | +| After operation 2 | [4, 8] | +| After operation 3 | [16, 8] | + +**Constraints:** + +* `1 <= nums.length <= 100` +* `1 <= nums[i] <= 100` +* `1 <= k <= 10` +* `1 <= multiplier <= 5` \ No newline at end of file diff --git a/src/main/kotlin/g3201_3300/s3265_count_almost_equal_pairs_i/Solution.kt b/src/main/kotlin/g3201_3300/s3265_count_almost_equal_pairs_i/Solution.kt new file mode 100644 index 00000000..c71e5009 --- /dev/null +++ b/src/main/kotlin/g3201_3300/s3265_count_almost_equal_pairs_i/Solution.kt @@ -0,0 +1,50 @@ +package g3201_3300.s3265_count_almost_equal_pairs_i + +// #Medium #2024_08_28_Time_188_ms_(100.00%)_Space_37.5_MB_(100.00%) + +@Suppress("NAME_SHADOWING") +class Solution { + fun countPairs(nums: IntArray): Int { + var ans = 0 + for (i in 0 until nums.size - 1) { + for (j in i + 1 until nums.size) { + if (nums[i] == nums[j] || + ((nums[j] - nums[i]) % 9 == 0 && check(nums[i], nums[j])) + ) { + ans++ + } + } + } + return ans + } + + private fun check(a: Int, b: Int): Boolean { + var a = a + var b = b + val ca = IntArray(10) + val cb = IntArray(10) + var d = 0 + while (a > 0 || b > 0) { + if (a % 10 != b % 10) { + d++ + if (d > 2) { + return false + } + } + ca[a % 10]++ + cb[b % 10]++ + a /= 10 + b /= 10 + } + return d == 2 && areEqual(ca, cb) + } + + private fun areEqual(a: IntArray, b: IntArray): Boolean { + for (i in 0..9) { + if (a[i] != b[i]) { + return false + } + } + return true + } +} diff --git a/src/main/kotlin/g3201_3300/s3265_count_almost_equal_pairs_i/readme.md b/src/main/kotlin/g3201_3300/s3265_count_almost_equal_pairs_i/readme.md new file mode 100644 index 00000000..90aa7942 --- /dev/null +++ b/src/main/kotlin/g3201_3300/s3265_count_almost_equal_pairs_i/readme.md @@ -0,0 +1,51 @@ +3265\. Count Almost Equal Pairs I + +Medium + +You are given an array `nums` consisting of positive integers. + +We call two integers `x` and `y` in this problem **almost equal** if both integers can become equal after performing the following operation **at most once**: + +* Choose **either** `x` or `y` and swap any two digits within the chosen number. + +Return the number of indices `i` and `j` in `nums` where `i < j` such that `nums[i]` and `nums[j]` are **almost equal**. + +**Note** that it is allowed for an integer to have leading zeros after performing an operation. + +**Example 1:** + +**Input:** nums = [3,12,30,17,21] + +**Output:** 2 + +**Explanation:** + +The almost equal pairs of elements are: + +* 3 and 30. By swapping 3 and 0 in 30, you get 3. +* 12 and 21. By swapping 1 and 2 in 12, you get 21. + +**Example 2:** + +**Input:** nums = [1,1,1,1,1] + +**Output:** 10 + +**Explanation:** + +Every two elements in the array are almost equal. + +**Example 3:** + +**Input:** nums = [123,231] + +**Output:** 0 + +**Explanation:** + +We cannot swap any two digits of 123 or 231 to reach the other. + +**Constraints:** + +* `2 <= nums.length <= 100` +* 1 <= nums[i] <= 106 \ No newline at end of file diff --git a/src/main/kotlin/g3201_3300/s3266_final_array_state_after_k_multiplication_operations_ii/Solution.kt b/src/main/kotlin/g3201_3300/s3266_final_array_state_after_k_multiplication_operations_ii/Solution.kt new file mode 100644 index 00000000..0b200ceb --- /dev/null +++ b/src/main/kotlin/g3201_3300/s3266_final_array_state_after_k_multiplication_operations_ii/Solution.kt @@ -0,0 +1,89 @@ +package g3201_3300.s3266_final_array_state_after_k_multiplication_operations_ii + +// #Hard #2024_08_28_Time_546_ms_(100.00%)_Space_60.8_MB_(66.67%) + +import java.util.PriorityQueue +import kotlin.math.max + +@Suppress("NAME_SHADOWING") +class Solution { + fun getFinalState(nums: IntArray, k: Int, multiplier: Int): IntArray { + var k = k + if (multiplier == 1) { + return nums + } + val n = nums.size + var mx = 0 + for (x in nums) { + mx = max(mx, x) + } + val a = LongArray(n) + var left = k + var shouldExit = false + run { + var i = 0 + while (i < n && !shouldExit) { + var x = nums[i].toLong() + while (x < mx) { + x *= multiplier.toLong() + if (--left < 0) { + shouldExit = true + break + } + } + a[i] = x + i++ + } + } + if (left < 0) { + val pq = + PriorityQueue { p: LongArray, q: LongArray -> + if (p[0] != q[0] + ) java.lang.Long.compare(p[0], q[0]) + else java.lang.Long.compare(p[1], q[1]) + } + for (i in 0 until n) { + pq.offer(longArrayOf(nums[i].toLong(), i.toLong())) + } + while (k-- > 0) { + val p = pq.poll() + p[0] *= multiplier.toLong() + pq.offer(p) + } + while (pq.isNotEmpty()) { + val p = pq.poll() + nums[p[1].toInt()] = (p[0] % MOD).toInt() + } + return nums + } + + val ids: Array = Array(n) { i: Int -> i } + ids.sortWith { i: Int?, j: Int? -> java.lang.Long.compare(a[i!!], a[j!!]) } + k = left + val pow1 = pow(multiplier.toLong(), k / n) + val pow2 = pow1 * multiplier % MOD + for (i in 0 until n) { + val j = ids[i] + nums[j] = (a[j] % MOD * (if (i < k % n) pow2 else pow1) % MOD).toInt() + } + return nums + } + + private fun pow(x: Long, n: Int): Long { + var x = x + var n = n + var res: Long = 1 + while (n > 0) { + if (n % 2 > 0) { + res = res * x % MOD + } + x = x * x % MOD + n /= 2 + } + return res + } + + companion object { + private const val MOD = 1000000007 + } +} diff --git a/src/main/kotlin/g3201_3300/s3266_final_array_state_after_k_multiplication_operations_ii/readme.md b/src/main/kotlin/g3201_3300/s3266_final_array_state_after_k_multiplication_operations_ii/readme.md new file mode 100644 index 00000000..be086870 --- /dev/null +++ b/src/main/kotlin/g3201_3300/s3266_final_array_state_after_k_multiplication_operations_ii/readme.md @@ -0,0 +1,52 @@ +3266\. Final Array State After K Multiplication Operations II + +Hard + +You are given an integer array `nums`, an integer `k`, and an integer `multiplier`. + +You need to perform `k` operations on `nums`. In each operation: + +* Find the **minimum** value `x` in `nums`. If there are multiple occurrences of the minimum value, select the one that appears **first**. +* Replace the selected minimum value `x` with `x * multiplier`. + +After the `k` operations, apply **modulo** 109 + 7 to every value in `nums`. + +Return an integer array denoting the _final state_ of `nums` after performing all `k` operations and then applying the modulo. + +**Example 1:** + +**Input:** nums = [2,1,3,5,6], k = 5, multiplier = 2 + +**Output:** [8,4,6,5,6] + +**Explanation:** + +| Operation | Result | +|-------------------------|------------------| +| After operation 1 | [2, 2, 3, 5, 6] | +| After operation 2 | [4, 2, 3, 5, 6] | +| After operation 3 | [4, 4, 3, 5, 6] | +| After operation 4 | [4, 4, 6, 5, 6] | +| After operation 5 | [8, 4, 6, 5, 6] | +| After applying modulo | [8, 4, 6, 5, 6] | + +**Example 2:** + +**Input:** nums = [100000,2000], k = 2, multiplier = 1000000 + +**Output:** [999999307,999999993] + +**Explanation:** + +| Operation | Result | +|-------------------------|----------------------| +| After operation 1 | [100000, 2000000000] | +| After operation 2 | [100000000000, 2000000000] | +| After applying modulo | [999999307, 999999993] | + +**Constraints:** + +* 1 <= nums.length <= 104 +* 1 <= nums[i] <= 109 +* 1 <= k <= 109 +* 1 <= multiplier <= 106 \ No newline at end of file diff --git a/src/main/kotlin/g3201_3300/s3267_count_almost_equal_pairs_ii/Solution.kt b/src/main/kotlin/g3201_3300/s3267_count_almost_equal_pairs_ii/Solution.kt new file mode 100644 index 00000000..9aa72bfe --- /dev/null +++ b/src/main/kotlin/g3201_3300/s3267_count_almost_equal_pairs_ii/Solution.kt @@ -0,0 +1,57 @@ +package g3201_3300.s3267_count_almost_equal_pairs_ii + +// #Hard #2024_08_28_Time_791_ms_(100.00%)_Space_50.7_MB_(57.14%) + +class Solution { + fun countPairs(nums: IntArray): Int { + var pairs = 0 + val counts: MutableMap = HashMap() + nums.sort() + for (num in nums) { + val newNums: MutableSet = HashSet() + newNums.add(num) + var unit1 = 1 + var remain1 = num + while (remain1 > 0) { + val digit1 = num / unit1 % 10 + var unit2 = unit1 * 10 + var remain2 = remain1 / 10 + while (remain2 > 0 + ) { + val digit2 = num / unit2 % 10 + val newNum1 = + num - digit1 * unit1 - digit2 * unit2 + digit2 * unit1 + digit1 * unit2 + newNums.add(newNum1) + var unit3 = unit1 * 10 + var remain3 = remain1 / 10 + while (remain3 > 0 + ) { + val digit3 = newNum1 / unit3 % 10 + var unit4 = unit3 * 10 + var remain4 = remain3 / 10 + while (remain4 > 0 + ) { + val digit4 = newNum1 / unit4 % 10 + val newNum2 = + newNum1 - digit3 * unit3 - digit4 * unit4 + digit4 * unit3 + digit3 * unit4 + newNums.add(newNum2) + unit4 *= 10 + remain4 /= 10 + } + unit3 *= 10 + remain3 /= 10 + } + unit2 *= 10 + remain2 /= 10 + } + unit1 *= 10 + remain1 /= 10 + } + for (newNum in newNums) { + pairs += counts.getOrDefault(newNum, 0) + } + counts[num] = counts.getOrDefault(num, 0) + 1 + } + return pairs + } +} diff --git a/src/main/kotlin/g3201_3300/s3267_count_almost_equal_pairs_ii/readme.md b/src/main/kotlin/g3201_3300/s3267_count_almost_equal_pairs_ii/readme.md new file mode 100644 index 00000000..b1941676 --- /dev/null +++ b/src/main/kotlin/g3201_3300/s3267_count_almost_equal_pairs_ii/readme.md @@ -0,0 +1,49 @@ +3267\. Count Almost Equal Pairs II + +Hard + +**Attention**: In this version, the number of operations that can be performed, has been increased to **twice**. + +You are given an array `nums` consisting of positive integers. + +We call two integers `x` and `y` **almost equal** if both integers can become equal after performing the following operation **at most twice**: + +* Choose **either** `x` or `y` and swap any two digits within the chosen number. + +Return the number of indices `i` and `j` in `nums` where `i < j` such that `nums[i]` and `nums[j]` are **almost equal**. + +**Note** that it is allowed for an integer to have leading zeros after performing an operation. + +**Example 1:** + +**Input:** nums = [1023,2310,2130,213] + +**Output:** 4 + +**Explanation:** + +The almost equal pairs of elements are: + +* 1023 and 2310. By swapping the digits 1 and 2, and then the digits 0 and 3 in 1023, you get 2310. +* 1023 and 213. By swapping the digits 1 and 0, and then the digits 1 and 2 in 1023, you get 0213, which is 213. +* 2310 and 213. By swapping the digits 2 and 0, and then the digits 3 and 2 in 2310, you get 0213, which is 213. +* 2310 and 2130. By swapping the digits 3 and 1 in 2310, you get 2130. + +**Example 2:** + +**Input:** nums = [1,10,100] + +**Output:** 3 + +**Explanation:** + +The almost equal pairs of elements are: + +* 1 and 10. By swapping the digits 1 and 0 in 10, you get 01 which is 1. +* 1 and 100. By swapping the second 0 with the digit 1 in 100, you get 001, which is 1. +* 10 and 100. By swapping the first 0 with the digit 1 in 100, you get 010, which is 10. + +**Constraints:** + +* `2 <= nums.length <= 5000` +* 1 <= nums[i] < 107 \ No newline at end of file diff --git a/src/test/kotlin/g3201_3300/s3264_final_array_state_after_k_multiplication_operations_i/SolutionTest.kt b/src/test/kotlin/g3201_3300/s3264_final_array_state_after_k_multiplication_operations_i/SolutionTest.kt new file mode 100644 index 00000000..77d0a10b --- /dev/null +++ b/src/test/kotlin/g3201_3300/s3264_final_array_state_after_k_multiplication_operations_i/SolutionTest.kt @@ -0,0 +1,22 @@ +package g3201_3300.s3264_final_array_state_after_k_multiplication_operations_i + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun finalState() { + assertThat( + Solution().getFinalState(intArrayOf(2, 1, 3, 5, 6), 5, 2), + equalTo(intArrayOf(8, 4, 6, 5, 6)) + ) + } + + @Test + fun finalState2() { + assertThat( + Solution().getFinalState(intArrayOf(1, 2), 3, 4), equalTo(intArrayOf(16, 8)) + ) + } +} diff --git a/src/test/kotlin/g3201_3300/s3265_count_almost_equal_pairs_i/SolutionTest.kt b/src/test/kotlin/g3201_3300/s3265_count_almost_equal_pairs_i/SolutionTest.kt new file mode 100644 index 00000000..d65ce420 --- /dev/null +++ b/src/test/kotlin/g3201_3300/s3265_count_almost_equal_pairs_i/SolutionTest.kt @@ -0,0 +1,22 @@ +package g3201_3300.s3265_count_almost_equal_pairs_i + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun countPairs() { + assertThat(Solution().countPairs(intArrayOf(3, 12, 30, 17, 21)), equalTo(2)) + } + + @Test + fun countPairs2() { + assertThat(Solution().countPairs(intArrayOf(1, 1, 1, 1, 1)), equalTo(10)) + } + + @Test + fun countPairs3() { + assertThat(Solution().countPairs(intArrayOf(123, 231)), equalTo(0)) + } +} diff --git a/src/test/kotlin/g3201_3300/s3266_final_array_state_after_k_multiplication_operations_ii/SolutionTest.kt b/src/test/kotlin/g3201_3300/s3266_final_array_state_after_k_multiplication_operations_ii/SolutionTest.kt new file mode 100644 index 00000000..201101a6 --- /dev/null +++ b/src/test/kotlin/g3201_3300/s3266_final_array_state_after_k_multiplication_operations_ii/SolutionTest.kt @@ -0,0 +1,23 @@ +package g3201_3300.s3266_final_array_state_after_k_multiplication_operations_ii + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun finalState() { + assertThat( + Solution().getFinalState(intArrayOf(2, 1, 3, 5, 6), 5, 2), + equalTo(intArrayOf(8, 4, 6, 5, 6)) + ) + } + + @Test + fun finalState2() { + assertThat( + Solution().getFinalState(intArrayOf(100000, 2000), 2, 1000000), + equalTo(intArrayOf(999999307, 999999993)) + ) + } +} diff --git a/src/test/kotlin/g3201_3300/s3267_count_almost_equal_pairs_ii/SolutionTest.kt b/src/test/kotlin/g3201_3300/s3267_count_almost_equal_pairs_ii/SolutionTest.kt new file mode 100644 index 00000000..272fa8e5 --- /dev/null +++ b/src/test/kotlin/g3201_3300/s3267_count_almost_equal_pairs_ii/SolutionTest.kt @@ -0,0 +1,17 @@ +package g3201_3300.s3267_count_almost_equal_pairs_ii + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun countPairs() { + assertThat(Solution().countPairs(intArrayOf(1023, 2310, 2130, 213)), equalTo(4)) + } + + @Test + fun countPairs2() { + assertThat(Solution().countPairs(intArrayOf(1, 10, 100)), equalTo(3)) + } +}