Skip to content

Commit

Permalink
Added tasks 3264-3267
Browse files Browse the repository at this point in the history
  • Loading branch information
javadev authored Aug 28, 2024
1 parent 695b7b9 commit af3063f
Show file tree
Hide file tree
Showing 12 changed files with 484 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package g3201_3300.s3264_final_array_state_after_k_multiplication_operations_i;

// #Easy #2024_08_28_Time_1_ms_(100.00%)_Space_44.9_MB_(31.20%)

public class Solution {
public int[] getFinalState(int[] nums, int k, int multiplier) {
while (k-- > 0) {
int min = nums[0];
int index = 0;
for (int i = 0; i < nums.length; i++) {
if (min > nums[i]) {
min = nums[i];
index = i;
}
}
nums[index] = nums[index] * multiplier;
}
return nums;
}
}
Original file line number Diff line number Diff line change
@@ -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`
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package g3201_3300.s3265_count_almost_equal_pairs_i;

// #Medium #2024_08_28_Time_5_ms_(100.00%)_Space_44.7_MB_(91.23%)

public class Solution {
public int countPairs(int[] nums) {
int ans = 0;
for (int i = 0; i < nums.length - 1; i++) {
for (int j = i + 1; j < nums.length; j++) {
if (nums[i] == nums[j]
|| ((nums[j] - nums[i]) % 9 == 0 && check(nums[i], nums[j]))) {
ans++;
}
}
}
return ans;
}

private boolean check(int a, int b) {
int[] ca = new int[10];
int[] cb = new int[10];
int 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 boolean areEqual(int[] a, int[] b) {
for (int i = 0; i < 10; i++) {
if (a[i] != b[i]) {
return false;
}
}
return true;
}
}
Original file line number Diff line number Diff line change
@@ -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`
* <code>1 <= nums[i] <= 10<sup>6</sup></code>
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package g3201_3300.s3266_final_array_state_after_k_multiplication_operations_ii;

// #Hard #2024_08_28_Time_26_ms_(100.00%)_Space_47_MB_(51.94%)

import java.util.Arrays;
import java.util.PriorityQueue;

public class Solution {
private static final int MOD = 1_000_000_007;

public int[] getFinalState(int[] nums, int k, int multiplier) {
if (multiplier == 1) {
return nums;
}
int n = nums.length;
int mx = 0;
for (int x : nums) {
mx = Math.max(mx, x);
}
long[] a = new long[n];
int left = k;
boolean shouldExit = false;
for (int i = 0; i < n && !shouldExit; i++) {
long x = nums[i];
while (x < mx) {
x *= multiplier;
if (--left < 0) {
shouldExit = true;
break;
}
}
a[i] = x;
}
if (left < 0) {
PriorityQueue<long[]> pq =
new PriorityQueue<>(
(p, q) ->
p[0] != q[0]
? Long.compare(p[0], q[0])
: Long.compare(p[1], q[1]));
for (int i = 0; i < n; i++) {
pq.offer(new long[] {nums[i], i});
}
while (k-- > 0) {
long[] p = pq.poll();
p[0] *= multiplier;
pq.offer(p);
}
while (!pq.isEmpty()) {
long[] p = pq.poll();
nums[(int) p[1]] = (int) (p[0] % MOD);
}
return nums;
}

Integer[] ids = new Integer[n];
Arrays.setAll(ids, i -> i);
Arrays.sort(ids, (i, j) -> Long.compare(a[i], a[j]));
k = left;
long pow1 = pow(multiplier, k / n);
long pow2 = pow1 * multiplier % MOD;
for (int i = 0; i < n; i++) {
int j = ids[i];
nums[j] = (int) (a[j] % MOD * (i < k % n ? pow2 : pow1) % MOD);
}
return nums;
}

private long pow(long x, int n) {
long res = 1;
for (; n > 0; n /= 2) {
if (n % 2 > 0) {
res = res * x % MOD;
}
x = x * x % MOD;
}
return res;
}
}
Original file line number Diff line number Diff line change
@@ -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** <code>10<sup>9</sup> + 7</code> 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:**

* <code>1 <= nums.length <= 10<sup>4</sup></code>
* <code>1 <= nums[i] <= 10<sup>9</sup></code>
* <code>1 <= k <= 10<sup>9</sup></code>
* <code>1 <= multiplier <= 10<sup>6</sup></code>
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package g3201_3300.s3267_count_almost_equal_pairs_ii;

// #Hard #2024_08_28_Time_353_ms_(99.78%)_Space_45.8_MB_(56.64%)

import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class Solution {
public int countPairs(int[] nums) {
int pairs = 0;
Map<Integer, Integer> counts = new HashMap<>();
Arrays.sort(nums);
for (int num : nums) {
Set<Integer> newNums = new HashSet<>();
newNums.add(num);
for (int unit1 = 1, remain1 = num; remain1 > 0; unit1 *= 10, remain1 /= 10) {
int digit1 = num / unit1 % 10;
for (int unit2 = unit1 * 10, remain2 = remain1 / 10;
remain2 > 0;
unit2 *= 10, remain2 /= 10) {
int digit2 = num / unit2 % 10;
int newNum1 =
num - digit1 * unit1 - digit2 * unit2 + digit2 * unit1 + digit1 * unit2;
newNums.add(newNum1);
for (int unit3 = unit1 * 10, remain3 = remain1 / 10;
remain3 > 0;
unit3 *= 10, remain3 /= 10) {
int digit3 = newNum1 / unit3 % 10;
for (int unit4 = unit3 * 10, remain4 = remain3 / 10;
remain4 > 0;
unit4 *= 10, remain4 /= 10) {
int digit4 = newNum1 / unit4 % 10;
int newNum2 =
newNum1
- digit3 * unit3
- digit4 * unit4
+ digit4 * unit3
+ digit3 * unit4;
newNums.add(newNum2);
}
}
}
}
for (int newNum : newNums) {
pairs += counts.getOrDefault(newNum, 0);
}
counts.put(num, counts.getOrDefault(num, 0) + 1);
}
return pairs;
}
}
Loading

0 comments on commit af3063f

Please sign in to comment.