Skip to content

Commit 86ef235

Browse files
committed
Added tasks 3345, 3346
1 parent 4f82bb4 commit 86ef235

File tree

6 files changed

+170
-0
lines changed

6 files changed

+170
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g3301_3400.s3345_smallest_divisible_digit_product_i;
2+
3+
// #Easy #2024_11_12_Time_1_ms_(59.15%)_Space_40.5_MB_(98.74%)
4+
5+
public class Solution {
6+
public int smallestNumber(int n, int t) {
7+
for (int i = n; i < 101; i++) {
8+
if (digProduct(i) % t == 0) {
9+
return i;
10+
}
11+
}
12+
return -1;
13+
}
14+
15+
private int digProduct(int n) {
16+
int pro = 1;
17+
while (n > 0) {
18+
pro *= n % 10;
19+
n /= 10;
20+
}
21+
return pro;
22+
}
23+
}
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,37 @@
1+
package g3301_3400.s3346_maximum_frequency_of_an_element_after_performing_operations_i;
2+
3+
// #Medium #2024_11_12_Time_7_ms_(96.72%)_Space_57.4_MB_(44.86%)
4+
5+
public class Solution {
6+
private int getMax(int[] nums) {
7+
int max = nums[0];
8+
for (int num : nums) {
9+
max = Math.max(num, max);
10+
}
11+
return max;
12+
}
13+
14+
public int maxFrequency(int[] nums, int k, int numOperations) {
15+
int maxNum = getMax(nums);
16+
int n = maxNum + k + 2;
17+
int[] freq = new int[n];
18+
for (int num : nums) {
19+
freq[num]++;
20+
}
21+
int[] pref = new int[n];
22+
pref[0] = freq[0];
23+
for (int i = 1; i < n; i++) {
24+
pref[i] = pref[i - 1] + freq[i];
25+
}
26+
int res = 0;
27+
for (int i = 0; i < n; i++) {
28+
int left = Math.max(0, i - k), right = Math.min(n - 1, i + k);
29+
int tot = pref[right];
30+
if (left > 0) {
31+
tot -= pref[left - 1];
32+
}
33+
res = Math.max(res, freq[i] + Math.min(numOperations, tot - freq[i]));
34+
}
35+
return res;
36+
}
37+
}
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,18 @@
1+
package g3301_3400.s3345_smallest_divisible_digit_product_i;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void smallestNumber() {
11+
assertThat(new Solution().smallestNumber(10, 2), equalTo(10));
12+
}
13+
14+
@Test
15+
void smallestNumber2() {
16+
assertThat(new Solution().smallestNumber(15, 3), equalTo(16));
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g3301_3400.s3346_maximum_frequency_of_an_element_after_performing_operations_i;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void maxFrequency() {
11+
assertThat(new Solution().maxFrequency(new int[] {1, 4, 5}, 1, 2), equalTo(2));
12+
}
13+
14+
@Test
15+
void maxFrequency2() {
16+
assertThat(new Solution().maxFrequency(new int[] {5, 11, 20, 20}, 5, 1), equalTo(2));
17+
}
18+
}

0 commit comments

Comments
 (0)