Skip to content

Commit 8db0f52

Browse files
authored
Added tasks 3345-3348
1 parent c8f324f commit 8db0f52

File tree

13 files changed

+455
-1
lines changed

13 files changed

+455
-1
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package g3301_3400.s3345_smallest_divisible_digit_product_i
2+
3+
// #Easy #Math #Enumeration #2024_11_14_Time_1_ms_(100.00%)_Space_33.7_MB_(100.00%)
4+
5+
class Solution {
6+
fun smallestNumber(n: Int, t: Int): Int {
7+
var num = -1
8+
var check = n
9+
while (num == -1) {
10+
val product = findProduct(check)
11+
if (product % t == 0) {
12+
num = check
13+
}
14+
check += 1
15+
}
16+
return num
17+
}
18+
19+
private fun findProduct(check: Int): Int {
20+
var check = check
21+
var res = 1
22+
while (check > 0) {
23+
res *= check % 10
24+
check = check / 10
25+
}
26+
return res
27+
}
28+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
3345\. Smallest Divisible Digit Product I
2+
3+
Easy
4+
5+
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`.
6+
7+
**Example 1:**
8+
9+
**Input:** n = 10, t = 2
10+
11+
**Output:** 10
12+
13+
**Explanation:**
14+
15+
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.
16+
17+
**Example 2:**
18+
19+
**Input:** n = 15, t = 3
20+
21+
**Output:** 16
22+
23+
**Explanation:**
24+
25+
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.
26+
27+
**Constraints:**
28+
29+
* `1 <= n <= 100`
30+
* `1 <= t <= 10`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package g3301_3400.s3346_maximum_frequency_of_an_element_after_performing_operations_i
2+
3+
// #Medium #Array #Sorting #Binary_Search #Prefix_Sum #Sliding_Window
4+
// #2024_11_14_Time_12_ms_(100.00%)_Space_64.1_MB_(80.00%)
5+
6+
import kotlin.math.max
7+
import kotlin.math.min
8+
9+
class Solution {
10+
private fun getMax(nums: IntArray): Int {
11+
var max = nums[0]
12+
for (num in nums) {
13+
max = max(num, max)
14+
}
15+
return max
16+
}
17+
18+
fun maxFrequency(nums: IntArray, k: Int, numOperations: Int): Int {
19+
val maxNum = getMax(nums)
20+
val n = maxNum + k + 2
21+
val freq = IntArray(n)
22+
for (num in nums) {
23+
freq[num]++
24+
}
25+
val pref = IntArray(n)
26+
pref[0] = freq[0]
27+
for (i in 1 until n) {
28+
pref[i] = pref[i - 1] + freq[i]
29+
}
30+
var res = 0
31+
for (i in 0 until n) {
32+
val left: Int = max(0, (i - k))
33+
val right: Int = min((n - 1), (i + k))
34+
var tot = pref[right]
35+
if (left > 0) {
36+
tot -= pref[left - 1]
37+
}
38+
res = max(res, (freq[i] + min(numOperations, (tot - freq[i]))))
39+
}
40+
return res
41+
}
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
3346\. Maximum Frequency of an Element After Performing Operations I
2+
3+
Medium
4+
5+
You are given an integer array `nums` and two integers `k` and `numOperations`.
6+
7+
You must perform an **operation** `numOperations` times on `nums`, where in each operation you:
8+
9+
* Select an index `i` that was **not** selected in any previous operations.
10+
* Add an integer in the range `[-k, k]` to `nums[i]`.
11+
12+
Return the **maximum** possible frequency of any element in `nums` after performing the **operations**.
13+
14+
**Example 1:**
15+
16+
**Input:** nums = [1,4,5], k = 1, numOperations = 2
17+
18+
**Output:** 2
19+
20+
**Explanation:**
21+
22+
We can achieve a maximum frequency of two by:
23+
24+
* Adding 0 to `nums[1]`. `nums` becomes `[1, 4, 5]`.
25+
* Adding -1 to `nums[2]`. `nums` becomes `[1, 4, 4]`.
26+
27+
**Example 2:**
28+
29+
**Input:** nums = [5,11,20,20], k = 5, numOperations = 1
30+
31+
**Output:** 2
32+
33+
**Explanation:**
34+
35+
We can achieve a maximum frequency of two by:
36+
37+
* Adding 0 to `nums[1]`.
38+
39+
**Constraints:**
40+
41+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
42+
* <code>1 <= nums[i] <= 10<sup>5</sup></code>
43+
* <code>0 <= k <= 10<sup>5</sup></code>
44+
* `0 <= numOperations <= nums.length`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package g3301_3400.s3347_maximum_frequency_of_an_element_after_performing_operations_ii
2+
3+
// #Hard #Array #Sorting #Binary_Search #Prefix_Sum #Sliding_Window
4+
// #2024_11_14_Time_48_ms_(100.00%)_Space_67.8_MB_(93.33%)
5+
6+
import kotlin.math.max
7+
import kotlin.math.min
8+
9+
class Solution {
10+
fun maxFrequency(nums: IntArray, k: Int, numOperations: Int): Int {
11+
nums.sort()
12+
val n = nums.size
13+
var l = 0
14+
var r = 0
15+
var i = 0
16+
var j = 0
17+
var res = 0
18+
while (i < n) {
19+
while (j < n && nums[j] == nums[i]) {
20+
j++
21+
}
22+
while (l < i && nums[i] - nums[l] > k) {
23+
l++
24+
}
25+
while (r < n && nums[r] - nums[i] <= k) {
26+
r++
27+
}
28+
res = max(res, (min((i - l + r - j), numOperations) + j - i))
29+
i = j
30+
}
31+
i = 0
32+
j = 0
33+
while (i < n && j < n) {
34+
while (j < n && j - i < numOperations && nums[j] - nums[i] <= k * 2) {
35+
j++
36+
}
37+
res = max(res, (j - i))
38+
i++
39+
}
40+
return res
41+
}
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
3347\. Maximum Frequency of an Element After Performing Operations II
2+
3+
Hard
4+
5+
You are given an integer array `nums` and two integers `k` and `numOperations`.
6+
7+
You must perform an **operation** `numOperations` times on `nums`, where in each operation you:
8+
9+
* Select an index `i` that was **not** selected in any previous operations.
10+
* Add an integer in the range `[-k, k]` to `nums[i]`.
11+
12+
Return the **maximum** possible frequency of any element in `nums` after performing the **operations**.
13+
14+
**Example 1:**
15+
16+
**Input:** nums = [1,4,5], k = 1, numOperations = 2
17+
18+
**Output:** 2
19+
20+
**Explanation:**
21+
22+
We can achieve a maximum frequency of two by:
23+
24+
* Adding 0 to `nums[1]`, after which `nums` becomes `[1, 4, 5]`.
25+
* Adding -1 to `nums[2]`, after which `nums` becomes `[1, 4, 4]`.
26+
27+
**Example 2:**
28+
29+
**Input:** nums = [5,11,20,20], k = 5, numOperations = 1
30+
31+
**Output:** 2
32+
33+
**Explanation:**
34+
35+
We can achieve a maximum frequency of two by:
36+
37+
* Adding 0 to `nums[1]`.
38+
39+
**Constraints:**
40+
41+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
42+
* <code>1 <= nums[i] <= 10<sup>9</sup></code>
43+
* <code>0 <= k <= 10<sup>9</sup></code>
44+
* `0 <= numOperations <= nums.length`
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package g3301_3400.s3348_smallest_divisible_digit_product_ii
2+
3+
// #Hard #String #Math #Greedy #Backtracking #Number_Theory
4+
// #2024_11_14_Time_46_ms_(100.00%)_Space_48.2_MB_(100.00%)
5+
6+
class Solution {
7+
fun smallestNumber(num: String, t: Long): String {
8+
var t = t
9+
var tmp = t
10+
for (i in 9 downTo 2) {
11+
while (tmp % i == 0L) {
12+
tmp /= i.toLong()
13+
}
14+
}
15+
if (tmp > 1) {
16+
return "-1"
17+
}
18+
val s = num.toCharArray()
19+
val n = s.size
20+
val leftT = LongArray(n + 1)
21+
leftT[0] = t
22+
var i0 = n - 1
23+
for (i in 0 until n) {
24+
if (s[i] == '0') {
25+
i0 = i
26+
break
27+
}
28+
leftT[i + 1] = leftT[i] / gcd(leftT[i], s[i].code.toLong() - '0'.code.toLong())
29+
}
30+
if (leftT[n] == 1L) {
31+
return num
32+
}
33+
for (i in i0 downTo 0) {
34+
while (++s[i] <= '9') {
35+
var tt = leftT[i] / gcd(leftT[i], s[i].code.toLong() - '0'.code.toLong())
36+
for (j in n - 1 downTo i + 1) {
37+
if (tt == 1L) {
38+
s[j] = '1'
39+
continue
40+
}
41+
for (k in 9 downTo 2) {
42+
if (tt % k == 0L) {
43+
s[j] = ('0'.code + k).toChar()
44+
tt /= k.toLong()
45+
break
46+
}
47+
}
48+
}
49+
if (tt == 1L) {
50+
return String(s)
51+
}
52+
}
53+
}
54+
val ans = StringBuilder()
55+
for (i in 9 downTo 2) {
56+
while (t % i == 0L) {
57+
ans.append(('0'.code + i).toChar())
58+
t /= i.toLong()
59+
}
60+
}
61+
while (ans.length <= n) {
62+
ans.append('1')
63+
}
64+
return ans.reverse().toString()
65+
}
66+
67+
private fun gcd(a: Long, b: Long): Long {
68+
var a = a
69+
var b = b
70+
while (a != 0L) {
71+
val tmp = a
72+
a = b % a
73+
b = tmp
74+
}
75+
return b
76+
}
77+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
3348\. Smallest Divisible Digit Product II
2+
3+
Hard
4+
5+
You are given a string `num` which represents a **positive** integer, and an integer `t`.
6+
7+
A number is called **zero-free** if _none_ of its digits are 0.
8+
9+
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"`.
10+
11+
**Example 1:**
12+
13+
**Input:** num = "1234", t = 256
14+
15+
**Output:** "1488"
16+
17+
**Explanation:**
18+
19+
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.
20+
21+
**Example 2:**
22+
23+
**Input:** num = "12355", t = 50
24+
25+
**Output:** "12355"
26+
27+
**Explanation:**
28+
29+
12355 is already zero-free and has the product of its digits divisible by 50, with the product of its digits equal to 150.
30+
31+
**Example 3:**
32+
33+
**Input:** num = "11111", t = 26
34+
35+
**Output:** "-1"
36+
37+
**Explanation:**
38+
39+
No number greater than 11111 has the product of its digits divisible by 26.
40+
41+
**Constraints:**
42+
43+
* <code>2 <= num.length <= 2 * 10<sup>5</sup></code>
44+
* `num` consists only of digits in the range `['0', '9']`.
45+
* `num` does not contain leading zeros.
46+
* <code>1 <= t <= 10<sup>14</sup></code>

src/test/kotlin/g1101_1200/s1195_fizz_buzz_multithreaded/FizzBuzzTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ internal class FizzBuzzTest {
3939
}
4040
}
4141
.start()
42-
TimeUnit.MILLISECONDS.sleep(1600)
42+
TimeUnit.MILLISECONDS.sleep(2000)
4343
assertThat(fizz[0] > 0, equalTo(true))
4444
}
4545

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package g3301_3400.s3345_smallest_divisible_digit_product_i
2+
3+
import org.hamcrest.CoreMatchers.equalTo
4+
import org.hamcrest.MatcherAssert.assertThat
5+
import org.junit.jupiter.api.Test
6+
7+
internal class SolutionTest {
8+
@Test
9+
fun smallestNumber() {
10+
assertThat<Int>(Solution().smallestNumber(10, 2), equalTo<Int>(10))
11+
}
12+
13+
@Test
14+
fun smallestNumber2() {
15+
assertThat<Int>(Solution().smallestNumber(15, 3), equalTo<Int>(16))
16+
}
17+
}

0 commit comments

Comments
 (0)