Skip to content

Commit b783a8c

Browse files
authored
Added tasks 3264-3277
1 parent 7723ec6 commit b783a8c

File tree

15 files changed

+1324
-166
lines changed
  • src/main/java
    • g1601_1700/s1616_split_two_strings_to_make_palindrome
    • g3201_3300
      • s3248_snake_in_matrix
      • s3264_final_array_state_after_k_multiplication_operations_i
      • s3265_count_almost_equal_pairs_i
      • s3266_final_array_state_after_k_multiplication_operations_ii
      • s3267_count_almost_equal_pairs_ii
      • s3270_find_the_key_of_the_numbers
      • s3271_hash_divided_string
      • s3272_find_the_count_of_good_integers
      • s3273_minimum_amount_of_damage_dealt_to_bob
      • s3274_check_if_two_chessboard_squares_have_the_same_color
      • s3275_k_th_nearest_obstacle_queries
      • s3276_select_cells_in_grid_with_maximum_score
      • s3277_maximum_xor_score_subarray_queries

15 files changed

+1324
-166
lines changed

README.md

Lines changed: 161 additions & 149 deletions
Large diffs are not rendered by default.

src/main/java/g1601_1700/s1616_split_two_strings_to_make_palindrome/readme.md

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -55,25 +55,29 @@ Then, a<sub>prefix</sub> + b<sub>suffix</sub> = "ula" + "alu" = "ulaalu", which
5555
@SuppressWarnings("java:S2234")
5656
public class Solution {
5757
public boolean checkPalindromeFormation(String a, String b) {
58-
return check(a, b) || check(b, a);
59-
}
60-
61-
private boolean check(String a, String b) {
62-
int i = 0;
63-
int j = b.length() - 1;
64-
while (j > i && a.charAt(i) == b.charAt(j)) {
65-
++i;
66-
--j;
58+
int n = a.length();
59+
int s = 0;
60+
int e = n - 1;
61+
if (isPalindrome(a, b, s, e, true)) {
62+
return true;
63+
} else {
64+
return isPalindrome(b, a, s, e, true);
6765
}
68-
return isPalindrome(a, i, j) || isPalindrome(b, i, j);
6966
}
7067

71-
private boolean isPalindrome(String s, int i, int j) {
72-
while (j > i && s.charAt(i) == s.charAt(j)) {
73-
++i;
74-
--j;
68+
private boolean isPalindrome(String a, String b, int s, int e, boolean check) {
69+
if (s == e) {
70+
return true;
71+
}
72+
while (s < e) {
73+
if (a.charAt(s) != b.charAt(e)) {
74+
return check
75+
&& (isPalindrome(a, a, s, e, false) || isPalindrome(b, b, s, e, false));
76+
}
77+
s++;
78+
e--;
7579
}
76-
return i >= j;
80+
return true;
7781
}
7882
}
7983
```

src/main/java/g3201_3300/s3248_snake_in_matrix/readme.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Return the position of the final cell where the snake ends up after executing `c
2121

2222
**Explanation:**
2323

24-
![image](image01.png)
24+
![image](https://leetcode-in-java.github.io/src/main/java/g3201_3300/s3248_snake_in_matrix/image01.png)
2525

2626
**Example 2:**
2727

@@ -31,7 +31,7 @@ Return the position of the final cell where the snake ends up after executing `c
3131

3232
**Explanation:**
3333

34-
![image](image02.png)
34+
![image](https://leetcode-in-java.github.io/src/main/java/g3201_3300/s3248_snake_in_matrix/image02.png)
3535

3636
**Constraints:**
3737

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Java?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Java)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Java?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Java/fork)
3+
4+
## 3264\. Final Array State After K Multiplication Operations I
5+
6+
Easy
7+
8+
You are given an integer array `nums`, an integer `k`, and an integer `multiplier`.
9+
10+
You need to perform `k` operations on `nums`. In each operation:
11+
12+
* Find the **minimum** value `x` in `nums`. If there are multiple occurrences of the minimum value, select the one that appears **first**.
13+
* Replace the selected minimum value `x` with `x * multiplier`.
14+
15+
Return an integer array denoting the _final state_ of `nums` after performing all `k` operations.
16+
17+
**Example 1:**
18+
19+
**Input:** nums = [2,1,3,5,6], k = 5, multiplier = 2
20+
21+
**Output:** [8,4,6,5,6]
22+
23+
**Explanation:**
24+
25+
| Operation | Result |
26+
|---------------------|------------------|
27+
| After operation 1 | [2, 2, 3, 5, 6] |
28+
| After operation 2 | [4, 2, 3, 5, 6] |
29+
| After operation 3 | [4, 4, 3, 5, 6] |
30+
| After operation 4 | [4, 4, 6, 5, 6] |
31+
| After operation 5 | [8, 4, 6, 5, 6] |
32+
33+
**Example 2:**
34+
35+
**Input:** nums = [1,2], k = 3, multiplier = 4
36+
37+
**Output:** [16,8]
38+
39+
**Explanation:**
40+
41+
| Operation | Result |
42+
|---------------------|-------------|
43+
| After operation 1 | [4, 2] |
44+
| After operation 2 | [4, 8] |
45+
| After operation 3 | [16, 8] |
46+
47+
**Constraints:**
48+
49+
* `1 <= nums.length <= 100`
50+
* `1 <= nums[i] <= 100`
51+
* `1 <= k <= 10`
52+
* `1 <= multiplier <= 5`
53+
54+
## Solution
55+
56+
```java
57+
public class Solution {
58+
public int[] getFinalState(int[] nums, int k, int multiplier) {
59+
while (k-- > 0) {
60+
int min = nums[0];
61+
int index = 0;
62+
for (int i = 0; i < nums.length; i++) {
63+
if (min > nums[i]) {
64+
min = nums[i];
65+
index = i;
66+
}
67+
}
68+
nums[index] = nums[index] * multiplier;
69+
}
70+
return nums;
71+
}
72+
}
73+
```
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Java?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Java)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Java?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Java/fork)
3+
4+
## 3265\. Count Almost Equal Pairs I
5+
6+
Medium
7+
8+
You are given an array `nums` consisting of positive integers.
9+
10+
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**:
11+
12+
* Choose **either** `x` or `y` and swap any two digits within the chosen number.
13+
14+
Return the number of indices `i` and `j` in `nums` where `i < j` such that `nums[i]` and `nums[j]` are **almost equal**.
15+
16+
**Note** that it is allowed for an integer to have leading zeros after performing an operation.
17+
18+
**Example 1:**
19+
20+
**Input:** nums = [3,12,30,17,21]
21+
22+
**Output:** 2
23+
24+
**Explanation:**
25+
26+
The almost equal pairs of elements are:
27+
28+
* 3 and 30. By swapping 3 and 0 in 30, you get 3.
29+
* 12 and 21. By swapping 1 and 2 in 12, you get 21.
30+
31+
**Example 2:**
32+
33+
**Input:** nums = [1,1,1,1,1]
34+
35+
**Output:** 10
36+
37+
**Explanation:**
38+
39+
Every two elements in the array are almost equal.
40+
41+
**Example 3:**
42+
43+
**Input:** nums = [123,231]
44+
45+
**Output:** 0
46+
47+
**Explanation:**
48+
49+
We cannot swap any two digits of 123 or 231 to reach the other.
50+
51+
**Constraints:**
52+
53+
* `2 <= nums.length <= 100`
54+
* <code>1 <= nums[i] <= 10<sup>6</sup></code>
55+
56+
## Solution
57+
58+
```java
59+
public class Solution {
60+
public int countPairs(int[] nums) {
61+
int ans = 0;
62+
for (int i = 0; i < nums.length - 1; i++) {
63+
for (int j = i + 1; j < nums.length; j++) {
64+
if (nums[i] == nums[j]
65+
|| ((nums[j] - nums[i]) % 9 == 0 && check(nums[i], nums[j]))) {
66+
ans++;
67+
}
68+
}
69+
}
70+
return ans;
71+
}
72+
73+
private boolean check(int a, int b) {
74+
int[] ca = new int[10];
75+
int[] cb = new int[10];
76+
int d = 0;
77+
while (a > 0 || b > 0) {
78+
if (a % 10 != b % 10) {
79+
d++;
80+
if (d > 2) {
81+
return false;
82+
}
83+
}
84+
ca[a % 10]++;
85+
cb[b % 10]++;
86+
a /= 10;
87+
b /= 10;
88+
}
89+
return d == 2 && areEqual(ca, cb);
90+
}
91+
92+
private boolean areEqual(int[] a, int[] b) {
93+
for (int i = 0; i < 10; i++) {
94+
if (a[i] != b[i]) {
95+
return false;
96+
}
97+
}
98+
return true;
99+
}
100+
}
101+
```
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Java?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Java)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Java?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Java/fork)
3+
4+
## 3266\. Final Array State After K Multiplication Operations II
5+
6+
Hard
7+
8+
You are given an integer array `nums`, an integer `k`, and an integer `multiplier`.
9+
10+
You need to perform `k` operations on `nums`. In each operation:
11+
12+
* Find the **minimum** value `x` in `nums`. If there are multiple occurrences of the minimum value, select the one that appears **first**.
13+
* Replace the selected minimum value `x` with `x * multiplier`.
14+
15+
After the `k` operations, apply **modulo** <code>10<sup>9</sup> + 7</code> to every value in `nums`.
16+
17+
Return an integer array denoting the _final state_ of `nums` after performing all `k` operations and then applying the modulo.
18+
19+
**Example 1:**
20+
21+
**Input:** nums = [2,1,3,5,6], k = 5, multiplier = 2
22+
23+
**Output:** [8,4,6,5,6]
24+
25+
**Explanation:**
26+
27+
| Operation | Result |
28+
|-------------------------|------------------|
29+
| After operation 1 | [2, 2, 3, 5, 6] |
30+
| After operation 2 | [4, 2, 3, 5, 6] |
31+
| After operation 3 | [4, 4, 3, 5, 6] |
32+
| After operation 4 | [4, 4, 6, 5, 6] |
33+
| After operation 5 | [8, 4, 6, 5, 6] |
34+
| After applying modulo | [8, 4, 6, 5, 6] |
35+
36+
**Example 2:**
37+
38+
**Input:** nums = [100000,2000], k = 2, multiplier = 1000000
39+
40+
**Output:** [999999307,999999993]
41+
42+
**Explanation:**
43+
44+
| Operation | Result |
45+
|-------------------------|----------------------|
46+
| After operation 1 | [100000, 2000000000] |
47+
| After operation 2 | [100000000000, 2000000000] |
48+
| After applying modulo | [999999307, 999999993] |
49+
50+
**Constraints:**
51+
52+
* <code>1 <= nums.length <= 10<sup>4</sup></code>
53+
* <code>1 <= nums[i] <= 10<sup>9</sup></code>
54+
* <code>1 <= k <= 10<sup>9</sup></code>
55+
* <code>1 <= multiplier <= 10<sup>6</sup></code>
56+
57+
## Solution
58+
59+
```java
60+
import java.util.Arrays;
61+
import java.util.PriorityQueue;
62+
63+
public class Solution {
64+
private static final int MOD = 1_000_000_007;
65+
66+
public int[] getFinalState(int[] nums, int k, int multiplier) {
67+
if (multiplier == 1) {
68+
return nums;
69+
}
70+
int n = nums.length;
71+
int mx = 0;
72+
for (int x : nums) {
73+
mx = Math.max(mx, x);
74+
}
75+
long[] a = new long[n];
76+
int left = k;
77+
boolean shouldExit = false;
78+
for (int i = 0; i < n && !shouldExit; i++) {
79+
long x = nums[i];
80+
while (x < mx) {
81+
x *= multiplier;
82+
if (--left < 0) {
83+
shouldExit = true;
84+
break;
85+
}
86+
}
87+
a[i] = x;
88+
}
89+
if (left < 0) {
90+
PriorityQueue<long[]> pq =
91+
new PriorityQueue<>(
92+
(p, q) ->
93+
p[0] != q[0]
94+
? Long.compare(p[0], q[0])
95+
: Long.compare(p[1], q[1]));
96+
for (int i = 0; i < n; i++) {
97+
pq.offer(new long[] {nums[i], i});
98+
}
99+
while (k-- > 0) {
100+
long[] p = pq.poll();
101+
p[0] *= multiplier;
102+
pq.offer(p);
103+
}
104+
while (!pq.isEmpty()) {
105+
long[] p = pq.poll();
106+
nums[(int) p[1]] = (int) (p[0] % MOD);
107+
}
108+
return nums;
109+
}
110+
111+
Integer[] ids = new Integer[n];
112+
Arrays.setAll(ids, i -> i);
113+
Arrays.sort(ids, (i, j) -> Long.compare(a[i], a[j]));
114+
k = left;
115+
long pow1 = pow(multiplier, k / n);
116+
long pow2 = pow1 * multiplier % MOD;
117+
for (int i = 0; i < n; i++) {
118+
int j = ids[i];
119+
nums[j] = (int) (a[j] % MOD * (i < k % n ? pow2 : pow1) % MOD);
120+
}
121+
return nums;
122+
}
123+
124+
private long pow(long x, int n) {
125+
long res = 1;
126+
for (; n > 0; n /= 2) {
127+
if (n % 2 > 0) {
128+
res = res * x % MOD;
129+
}
130+
x = x * x % MOD;
131+
}
132+
return res;
133+
}
134+
}
135+
```

0 commit comments

Comments
 (0)