Skip to content

Commit 7264412

Browse files
committed
Added tasks 3442-3446
1 parent 58f26c0 commit 7264412

File tree

12 files changed

+468
-0
lines changed

12 files changed

+468
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package g3401_3500.s3442_maximum_difference_between_even_and_odd_frequency_i;
2+
3+
// #Easy #2025_02_02_Time_1_(100.00%)_Space_42.30_(_%)
4+
5+
import java.util.Arrays;
6+
7+
public class Solution {
8+
public int maxDifference(String s) {
9+
int[] freq = new int[26];
10+
Arrays.fill(freq, 0);
11+
int maxOdd = 0;
12+
int minEven = 1000;
13+
for (int i = 0; i < s.length(); ++i) {
14+
freq[s.charAt(i) - 'a']++;
15+
}
16+
for (int j : freq) {
17+
if (j != 0 && j % 2 == 0) {
18+
minEven = Math.min(minEven, j);
19+
} else {
20+
maxOdd = Math.max(maxOdd, j);
21+
}
22+
}
23+
return maxOdd - minEven;
24+
}
25+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
3442\. Maximum Difference Between Even and Odd Frequency I
2+
3+
Easy
4+
5+
You are given a string `s` consisting of lowercase English letters. Your task is to find the **maximum** difference between the frequency of **two** characters in the string such that:
6+
7+
* One of the characters has an **even frequency** in the string.
8+
* The other character has an **odd frequency** in the string.
9+
10+
Return the **maximum** difference, calculated as the frequency of the character with an **odd** frequency **minus** the frequency of the character with an **even** frequency.
11+
12+
**Example 1:**
13+
14+
**Input:** s = "aaaaabbc"
15+
16+
**Output:** 3
17+
18+
**Explanation:**
19+
20+
* The character `'a'` has an **odd frequency** of `5`, and `'b'` has an **even frequency** of `2`.
21+
* The maximum difference is `5 - 2 = 3`.
22+
23+
**Example 2:**
24+
25+
**Input:** s = "abcabcab"
26+
27+
**Output:** 1
28+
29+
**Explanation:**
30+
31+
* The character `'a'` has an **odd frequency** of `3`, and `'c'` has an **even frequency** of 2.
32+
* The maximum difference is `3 - 2 = 1`.
33+
34+
**Constraints:**
35+
36+
* `3 <= s.length <= 100`
37+
* `s` consists only of lowercase English letters.
38+
* `s` contains at least one character with an odd frequency and one with an even frequency.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package g3401_3500.s3443_maximum_manhattan_distance_after_k_changes;
2+
3+
// #Medium #2025_02_02_Time_189_(_%)_Space_45.65_(100.00%)
4+
5+
public class Solution {
6+
public int maxDistance(String s, int k) {
7+
int ans = 0;
8+
char[][] dir = new char[][] {{'N', 'E'}, {'N', 'W'}, {'S', 'E'}, {'S', 'W'}};
9+
for (char[] d : dir) {
10+
int curr = 0;
11+
int t = k;
12+
for (int i = 0; i < s.length(); ++i) {
13+
if (s.charAt(i) == d[0] || s.charAt(i) == d[1]) {
14+
if (t > 0) {
15+
t--;
16+
curr++;
17+
} else {
18+
curr--;
19+
}
20+
} else {
21+
curr++;
22+
}
23+
ans = Math.max(ans, curr);
24+
}
25+
}
26+
return ans;
27+
}
28+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
3443\. Maximum Manhattan Distance After K Changes
2+
3+
Medium
4+
5+
You are given a string `s` consisting of the characters `'N'`, `'S'`, `'E'`, and `'W'`, where `s[i]` indicates movements in an infinite grid:
6+
7+
* `'N'` : Move north by 1 unit.
8+
* `'S'` : Move south by 1 unit.
9+
* `'E'` : Move east by 1 unit.
10+
* `'W'` : Move west by 1 unit.
11+
12+
Initially, you are at the origin `(0, 0)`. You can change **at most** `k` characters to any of the four directions.
13+
14+
Find the **maximum** **Manhattan distance** from the origin that can be achieved **at any time** while performing the movements **in order**.
15+
16+
The **Manhattan Distance** between two cells <code>(x<sub>i</sub>, y<sub>i</sub>)</code> and <code>(x<sub>j</sub>, y<sub>j</sub>)</code> is <code>|x<sub>i</sub> - x<sub>j</sub>| + |y<sub>i</sub> - y<sub>j</sub>|</code>.
17+
18+
**Example 1:**
19+
20+
**Input:** s = "NWSE", k = 1
21+
22+
**Output:** 3
23+
24+
**Explanation:**
25+
26+
Change `s[2]` from `'S'` to `'N'`. The string `s` becomes `"NWNE"`.
27+
28+
| Movement | Position (x, y) | Manhattan Distance | Maximum |
29+
|-----------------|----------------|--------------------|---------|
30+
| s[0] == 'N' | (0, 1) | 0 + 1 = 1 | 1 |
31+
| s[1] == 'W' | (-1, 1) | 1 + 1 = 2 | 2 |
32+
| s[2] == 'N' | (-1, 2) | 1 + 2 = 3 | 3 |
33+
| s[3] == 'E' | (0, 2) | 0 + 2 = 2 | 3 |
34+
35+
The maximum Manhattan distance from the origin that can be achieved is 3. Hence, 3 is the output.
36+
37+
**Example 2:**
38+
39+
**Input:** s = "NSWWEW", k = 3
40+
41+
**Output:** 6
42+
43+
**Explanation:**
44+
45+
Change `s[1]` from `'S'` to `'N'`, and `s[4]` from `'E'` to `'W'`. The string `s` becomes `"NNWWWW"`.
46+
47+
The maximum Manhattan distance from the origin that can be achieved is 6. Hence, 6 is the output.
48+
49+
**Constraints:**
50+
51+
* <code>1 <= s.length <= 10<sup>5</sup></code>
52+
* `0 <= k <= s.length`
53+
* `s` consists of only `'N'`, `'S'`, `'E'`, and `'W'`.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package g3401_3500.s3444_minimum_increments_for_target_multiples_in_an_array;
2+
3+
// #Hard #2025_02_02_Time_47_(100.00%)_Space_47.37_(100.00%)
4+
5+
public class Solution {
6+
public int minimumIncrements(int[] nums, int[] target) {
7+
int m = target.length;
8+
int fullMask = (1 << m) - 1;
9+
long[] lcmArr = new long[1 << m];
10+
for (int mask = 1; mask < (1 << m); mask++) {
11+
long l = 1;
12+
for (int j = 0; j < m; j++) {
13+
if ((mask & (1 << j)) != 0) {
14+
l = lcm(l, target[j]);
15+
}
16+
}
17+
lcmArr[mask] = l;
18+
}
19+
long inf = Long.MAX_VALUE / 2;
20+
long[] dp = new long[1 << m];
21+
for (int i = 0; i < (1 << m); i++) {
22+
dp[i] = inf;
23+
}
24+
dp[0] = 0;
25+
for (int x : nums) {
26+
long[] newdp = dp.clone();
27+
for (int mask = 1; mask < (1 << m); mask++) {
28+
long l = lcmArr[mask];
29+
long r = x % l;
30+
long cost = (r == 0 ? 0 : l - r);
31+
for (int old = 0; old < (1 << m); old++) {
32+
int newMask = old | mask;
33+
newdp[newMask] = Math.min(newdp[newMask], dp[old] + cost);
34+
}
35+
}
36+
dp = newdp;
37+
}
38+
return (int) dp[fullMask];
39+
}
40+
41+
private long gcd(long a, long b) {
42+
return b == 0 ? a : gcd(b, a % b);
43+
}
44+
45+
private long lcm(long a, long b) {
46+
return a / gcd(a, b) * b;
47+
}
48+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
3444\. Minimum Increments for Target Multiples in an Array
2+
3+
Hard
4+
5+
You are given two arrays, `nums` and `target`.
6+
7+
In a single operation, you may increment any element of `nums` by 1.
8+
9+
Return **the minimum number** of operations required so that each element in `target` has **at least** one multiple in `nums`.
10+
11+
**Example 1:**
12+
13+
**Input:** nums = [1,2,3], target = [4]
14+
15+
**Output:** 1
16+
17+
**Explanation:**
18+
19+
The minimum number of operations required to satisfy the condition is 1.
20+
21+
* Increment 3 to 4 with just one operation, making 4 a multiple of itself.
22+
23+
**Example 2:**
24+
25+
**Input:** nums = [8,4], target = [10,5]
26+
27+
**Output:** 2
28+
29+
**Explanation:**
30+
31+
The minimum number of operations required to satisfy the condition is 2.
32+
33+
* Increment 8 to 10 with 2 operations, making 10 a multiple of both 5 and 10.
34+
35+
**Example 3:**
36+
37+
**Input:** nums = [7,9,10], target = [7]
38+
39+
**Output:** 0
40+
41+
**Explanation:**
42+
43+
Target 7 already has a multiple in nums, so no additional operations are needed.
44+
45+
**Constraints:**
46+
47+
* <code>1 <= nums.length <= 5 * 10<sup>4</sup></code>
48+
* `1 <= target.length <= 4`
49+
* `target.length <= nums.length`
50+
* <code>1 <= nums[i], target[i] <= 10<sup>4</sup></code>
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package g3401_3500.s3445_maximum_difference_between_even_and_odd_frequency_ii;
2+
3+
// #Hard #2025_02_02_Time_97_(100.00%)_Space_52.67_(100.00%)
4+
5+
import java.util.Arrays;
6+
7+
public class Solution {
8+
public int maxDifference(String s, int k) {
9+
int n = s.length();
10+
int[][] pre = new int[5][n];
11+
int[][] closestRight = new int[5][n];
12+
for (int x = 0; x < 5; x++) {
13+
Arrays.fill(closestRight[x], n);
14+
for (int i = 0; i < n; i++) {
15+
int num = s.charAt(i) - '0';
16+
pre[x][i] = (num == x) ? 1 : 0;
17+
if (i > 0) {
18+
pre[x][i] += pre[x][i - 1];
19+
}
20+
}
21+
for (int i = n - 1; i >= 0; i--) {
22+
int num = s.charAt(i) - '0';
23+
if (i < n - 1) {
24+
closestRight[x][i] = closestRight[x][i + 1];
25+
}
26+
if (num == x) {
27+
closestRight[x][i] = i;
28+
}
29+
}
30+
}
31+
int ans = Integer.MIN_VALUE;
32+
for (int a = 0; a <= 4; a++) {
33+
for (int b = 0; b <= 4; b++) {
34+
if (a != b) {
35+
ans = Math.max(ans, go(k, a, b, pre, closestRight, n));
36+
}
37+
}
38+
}
39+
return ans;
40+
}
41+
42+
private int go(int k, int odd, int even, int[][] pre, int[][] closestRight, int n) {
43+
int[][][] suf = new int[2][2][n];
44+
for (int[][] arr2D : suf) {
45+
for (int[] arr : arr2D) {
46+
Arrays.fill(arr, Integer.MIN_VALUE);
47+
}
48+
}
49+
for (int endIdx = 0; endIdx < n; endIdx++) {
50+
int oddParity = pre[odd][endIdx] % 2;
51+
int evenParity = pre[even][endIdx] % 2;
52+
if (pre[odd][endIdx] > 0 && pre[even][endIdx] > 0) {
53+
suf[oddParity][evenParity][endIdx] = pre[odd][endIdx] - pre[even][endIdx];
54+
}
55+
}
56+
for (int oddParity = 0; oddParity < 2; oddParity++) {
57+
for (int evenParity = 0; evenParity < 2; evenParity++) {
58+
for (int endIdx = n - 2; endIdx >= 0; endIdx--) {
59+
suf[oddParity][evenParity][endIdx] =
60+
Math.max(
61+
suf[oddParity][evenParity][endIdx],
62+
suf[oddParity][evenParity][endIdx + 1]);
63+
}
64+
}
65+
}
66+
int ans = Integer.MIN_VALUE;
67+
for (int startIdx = 0; startIdx < n; startIdx++) {
68+
int minEndIdx = startIdx + k - 1;
69+
if (minEndIdx >= n) {
70+
break;
71+
}
72+
int oddBelowI = (startIdx == 0 ? 0 : pre[odd][startIdx - 1]);
73+
int evenBelowI = (startIdx == 0 ? 0 : pre[even][startIdx - 1]);
74+
int goodOddParity = (oddBelowI + 1) % 2;
75+
int goodEvenParity = evenBelowI % 2;
76+
int query =
77+
Math.max(
78+
Math.max(startIdx + k - 1, closestRight[odd][startIdx]),
79+
closestRight[even][startIdx]);
80+
if (query >= n) {
81+
continue;
82+
}
83+
int val = suf[goodOddParity][goodEvenParity][query];
84+
if (val == Integer.MIN_VALUE) {
85+
continue;
86+
}
87+
ans = Math.max(ans, val - oddBelowI + evenBelowI);
88+
}
89+
return ans;
90+
}
91+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
3445\. Maximum Difference Between Even and Odd Frequency II
2+
3+
Hard
4+
5+
You are given a string `s` and an integer `k`. Your task is to find the **maximum** difference between the frequency of **two** characters, `freq[a] - freq[b]`, in a
6+
7+
substring
8+
9+
`subs` of `s`, such that:
10+
11+
* `subs` has a size of **at least** `k`.
12+
* Character `a` has an _odd frequency_ in `subs`.
13+
* Character `b` has an _even frequency_ in `subs`.
14+
15+
Return the **maximum** difference.
16+
17+
**Note** that `subs` can contain more than 2 **distinct** characters.
18+
19+
**Example 1:**
20+
21+
**Input:** s = "12233", k = 4
22+
23+
**Output:** \-1
24+
25+
**Explanation:**
26+
27+
For the substring `"12233"`, the frequency of `'1'` is 1 and the frequency of `'3'` is 2. The difference is `1 - 2 = -1`.
28+
29+
**Example 2:**
30+
31+
**Input:** s = "1122211", k = 3
32+
33+
**Output:** 1
34+
35+
**Explanation:**
36+
37+
For the substring `"11222"`, the frequency of `'2'` is 3 and the frequency of `'1'` is 2. The difference is `3 - 2 = 1`.
38+
39+
**Example 3:**
40+
41+
**Input:** s = "110", k = 3
42+
43+
**Output:** \-1
44+
45+
**Constraints:**
46+
47+
* <code>3 <= s.length <= 3 * 10<sup>4</sup></code>
48+
* `s` consists only of digits `'0'` to `'4'`.
49+
* The input is generated that at least one substring has a character with an even frequency and a character with an odd frequency.
50+
* `1 <= k <= s.length`

0 commit comments

Comments
 (0)