diff --git a/src/main/java/g3201_3300/s3264_final_array_state_after_k_multiplication_operations_i/Solution.java b/src/main/java/g3201_3300/s3264_final_array_state_after_k_multiplication_operations_i/Solution.java new file mode 100644 index 000000000..bd0cd469d --- /dev/null +++ b/src/main/java/g3201_3300/s3264_final_array_state_after_k_multiplication_operations_i/Solution.java @@ -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; + } +} diff --git a/src/main/java/g3201_3300/s3264_final_array_state_after_k_multiplication_operations_i/readme.md b/src/main/java/g3201_3300/s3264_final_array_state_after_k_multiplication_operations_i/readme.md new file mode 100644 index 000000000..70d684679 --- /dev/null +++ b/src/main/java/g3201_3300/s3264_final_array_state_after_k_multiplication_operations_i/readme.md @@ -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` \ No newline at end of file diff --git a/src/main/java/g3201_3300/s3265_count_almost_equal_pairs_i/Solution.java b/src/main/java/g3201_3300/s3265_count_almost_equal_pairs_i/Solution.java new file mode 100644 index 000000000..d56bda5d0 --- /dev/null +++ b/src/main/java/g3201_3300/s3265_count_almost_equal_pairs_i/Solution.java @@ -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; + } +} diff --git a/src/main/java/g3201_3300/s3265_count_almost_equal_pairs_i/readme.md b/src/main/java/g3201_3300/s3265_count_almost_equal_pairs_i/readme.md new file mode 100644 index 000000000..90aa7942f --- /dev/null +++ b/src/main/java/g3201_3300/s3265_count_almost_equal_pairs_i/readme.md @@ -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` +* 1 <= nums[i] <= 106 \ No newline at end of file diff --git a/src/main/java/g3201_3300/s3266_final_array_state_after_k_multiplication_operations_ii/Solution.java b/src/main/java/g3201_3300/s3266_final_array_state_after_k_multiplication_operations_ii/Solution.java new file mode 100644 index 000000000..f8bc99cd8 --- /dev/null +++ b/src/main/java/g3201_3300/s3266_final_array_state_after_k_multiplication_operations_ii/Solution.java @@ -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 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; + } +} diff --git a/src/main/java/g3201_3300/s3266_final_array_state_after_k_multiplication_operations_ii/readme.md b/src/main/java/g3201_3300/s3266_final_array_state_after_k_multiplication_operations_ii/readme.md new file mode 100644 index 000000000..be0868709 --- /dev/null +++ b/src/main/java/g3201_3300/s3266_final_array_state_after_k_multiplication_operations_ii/readme.md @@ -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** 109 + 7 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:** + +* 1 <= nums.length <= 104 +* 1 <= nums[i] <= 109 +* 1 <= k <= 109 +* 1 <= multiplier <= 106 \ No newline at end of file diff --git a/src/main/java/g3201_3300/s3267_count_almost_equal_pairs_ii/Solution.java b/src/main/java/g3201_3300/s3267_count_almost_equal_pairs_ii/Solution.java new file mode 100644 index 000000000..dce3ef735 --- /dev/null +++ b/src/main/java/g3201_3300/s3267_count_almost_equal_pairs_ii/Solution.java @@ -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 counts = new HashMap<>(); + Arrays.sort(nums); + for (int num : nums) { + Set 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; + } +} diff --git a/src/main/java/g3201_3300/s3267_count_almost_equal_pairs_ii/readme.md b/src/main/java/g3201_3300/s3267_count_almost_equal_pairs_ii/readme.md new file mode 100644 index 000000000..b1941676b --- /dev/null +++ b/src/main/java/g3201_3300/s3267_count_almost_equal_pairs_ii/readme.md @@ -0,0 +1,49 @@ +3267\. Count Almost Equal Pairs II + +Hard + +**Attention**: In this version, the number of operations that can be performed, has been increased to **twice**. + +You are given an array `nums` consisting of positive integers. + +We call two integers `x` and `y` **almost equal** if both integers can become equal after performing the following operation **at most twice**: + +* 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 = [1023,2310,2130,213] + +**Output:** 4 + +**Explanation:** + +The almost equal pairs of elements are: + +* 1023 and 2310. By swapping the digits 1 and 2, and then the digits 0 and 3 in 1023, you get 2310. +* 1023 and 213. By swapping the digits 1 and 0, and then the digits 1 and 2 in 1023, you get 0213, which is 213. +* 2310 and 213. By swapping the digits 2 and 0, and then the digits 3 and 2 in 2310, you get 0213, which is 213. +* 2310 and 2130. By swapping the digits 3 and 1 in 2310, you get 2130. + +**Example 2:** + +**Input:** nums = [1,10,100] + +**Output:** 3 + +**Explanation:** + +The almost equal pairs of elements are: + +* 1 and 10. By swapping the digits 1 and 0 in 10, you get 01 which is 1. +* 1 and 100. By swapping the second 0 with the digit 1 in 100, you get 001, which is 1. +* 10 and 100. By swapping the first 0 with the digit 1 in 100, you get 010, which is 10. + +**Constraints:** + +* `2 <= nums.length <= 5000` +* 1 <= nums[i] < 107 \ No newline at end of file diff --git a/src/test/java/g3201_3300/s3264_final_array_state_after_k_multiplication_operations_i/SolutionTest.java b/src/test/java/g3201_3300/s3264_final_array_state_after_k_multiplication_operations_i/SolutionTest.java new file mode 100644 index 000000000..56d8b43d8 --- /dev/null +++ b/src/test/java/g3201_3300/s3264_final_array_state_after_k_multiplication_operations_i/SolutionTest.java @@ -0,0 +1,21 @@ +package g3201_3300.s3264_final_array_state_after_k_multiplication_operations_i; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void getFinalState() { + assertThat( + new Solution().getFinalState(new int[] {2, 1, 3, 5, 6}, 5, 2), + equalTo(new int[] {8, 4, 6, 5, 6})); + } + + @Test + void getFinalState2() { + assertThat( + new Solution().getFinalState(new int[] {1, 2}, 3, 4), equalTo(new int[] {16, 8})); + } +} diff --git a/src/test/java/g3201_3300/s3265_count_almost_equal_pairs_i/SolutionTest.java b/src/test/java/g3201_3300/s3265_count_almost_equal_pairs_i/SolutionTest.java new file mode 100644 index 000000000..e10d41973 --- /dev/null +++ b/src/test/java/g3201_3300/s3265_count_almost_equal_pairs_i/SolutionTest.java @@ -0,0 +1,23 @@ +package g3201_3300.s3265_count_almost_equal_pairs_i; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void countPairs() { + assertThat(new Solution().countPairs(new int[] {3, 12, 30, 17, 21}), equalTo(2)); + } + + @Test + void countPairs2() { + assertThat(new Solution().countPairs(new int[] {1, 1, 1, 1, 1}), equalTo(10)); + } + + @Test + void countPairs3() { + assertThat(new Solution().countPairs(new int[] {123, 231}), equalTo(0)); + } +} diff --git a/src/test/java/g3201_3300/s3266_final_array_state_after_k_multiplication_operations_ii/SolutionTest.java b/src/test/java/g3201_3300/s3266_final_array_state_after_k_multiplication_operations_ii/SolutionTest.java new file mode 100644 index 000000000..ed1b32740 --- /dev/null +++ b/src/test/java/g3201_3300/s3266_final_array_state_after_k_multiplication_operations_ii/SolutionTest.java @@ -0,0 +1,22 @@ +package g3201_3300.s3266_final_array_state_after_k_multiplication_operations_ii; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void getFinalState() { + assertThat( + new Solution().getFinalState(new int[] {2, 1, 3, 5, 6}, 5, 2), + equalTo(new int[] {8, 4, 6, 5, 6})); + } + + @Test + void getFinalState2() { + assertThat( + new Solution().getFinalState(new int[] {100000, 2000}, 2, 1000000), + equalTo(new int[] {999999307, 999999993})); + } +} diff --git a/src/test/java/g3201_3300/s3267_count_almost_equal_pairs_ii/SolutionTest.java b/src/test/java/g3201_3300/s3267_count_almost_equal_pairs_ii/SolutionTest.java new file mode 100644 index 000000000..4488dc4e8 --- /dev/null +++ b/src/test/java/g3201_3300/s3267_count_almost_equal_pairs_ii/SolutionTest.java @@ -0,0 +1,18 @@ +package g3201_3300.s3267_count_almost_equal_pairs_ii; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void countPairs() { + assertThat(new Solution().countPairs(new int[] {1023, 2310, 2130, 213}), equalTo(4)); + } + + @Test + void countPairs2() { + assertThat(new Solution().countPairs(new int[] {1, 10, 100}), equalTo(3)); + } +}