From 15107e9d04389130c4fe1ce92e26e9374fa6c95f Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Thu, 14 Nov 2024 07:15:45 +0200 Subject: [PATCH 1/2] Added tasks 3345-3348 --- .../Solution.kt | 28 +++++++ .../readme.md | 30 ++++++++ .../Solution.kt | 42 ++++++++++ .../readme.md | 44 +++++++++++ .../Solution.kt | 42 ++++++++++ .../readme.md | 44 +++++++++++ .../Solution.kt | 77 +++++++++++++++++++ .../readme.md | 46 +++++++++++ .../SolutionTest.kt | 17 ++++ .../SolutionTest.kt | 23 ++++++ .../SolutionTest.kt | 23 ++++++ .../SolutionTest.kt | 38 +++++++++ 12 files changed, 454 insertions(+) create mode 100644 src/main/kotlin/g3301_3400/s3345_smallest_divisible_digit_product_i/Solution.kt create mode 100644 src/main/kotlin/g3301_3400/s3345_smallest_divisible_digit_product_i/readme.md create mode 100644 src/main/kotlin/g3301_3400/s3346_maximum_frequency_of_an_element_after_performing_operations_i/Solution.kt create mode 100644 src/main/kotlin/g3301_3400/s3346_maximum_frequency_of_an_element_after_performing_operations_i/readme.md create mode 100644 src/main/kotlin/g3301_3400/s3347_maximum_frequency_of_an_element_after_performing_operations_ii/Solution.kt create mode 100644 src/main/kotlin/g3301_3400/s3347_maximum_frequency_of_an_element_after_performing_operations_ii/readme.md create mode 100644 src/main/kotlin/g3301_3400/s3348_smallest_divisible_digit_product_ii/Solution.kt create mode 100644 src/main/kotlin/g3301_3400/s3348_smallest_divisible_digit_product_ii/readme.md create mode 100644 src/test/kotlin/g3301_3400/s3345_smallest_divisible_digit_product_i/SolutionTest.kt create mode 100644 src/test/kotlin/g3301_3400/s3346_maximum_frequency_of_an_element_after_performing_operations_i/SolutionTest.kt create mode 100644 src/test/kotlin/g3301_3400/s3347_maximum_frequency_of_an_element_after_performing_operations_ii/SolutionTest.kt create mode 100644 src/test/kotlin/g3301_3400/s3348_smallest_divisible_digit_product_ii/SolutionTest.kt diff --git a/src/main/kotlin/g3301_3400/s3345_smallest_divisible_digit_product_i/Solution.kt b/src/main/kotlin/g3301_3400/s3345_smallest_divisible_digit_product_i/Solution.kt new file mode 100644 index 000000000..02002a6a9 --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3345_smallest_divisible_digit_product_i/Solution.kt @@ -0,0 +1,28 @@ +package g3301_3400.s3345_smallest_divisible_digit_product_i + +// #Easy #Math #Enumeration #2024_11_14_Time_1_ms_(100.00%)_Space_33.7_MB_(100.00%) + +class Solution { + fun smallestNumber(n: Int, t: Int): Int { + var num = -1 + var check = n + while (num == -1) { + val product = findProduct(check) + if (product % t == 0) { + num = check + } + check += 1 + } + return num + } + + private fun findProduct(check: Int): Int { + var check = check + var res = 1 + while (check > 0) { + res *= check % 10 + check = check / 10 + } + return res + } +} diff --git a/src/main/kotlin/g3301_3400/s3345_smallest_divisible_digit_product_i/readme.md b/src/main/kotlin/g3301_3400/s3345_smallest_divisible_digit_product_i/readme.md new file mode 100644 index 000000000..92ea91da9 --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3345_smallest_divisible_digit_product_i/readme.md @@ -0,0 +1,30 @@ +3345\. Smallest Divisible Digit Product I + +Easy + +You are given two integers `n` and `t`. Return the **smallest** number greater than or equal to `n` such that the **product of its digits** is divisible by `t`. + +**Example 1:** + +**Input:** n = 10, t = 2 + +**Output:** 10 + +**Explanation:** + +The digit product of 10 is 0, which is divisible by 2, making it the smallest number greater than or equal to 10 that satisfies the condition. + +**Example 2:** + +**Input:** n = 15, t = 3 + +**Output:** 16 + +**Explanation:** + +The digit product of 16 is 6, which is divisible by 3, making it the smallest number greater than or equal to 15 that satisfies the condition. + +**Constraints:** + +* `1 <= n <= 100` +* `1 <= t <= 10` \ No newline at end of file diff --git a/src/main/kotlin/g3301_3400/s3346_maximum_frequency_of_an_element_after_performing_operations_i/Solution.kt b/src/main/kotlin/g3301_3400/s3346_maximum_frequency_of_an_element_after_performing_operations_i/Solution.kt new file mode 100644 index 000000000..38daaa98d --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3346_maximum_frequency_of_an_element_after_performing_operations_i/Solution.kt @@ -0,0 +1,42 @@ +package g3301_3400.s3346_maximum_frequency_of_an_element_after_performing_operations_i + +// #Medium #Array #Sorting #Binary_Search #Prefix_Sum #Sliding_Window +// #2024_11_14_Time_12_ms_(100.00%)_Space_64.1_MB_(80.00%) + +import kotlin.math.max +import kotlin.math.min + +class Solution { + private fun getMax(nums: IntArray): Int { + var max = nums[0] + for (num in nums) { + max = max(num, max) + } + return max + } + + fun maxFrequency(nums: IntArray, k: Int, numOperations: Int): Int { + val maxNum = getMax(nums) + val n = maxNum + k + 2 + val freq = IntArray(n) + for (num in nums) { + freq[num]++ + } + val pref = IntArray(n) + pref[0] = freq[0] + for (i in 1 until n) { + pref[i] = pref[i - 1] + freq[i] + } + var res = 0 + for (i in 0 until n) { + val left: Int = max(0, (i - k)) + val right: Int = min((n - 1), (i + k)) + var tot = pref[right] + if (left > 0) { + tot -= pref[left - 1] + } + res = max(res, (freq[i] + min(numOperations, (tot - freq[i])))) + } + return res + } +} diff --git a/src/main/kotlin/g3301_3400/s3346_maximum_frequency_of_an_element_after_performing_operations_i/readme.md b/src/main/kotlin/g3301_3400/s3346_maximum_frequency_of_an_element_after_performing_operations_i/readme.md new file mode 100644 index 000000000..d86c954eb --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3346_maximum_frequency_of_an_element_after_performing_operations_i/readme.md @@ -0,0 +1,44 @@ +3346\. Maximum Frequency of an Element After Performing Operations I + +Medium + +You are given an integer array `nums` and two integers `k` and `numOperations`. + +You must perform an **operation** `numOperations` times on `nums`, where in each operation you: + +* Select an index `i` that was **not** selected in any previous operations. +* Add an integer in the range `[-k, k]` to `nums[i]`. + +Return the **maximum** possible frequency of any element in `nums` after performing the **operations**. + +**Example 1:** + +**Input:** nums = [1,4,5], k = 1, numOperations = 2 + +**Output:** 2 + +**Explanation:** + +We can achieve a maximum frequency of two by: + +* Adding 0 to `nums[1]`. `nums` becomes `[1, 4, 5]`. +* Adding -1 to `nums[2]`. `nums` becomes `[1, 4, 4]`. + +**Example 2:** + +**Input:** nums = [5,11,20,20], k = 5, numOperations = 1 + +**Output:** 2 + +**Explanation:** + +We can achieve a maximum frequency of two by: + +* Adding 0 to `nums[1]`. + +**Constraints:** + +* 1 <= nums.length <= 105 +* 1 <= nums[i] <= 105 +* 0 <= k <= 105 +* `0 <= numOperations <= nums.length` \ No newline at end of file diff --git a/src/main/kotlin/g3301_3400/s3347_maximum_frequency_of_an_element_after_performing_operations_ii/Solution.kt b/src/main/kotlin/g3301_3400/s3347_maximum_frequency_of_an_element_after_performing_operations_ii/Solution.kt new file mode 100644 index 000000000..5b446c392 --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3347_maximum_frequency_of_an_element_after_performing_operations_ii/Solution.kt @@ -0,0 +1,42 @@ +package g3301_3400.s3347_maximum_frequency_of_an_element_after_performing_operations_ii + +// #Hard #Array #Sorting #Binary_Search #Prefix_Sum #Sliding_Window +// #2024_11_14_Time_48_ms_(100.00%)_Space_67.8_MB_(93.33%) + +import kotlin.math.max +import kotlin.math.min + +class Solution { + fun maxFrequency(nums: IntArray, k: Int, numOperations: Int): Int { + nums.sort() + val n = nums.size + var l = 0 + var r = 0 + var i = 0 + var j = 0 + var res = 0 + while (i < n) { + while (j < n && nums[j] == nums[i]) { + j++ + } + while (l < i && nums[i] - nums[l] > k) { + l++ + } + while (r < n && nums[r] - nums[i] <= k) { + r++ + } + res = max(res, (min((i - l + r - j), numOperations) + j - i)) + i = j + } + i = 0 + j = 0 + while (i < n && j < n) { + while (j < n && j - i < numOperations && nums[j] - nums[i] <= k * 2) { + j++ + } + res = max(res, (j - i)) + i++ + } + return res + } +} diff --git a/src/main/kotlin/g3301_3400/s3347_maximum_frequency_of_an_element_after_performing_operations_ii/readme.md b/src/main/kotlin/g3301_3400/s3347_maximum_frequency_of_an_element_after_performing_operations_ii/readme.md new file mode 100644 index 000000000..79914babd --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3347_maximum_frequency_of_an_element_after_performing_operations_ii/readme.md @@ -0,0 +1,44 @@ +3347\. Maximum Frequency of an Element After Performing Operations II + +Hard + +You are given an integer array `nums` and two integers `k` and `numOperations`. + +You must perform an **operation** `numOperations` times on `nums`, where in each operation you: + +* Select an index `i` that was **not** selected in any previous operations. +* Add an integer in the range `[-k, k]` to `nums[i]`. + +Return the **maximum** possible frequency of any element in `nums` after performing the **operations**. + +**Example 1:** + +**Input:** nums = [1,4,5], k = 1, numOperations = 2 + +**Output:** 2 + +**Explanation:** + +We can achieve a maximum frequency of two by: + +* Adding 0 to `nums[1]`, after which `nums` becomes `[1, 4, 5]`. +* Adding -1 to `nums[2]`, after which `nums` becomes `[1, 4, 4]`. + +**Example 2:** + +**Input:** nums = [5,11,20,20], k = 5, numOperations = 1 + +**Output:** 2 + +**Explanation:** + +We can achieve a maximum frequency of two by: + +* Adding 0 to `nums[1]`. + +**Constraints:** + +* 1 <= nums.length <= 105 +* 1 <= nums[i] <= 109 +* 0 <= k <= 109 +* `0 <= numOperations <= nums.length` \ No newline at end of file diff --git a/src/main/kotlin/g3301_3400/s3348_smallest_divisible_digit_product_ii/Solution.kt b/src/main/kotlin/g3301_3400/s3348_smallest_divisible_digit_product_ii/Solution.kt new file mode 100644 index 000000000..452dddc0d --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3348_smallest_divisible_digit_product_ii/Solution.kt @@ -0,0 +1,77 @@ +package g3301_3400.s3348_smallest_divisible_digit_product_ii + +// #Hard #String #Math #Greedy #Backtracking #Number_Theory +// #2024_11_14_Time_46_ms_(100.00%)_Space_48.2_MB_(100.00%) + +class Solution { + fun smallestNumber(num: String, t: Long): String { + var t = t + var tmp = t + for (i in 9 downTo 2) { + while (tmp % i == 0L) { + tmp /= i.toLong() + } + } + if (tmp > 1) { + return "-1" + } + val s = num.toCharArray() + val n = s.size + val leftT = LongArray(n + 1) + leftT[0] = t + var i0 = n - 1 + for (i in 0 until n) { + if (s[i] == '0') { + i0 = i + break + } + leftT[i + 1] = leftT[i] / gcd(leftT[i], s[i].code.toLong() - '0'.code.toLong()) + } + if (leftT[n] == 1L) { + return num + } + for (i in i0 downTo 0) { + while (++s[i] <= '9') { + var tt = leftT[i] / gcd(leftT[i], s[i].code.toLong() - '0'.code.toLong()) + for (j in n - 1 downTo i + 1) { + if (tt == 1L) { + s[j] = '1' + continue + } + for (k in 9 downTo 2) { + if (tt % k == 0L) { + s[j] = ('0'.code + k).toChar() + tt /= k.toLong() + break + } + } + } + if (tt == 1L) { + return String(s) + } + } + } + val ans = StringBuilder() + for (i in 9 downTo 2) { + while (t % i == 0L) { + ans.append(('0'.code + i).toChar()) + t /= i.toLong() + } + } + while (ans.length <= n) { + ans.append('1') + } + return ans.reverse().toString() + } + + private fun gcd(a: Long, b: Long): Long { + var a = a + var b = b + while (a != 0L) { + val tmp = a + a = b % a + b = tmp + } + return b + } +} diff --git a/src/main/kotlin/g3301_3400/s3348_smallest_divisible_digit_product_ii/readme.md b/src/main/kotlin/g3301_3400/s3348_smallest_divisible_digit_product_ii/readme.md new file mode 100644 index 000000000..4a24ceb5c --- /dev/null +++ b/src/main/kotlin/g3301_3400/s3348_smallest_divisible_digit_product_ii/readme.md @@ -0,0 +1,46 @@ +3348\. Smallest Divisible Digit Product II + +Hard + +You are given a string `num` which represents a **positive** integer, and an integer `t`. + +A number is called **zero-free** if _none_ of its digits are 0. + +Return a string representing the **smallest** **zero-free** number greater than or equal to `num` such that the **product of its digits** is divisible by `t`. If no such number exists, return `"-1"`. + +**Example 1:** + +**Input:** num = "1234", t = 256 + +**Output:** "1488" + +**Explanation:** + +The smallest zero-free number that is greater than 1234 and has the product of its digits divisible by 256 is 1488, with the product of its digits equal to 256. + +**Example 2:** + +**Input:** num = "12355", t = 50 + +**Output:** "12355" + +**Explanation:** + +12355 is already zero-free and has the product of its digits divisible by 50, with the product of its digits equal to 150. + +**Example 3:** + +**Input:** num = "11111", t = 26 + +**Output:** "-1" + +**Explanation:** + +No number greater than 11111 has the product of its digits divisible by 26. + +**Constraints:** + +* 2 <= num.length <= 2 * 105 +* `num` consists only of digits in the range `['0', '9']`. +* `num` does not contain leading zeros. +* 1 <= t <= 1014 \ No newline at end of file diff --git a/src/test/kotlin/g3301_3400/s3345_smallest_divisible_digit_product_i/SolutionTest.kt b/src/test/kotlin/g3301_3400/s3345_smallest_divisible_digit_product_i/SolutionTest.kt new file mode 100644 index 000000000..a3bcc1330 --- /dev/null +++ b/src/test/kotlin/g3301_3400/s3345_smallest_divisible_digit_product_i/SolutionTest.kt @@ -0,0 +1,17 @@ +package g3301_3400.s3345_smallest_divisible_digit_product_i + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun smallestNumber() { + assertThat(Solution().smallestNumber(10, 2), equalTo(10)) + } + + @Test + fun smallestNumber2() { + assertThat(Solution().smallestNumber(15, 3), equalTo(16)) + } +} diff --git a/src/test/kotlin/g3301_3400/s3346_maximum_frequency_of_an_element_after_performing_operations_i/SolutionTest.kt b/src/test/kotlin/g3301_3400/s3346_maximum_frequency_of_an_element_after_performing_operations_i/SolutionTest.kt new file mode 100644 index 000000000..c440d46df --- /dev/null +++ b/src/test/kotlin/g3301_3400/s3346_maximum_frequency_of_an_element_after_performing_operations_i/SolutionTest.kt @@ -0,0 +1,23 @@ +package g3301_3400.s3346_maximum_frequency_of_an_element_after_performing_operations_i + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun maxFrequency() { + assertThat( + Solution().maxFrequency(intArrayOf(1, 4, 5), 1, 2), + equalTo(2) + ) + } + + @Test + fun maxFrequency2() { + assertThat( + Solution().maxFrequency(intArrayOf(5, 11, 20, 20), 5, 1), + equalTo(2) + ) + } +} diff --git a/src/test/kotlin/g3301_3400/s3347_maximum_frequency_of_an_element_after_performing_operations_ii/SolutionTest.kt b/src/test/kotlin/g3301_3400/s3347_maximum_frequency_of_an_element_after_performing_operations_ii/SolutionTest.kt new file mode 100644 index 000000000..dc4a5faa2 --- /dev/null +++ b/src/test/kotlin/g3301_3400/s3347_maximum_frequency_of_an_element_after_performing_operations_ii/SolutionTest.kt @@ -0,0 +1,23 @@ +package g3301_3400.s3347_maximum_frequency_of_an_element_after_performing_operations_ii + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun maxFrequency() { + assertThat( + Solution().maxFrequency(intArrayOf(1, 4, 5), 1, 2), + equalTo(2) + ) + } + + @Test + fun maxFrequency2() { + assertThat( + Solution().maxFrequency(intArrayOf(5, 11, 20, 20), 5, 1), + equalTo(2) + ) + } +} diff --git a/src/test/kotlin/g3301_3400/s3348_smallest_divisible_digit_product_ii/SolutionTest.kt b/src/test/kotlin/g3301_3400/s3348_smallest_divisible_digit_product_ii/SolutionTest.kt new file mode 100644 index 000000000..5f22c77ea --- /dev/null +++ b/src/test/kotlin/g3301_3400/s3348_smallest_divisible_digit_product_ii/SolutionTest.kt @@ -0,0 +1,38 @@ +package g3301_3400.s3348_smallest_divisible_digit_product_ii + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun smallestNumber() { + assertThat( + Solution().smallestNumber("1234", 256L), + equalTo("1488") + ) + } + + @Test + fun smallestNumber2() { + assertThat( + Solution().smallestNumber("12355", 50L), + equalTo("12355") + ) + } + + @Test + fun smallestNumber3() { + assertThat(Solution().smallestNumber("11111", 26L), equalTo("-1")) + } + + @Test + fun smallestNumber4() { + assertThat(Solution().smallestNumber("10", 320L), equalTo("588")) + } + + @Test + fun smallestNumber5() { + assertThat(Solution().smallestNumber("19", 2L), equalTo("21")) + } +} From 590016e44f864626424a9c3add512ffdf4d7fbbd Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Thu, 14 Nov 2024 07:21:20 +0200 Subject: [PATCH 2/2] Fixed test --- .../g1101_1200/s1195_fizz_buzz_multithreaded/FizzBuzzTest.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/kotlin/g1101_1200/s1195_fizz_buzz_multithreaded/FizzBuzzTest.kt b/src/test/kotlin/g1101_1200/s1195_fizz_buzz_multithreaded/FizzBuzzTest.kt index a5246b318..a635eaab1 100644 --- a/src/test/kotlin/g1101_1200/s1195_fizz_buzz_multithreaded/FizzBuzzTest.kt +++ b/src/test/kotlin/g1101_1200/s1195_fizz_buzz_multithreaded/FizzBuzzTest.kt @@ -39,7 +39,7 @@ internal class FizzBuzzTest { } } .start() - TimeUnit.MILLISECONDS.sleep(1600) + TimeUnit.MILLISECONDS.sleep(2000) assertThat(fizz[0] > 0, equalTo(true)) }