Skip to content

Commit af3063f

Browse files
authored
Added tasks 3264-3267
1 parent 695b7b9 commit af3063f

File tree

12 files changed

+484
-0
lines changed

12 files changed

+484
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package g3201_3300.s3264_final_array_state_after_k_multiplication_operations_i;
2+
3+
// #Easy #2024_08_28_Time_1_ms_(100.00%)_Space_44.9_MB_(31.20%)
4+
5+
public class Solution {
6+
public int[] getFinalState(int[] nums, int k, int multiplier) {
7+
while (k-- > 0) {
8+
int min = nums[0];
9+
int index = 0;
10+
for (int i = 0; i < nums.length; i++) {
11+
if (min > nums[i]) {
12+
min = nums[i];
13+
index = i;
14+
}
15+
}
16+
nums[index] = nums[index] * multiplier;
17+
}
18+
return nums;
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
3264\. Final Array State After K Multiplication Operations I
2+
3+
Easy
4+
5+
You are given an integer array `nums`, an integer `k`, and an integer `multiplier`.
6+
7+
You need to perform `k` operations on `nums`. In each operation:
8+
9+
* Find the **minimum** value `x` in `nums`. If there are multiple occurrences of the minimum value, select the one that appears **first**.
10+
* Replace the selected minimum value `x` with `x * multiplier`.
11+
12+
Return an integer array denoting the _final state_ of `nums` after performing all `k` operations.
13+
14+
**Example 1:**
15+
16+
**Input:** nums = [2,1,3,5,6], k = 5, multiplier = 2
17+
18+
**Output:** [8,4,6,5,6]
19+
20+
**Explanation:**
21+
22+
| Operation | Result |
23+
|---------------------|------------------|
24+
| After operation 1 | [2, 2, 3, 5, 6] |
25+
| After operation 2 | [4, 2, 3, 5, 6] |
26+
| After operation 3 | [4, 4, 3, 5, 6] |
27+
| After operation 4 | [4, 4, 6, 5, 6] |
28+
| After operation 5 | [8, 4, 6, 5, 6] |
29+
30+
**Example 2:**
31+
32+
**Input:** nums = [1,2], k = 3, multiplier = 4
33+
34+
**Output:** [16,8]
35+
36+
**Explanation:**
37+
38+
| Operation | Result |
39+
|---------------------|-------------|
40+
| After operation 1 | [4, 2] |
41+
| After operation 2 | [4, 8] |
42+
| After operation 3 | [16, 8] |
43+
44+
**Constraints:**
45+
46+
* `1 <= nums.length <= 100`
47+
* `1 <= nums[i] <= 100`
48+
* `1 <= k <= 10`
49+
* `1 <= multiplier <= 5`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package g3201_3300.s3265_count_almost_equal_pairs_i;
2+
3+
// #Medium #2024_08_28_Time_5_ms_(100.00%)_Space_44.7_MB_(91.23%)
4+
5+
public class Solution {
6+
public int countPairs(int[] nums) {
7+
int ans = 0;
8+
for (int i = 0; i < nums.length - 1; i++) {
9+
for (int j = i + 1; j < nums.length; j++) {
10+
if (nums[i] == nums[j]
11+
|| ((nums[j] - nums[i]) % 9 == 0 && check(nums[i], nums[j]))) {
12+
ans++;
13+
}
14+
}
15+
}
16+
return ans;
17+
}
18+
19+
private boolean check(int a, int b) {
20+
int[] ca = new int[10];
21+
int[] cb = new int[10];
22+
int d = 0;
23+
while (a > 0 || b > 0) {
24+
if (a % 10 != b % 10) {
25+
d++;
26+
if (d > 2) {
27+
return false;
28+
}
29+
}
30+
ca[a % 10]++;
31+
cb[b % 10]++;
32+
a /= 10;
33+
b /= 10;
34+
}
35+
return d == 2 && areEqual(ca, cb);
36+
}
37+
38+
private boolean areEqual(int[] a, int[] b) {
39+
for (int i = 0; i < 10; i++) {
40+
if (a[i] != b[i]) {
41+
return false;
42+
}
43+
}
44+
return true;
45+
}
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
3265\. Count Almost Equal Pairs I
2+
3+
Medium
4+
5+
You are given an array `nums` consisting of positive integers.
6+
7+
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**:
8+
9+
* Choose **either** `x` or `y` and swap any two digits within the chosen number.
10+
11+
Return the number of indices `i` and `j` in `nums` where `i < j` such that `nums[i]` and `nums[j]` are **almost equal**.
12+
13+
**Note** that it is allowed for an integer to have leading zeros after performing an operation.
14+
15+
**Example 1:**
16+
17+
**Input:** nums = [3,12,30,17,21]
18+
19+
**Output:** 2
20+
21+
**Explanation:**
22+
23+
The almost equal pairs of elements are:
24+
25+
* 3 and 30. By swapping 3 and 0 in 30, you get 3.
26+
* 12 and 21. By swapping 1 and 2 in 12, you get 21.
27+
28+
**Example 2:**
29+
30+
**Input:** nums = [1,1,1,1,1]
31+
32+
**Output:** 10
33+
34+
**Explanation:**
35+
36+
Every two elements in the array are almost equal.
37+
38+
**Example 3:**
39+
40+
**Input:** nums = [123,231]
41+
42+
**Output:** 0
43+
44+
**Explanation:**
45+
46+
We cannot swap any two digits of 123 or 231 to reach the other.
47+
48+
**Constraints:**
49+
50+
* `2 <= nums.length <= 100`
51+
* <code>1 <= nums[i] <= 10<sup>6</sup></code>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package g3201_3300.s3266_final_array_state_after_k_multiplication_operations_ii;
2+
3+
// #Hard #2024_08_28_Time_26_ms_(100.00%)_Space_47_MB_(51.94%)
4+
5+
import java.util.Arrays;
6+
import java.util.PriorityQueue;
7+
8+
public class Solution {
9+
private static final int MOD = 1_000_000_007;
10+
11+
public int[] getFinalState(int[] nums, int k, int multiplier) {
12+
if (multiplier == 1) {
13+
return nums;
14+
}
15+
int n = nums.length;
16+
int mx = 0;
17+
for (int x : nums) {
18+
mx = Math.max(mx, x);
19+
}
20+
long[] a = new long[n];
21+
int left = k;
22+
boolean shouldExit = false;
23+
for (int i = 0; i < n && !shouldExit; i++) {
24+
long x = nums[i];
25+
while (x < mx) {
26+
x *= multiplier;
27+
if (--left < 0) {
28+
shouldExit = true;
29+
break;
30+
}
31+
}
32+
a[i] = x;
33+
}
34+
if (left < 0) {
35+
PriorityQueue<long[]> pq =
36+
new PriorityQueue<>(
37+
(p, q) ->
38+
p[0] != q[0]
39+
? Long.compare(p[0], q[0])
40+
: Long.compare(p[1], q[1]));
41+
for (int i = 0; i < n; i++) {
42+
pq.offer(new long[] {nums[i], i});
43+
}
44+
while (k-- > 0) {
45+
long[] p = pq.poll();
46+
p[0] *= multiplier;
47+
pq.offer(p);
48+
}
49+
while (!pq.isEmpty()) {
50+
long[] p = pq.poll();
51+
nums[(int) p[1]] = (int) (p[0] % MOD);
52+
}
53+
return nums;
54+
}
55+
56+
Integer[] ids = new Integer[n];
57+
Arrays.setAll(ids, i -> i);
58+
Arrays.sort(ids, (i, j) -> Long.compare(a[i], a[j]));
59+
k = left;
60+
long pow1 = pow(multiplier, k / n);
61+
long pow2 = pow1 * multiplier % MOD;
62+
for (int i = 0; i < n; i++) {
63+
int j = ids[i];
64+
nums[j] = (int) (a[j] % MOD * (i < k % n ? pow2 : pow1) % MOD);
65+
}
66+
return nums;
67+
}
68+
69+
private long pow(long x, int n) {
70+
long res = 1;
71+
for (; n > 0; n /= 2) {
72+
if (n % 2 > 0) {
73+
res = res * x % MOD;
74+
}
75+
x = x * x % MOD;
76+
}
77+
return res;
78+
}
79+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
3266\. Final Array State After K Multiplication Operations II
2+
3+
Hard
4+
5+
You are given an integer array `nums`, an integer `k`, and an integer `multiplier`.
6+
7+
You need to perform `k` operations on `nums`. In each operation:
8+
9+
* Find the **minimum** value `x` in `nums`. If there are multiple occurrences of the minimum value, select the one that appears **first**.
10+
* Replace the selected minimum value `x` with `x * multiplier`.
11+
12+
After the `k` operations, apply **modulo** <code>10<sup>9</sup> + 7</code> to every value in `nums`.
13+
14+
Return an integer array denoting the _final state_ of `nums` after performing all `k` operations and then applying the modulo.
15+
16+
**Example 1:**
17+
18+
**Input:** nums = [2,1,3,5,6], k = 5, multiplier = 2
19+
20+
**Output:** [8,4,6,5,6]
21+
22+
**Explanation:**
23+
24+
| Operation | Result |
25+
|-------------------------|------------------|
26+
| After operation 1 | [2, 2, 3, 5, 6] |
27+
| After operation 2 | [4, 2, 3, 5, 6] |
28+
| After operation 3 | [4, 4, 3, 5, 6] |
29+
| After operation 4 | [4, 4, 6, 5, 6] |
30+
| After operation 5 | [8, 4, 6, 5, 6] |
31+
| After applying modulo | [8, 4, 6, 5, 6] |
32+
33+
**Example 2:**
34+
35+
**Input:** nums = [100000,2000], k = 2, multiplier = 1000000
36+
37+
**Output:** [999999307,999999993]
38+
39+
**Explanation:**
40+
41+
| Operation | Result |
42+
|-------------------------|----------------------|
43+
| After operation 1 | [100000, 2000000000] |
44+
| After operation 2 | [100000000000, 2000000000] |
45+
| After applying modulo | [999999307, 999999993] |
46+
47+
**Constraints:**
48+
49+
* <code>1 <= nums.length <= 10<sup>4</sup></code>
50+
* <code>1 <= nums[i] <= 10<sup>9</sup></code>
51+
* <code>1 <= k <= 10<sup>9</sup></code>
52+
* <code>1 <= multiplier <= 10<sup>6</sup></code>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package g3201_3300.s3267_count_almost_equal_pairs_ii;
2+
3+
// #Hard #2024_08_28_Time_353_ms_(99.78%)_Space_45.8_MB_(56.64%)
4+
5+
import java.util.Arrays;
6+
import java.util.HashMap;
7+
import java.util.HashSet;
8+
import java.util.Map;
9+
import java.util.Set;
10+
11+
public class Solution {
12+
public int countPairs(int[] nums) {
13+
int pairs = 0;
14+
Map<Integer, Integer> counts = new HashMap<>();
15+
Arrays.sort(nums);
16+
for (int num : nums) {
17+
Set<Integer> newNums = new HashSet<>();
18+
newNums.add(num);
19+
for (int unit1 = 1, remain1 = num; remain1 > 0; unit1 *= 10, remain1 /= 10) {
20+
int digit1 = num / unit1 % 10;
21+
for (int unit2 = unit1 * 10, remain2 = remain1 / 10;
22+
remain2 > 0;
23+
unit2 *= 10, remain2 /= 10) {
24+
int digit2 = num / unit2 % 10;
25+
int newNum1 =
26+
num - digit1 * unit1 - digit2 * unit2 + digit2 * unit1 + digit1 * unit2;
27+
newNums.add(newNum1);
28+
for (int unit3 = unit1 * 10, remain3 = remain1 / 10;
29+
remain3 > 0;
30+
unit3 *= 10, remain3 /= 10) {
31+
int digit3 = newNum1 / unit3 % 10;
32+
for (int unit4 = unit3 * 10, remain4 = remain3 / 10;
33+
remain4 > 0;
34+
unit4 *= 10, remain4 /= 10) {
35+
int digit4 = newNum1 / unit4 % 10;
36+
int newNum2 =
37+
newNum1
38+
- digit3 * unit3
39+
- digit4 * unit4
40+
+ digit4 * unit3
41+
+ digit3 * unit4;
42+
newNums.add(newNum2);
43+
}
44+
}
45+
}
46+
}
47+
for (int newNum : newNums) {
48+
pairs += counts.getOrDefault(newNum, 0);
49+
}
50+
counts.put(num, counts.getOrDefault(num, 0) + 1);
51+
}
52+
return pairs;
53+
}
54+
}

0 commit comments

Comments
 (0)