Skip to content

Commit 018cdfb

Browse files
committed
Added tasks 3471-3474
1 parent d365b3f commit 018cdfb

File tree

12 files changed

+449
-0
lines changed

12 files changed

+449
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package g3401_3500.s3471_find_the_largest_almost_missing_integer;
2+
3+
// #Easy #2025_03_02_Time_4_ms_(100.00%)_Space_44.26_MB_(100.00%)
4+
5+
import java.util.HashSet;
6+
import java.util.Set;
7+
8+
public class Solution {
9+
public int largestInteger(int[] nums, int k) {
10+
int[] freq = new int[51];
11+
for (int i = 0; i <= nums.length - k; i++) {
12+
Set<Integer> set = new HashSet<>();
13+
for (int j = i; j < i + k; j++) {
14+
set.add(nums[j]);
15+
}
16+
for (int key : set) {
17+
freq[key]++;
18+
}
19+
}
20+
for (int i = 50; i >= 0; i--) {
21+
if (freq[i] == 1) {
22+
return i;
23+
}
24+
}
25+
return -1;
26+
}
27+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
3471\. Find the Largest Almost Missing Integer
2+
3+
Easy
4+
5+
You are given an integer array `nums` and an integer `k`.
6+
7+
An integer `x` is **almost missing** from `nums` if `x` appears in _exactly_ one subarray of size `k` within `nums`.
8+
9+
Return the **largest** **almost missing** integer from `nums`. If no such integer exists, return `-1`.
10+
11+
A **subarray** is a contiguous sequence of elements within an array.
12+
13+
**Example 1:**
14+
15+
**Input:** nums = [3,9,2,1,7], k = 3
16+
17+
**Output:** 7
18+
19+
**Explanation:**
20+
21+
* 1 appears in 2 subarrays of size 3: `[9, 2, 1]` and `[2, 1, 7]`.
22+
* 2 appears in 3 subarrays of size 3: `[3, 9, 2]`, `[9, 2, 1]`, `[2, 1, 7]`.
23+
* 3 appears in 1 subarray of size 3: `[3, 9, 2]`.
24+
* 7 appears in 1 subarray of size 3: `[2, 1, 7]`.
25+
* 9 appears in 2 subarrays of size 3: `[3, 9, 2]`, and `[9, 2, 1]`.
26+
27+
We return 7 since it is the largest integer that appears in exactly one subarray of size `k`.
28+
29+
**Example 2:**
30+
31+
**Input:** nums = [3,9,7,2,1,7], k = 4
32+
33+
**Output:** 3
34+
35+
**Explanation:**
36+
37+
* 1 appears in 2 subarrays of size 4: `[9, 7, 2, 1]`, `[7, 2, 1, 7]`.
38+
* 2 appears in 3 subarrays of size 4: `[3, 9, 7, 2]`, `[9, 7, 2, 1]`, `[7, 2, 1, 7]`.
39+
* 3 appears in 1 subarray of size 4: `[3, 9, 7, 2]`.
40+
* 7 appears in 3 subarrays of size 4: `[3, 9, 7, 2]`, `[9, 7, 2, 1]`, `[7, 2, 1, 7]`.
41+
* 9 appears in 2 subarrays of size 4: `[3, 9, 7, 2]`, `[9, 7, 2, 1]`.
42+
43+
We return 3 since it is the largest and only integer that appears in exactly one subarray of size `k`.
44+
45+
**Example 3:**
46+
47+
**Input:** nums = [0,0], k = 1
48+
49+
**Output:** \-1
50+
51+
**Explanation:**
52+
53+
There is no integer that appears in only one subarray of size 1.
54+
55+
**Constraints:**
56+
57+
* `1 <= nums.length <= 50`
58+
* `0 <= nums[i] <= 50`
59+
* `1 <= k <= nums.length`
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package g3401_3500.s3472_longest_palindromic_subsequence_after_at_most_k_operations;
2+
3+
// #Medium #2025_03_02_Time_153_ms_(100.00%)_Space_87.64_MB_(100.00%)
4+
5+
public class Solution {
6+
public int longestPalindromicSubsequence(String s, int k) {
7+
int n = s.length();
8+
int[][] arr = new int[26][26];
9+
for (int i = 0; i < 26; i++) {
10+
for (int j = 0; j < 26; j++) {
11+
arr[i][j] = Math.min(Math.abs(i - j), 26 - Math.abs(i - j));
12+
}
13+
}
14+
int[][][] dp = new int[n][n][k + 1];
15+
for (int i = 0; i < n; i++) {
16+
for (int it = 0; it <= k; it++) {
17+
dp[i][i][it] = 1;
18+
}
19+
}
20+
for (int length = 2; length <= n; length++) {
21+
for (int i = 0; i <= n - length; i++) {
22+
int j = i + length - 1;
23+
for (int it = 0; it <= k; it++) {
24+
if (s.charAt(i) == s.charAt(j)) {
25+
dp[i][j][it] = 2 + dp[i + 1][j - 1][it];
26+
} else {
27+
int num1 = dp[i + 1][j][it];
28+
int num2 = dp[i][j - 1][it];
29+
int c = arr[s.charAt(i) - 'a'][s.charAt(j) - 'a'];
30+
int num3 = (it >= c) ? 2 + dp[i + 1][j - 1][it - c] : 0;
31+
dp[i][j][it] = Math.max(Math.max(num1, num2), num3);
32+
}
33+
}
34+
}
35+
}
36+
return dp[0][n - 1][k];
37+
}
38+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
3472\. Longest Palindromic Subsequence After at Most K Operations
2+
3+
Medium
4+
5+
You are given a string `s` and an integer `k`.
6+
7+
In one operation, you can replace the character at any position with the next or previous letter in the alphabet (wrapping around so that `'a'` is after `'z'`). For example, replacing `'a'` with the next letter results in `'b'`, and replacing `'a'` with the previous letter results in `'z'`. Similarly, replacing `'z'` with the next letter results in `'a'`, and replacing `'z'` with the previous letter results in `'y'`.
8+
9+
Return the length of the **longest palindromic subsequence** of `s` that can be obtained after performing **at most** `k` operations.
10+
11+
**Example 1:**
12+
13+
**Input:** s = "abced", k = 2
14+
15+
**Output:** 3
16+
17+
**Explanation:**
18+
19+
* Replace `s[1]` with the next letter, and `s` becomes `"acced"`.
20+
* Replace `s[4]` with the previous letter, and `s` becomes `"accec"`.
21+
22+
The subsequence `"ccc"` forms a palindrome of length 3, which is the maximum.
23+
24+
**Example 2:**
25+
26+
**Input:** s = "aaazzz", k = 4
27+
28+
**Output:** 6
29+
30+
**Explanation:**
31+
32+
* Replace `s[0]` with the previous letter, and `s` becomes `"zaazzz"`.
33+
* Replace `s[4]` with the next letter, and `s` becomes `"zaazaz"`.
34+
* Replace `s[3]` with the next letter, and `s` becomes `"zaaaaz"`.
35+
36+
The entire string forms a palindrome of length 6.
37+
38+
**Constraints:**
39+
40+
* `1 <= s.length <= 200`
41+
* `1 <= k <= 200`
42+
* `s` consists of only lowercase English letters.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package g3401_3500.s3473_sum_of_k_subarrays_with_length_at_least_m;
2+
3+
// #Medium #2025_03_02_Time_198_ms_(100.00%)_Space_83.70_MB_(100.00%)
4+
5+
public class Solution {
6+
public int maxSum(int[] nums, int k, int m) {
7+
int n = nums.length;
8+
// Calculate prefix sums
9+
int[] prefixSum = new int[n + 1];
10+
for (int i = 0; i < n; i++) {
11+
prefixSum[i + 1] = prefixSum[i] + nums[i];
12+
}
13+
// using elements from nums[0...i-1]
14+
int[][] dp = new int[n + 1][k + 1];
15+
// Initialize dp array
16+
for (int j = 1; j <= k; j++) {
17+
for (int i = 0; i <= n; i++) {
18+
dp[i][j] = Integer.MIN_VALUE / 2;
19+
}
20+
}
21+
// Fill dp array
22+
for (int j = 1; j <= k; j++) {
23+
int[] maxPrev = new int[n + 1];
24+
for (int i = 0; i < n + 1; i++) {
25+
maxPrev[i] =
26+
(i == 0)
27+
? dp[0][j - 1] - prefixSum[0]
28+
: Math.max(maxPrev[i - 1], dp[i][j - 1] - prefixSum[i]);
29+
}
30+
for (int i = m; i <= n; i++) {
31+
// Option 1: Don't include the current element in any new subarray
32+
dp[i][j] = dp[i - 1][j];
33+
// Option 2: Form a new subarray ending at position i
34+
// Find the best starting position for the subarray
35+
dp[i][j] = Math.max(dp[i][j], prefixSum[i] + maxPrev[i - m]);
36+
}
37+
}
38+
return dp[n][k];
39+
}
40+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
3473\. Sum of K Subarrays With Length at Least M
2+
3+
Medium
4+
5+
You are given an integer array `nums` and two integers, `k` and `m`.
6+
7+
Return the **maximum** sum of `k` non-overlapping subarrays of `nums`, where each subarray has a length of **at least** `m`.
8+
9+
**Example 1:**
10+
11+
**Input:** nums = [1,2,-1,3,3,4], k = 2, m = 2
12+
13+
**Output:** 13
14+
15+
**Explanation:**
16+
17+
The optimal choice is:
18+
19+
* Subarray `nums[3..5]` with sum `3 + 3 + 4 = 10` (length is `3 >= m`).
20+
* Subarray `nums[0..1]` with sum `1 + 2 = 3` (length is `2 >= m`).
21+
22+
The total sum is `10 + 3 = 13`.
23+
24+
**Example 2:**
25+
26+
**Input:** nums = [-10,3,-1,-2], k = 4, m = 1
27+
28+
**Output:** \-10
29+
30+
**Explanation:**
31+
32+
The optimal choice is choosing each element as a subarray. The output is `(-10) + 3 + (-1) + (-2) = -10`.
33+
34+
**Constraints:**
35+
36+
* `1 <= nums.length <= 2000`
37+
* <code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code>
38+
* `1 <= k <= floor(nums.length / m)`
39+
* `1 <= m <= 3`
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package g3401_3500.s3474_lexicographically_smallest_generated_string;
2+
3+
// #Hard #2025_03_02_Time_16_ms_(100.00%)_Space_45.37_MB_(100.00%)
4+
5+
public class Solution {
6+
public String generateString(String str1, String str2) {
7+
int n = str1.length();
8+
int m = str2.length();
9+
int l = n + m - 1;
10+
Character[] word = new Character[l];
11+
for (int i = 0; i < n; i++) {
12+
if (str1.charAt(i) == 'T') {
13+
for (int j = 0; j < m; j++) {
14+
int pos = i + j;
15+
if (word[pos] != null && word[pos] != str2.charAt(j)) {
16+
return "";
17+
}
18+
word[pos] = str2.charAt(j);
19+
}
20+
}
21+
}
22+
boolean[] free = new boolean[l];
23+
for (int i = 0; i < l; i++) {
24+
if (word[i] == null) {
25+
word[i] = 'a';
26+
free[i] = true;
27+
}
28+
}
29+
if (n == 0) {
30+
return String.join("", java.util.Collections.nCopies(l, "a"));
31+
}
32+
for (int i = 0; i < n; i++) {
33+
if (str1.charAt(i) == 'F') {
34+
if (intervalEquals(word, str2, i, m)) {
35+
boolean fixed = false;
36+
for (int j = m - 1; j >= 0; j--) {
37+
int pos = i + j;
38+
if (free[pos]) {
39+
word[pos] = 'b';
40+
free[pos] = false;
41+
fixed = true;
42+
break;
43+
}
44+
}
45+
if (!fixed) {
46+
return "";
47+
}
48+
}
49+
}
50+
}
51+
StringBuilder sb = new StringBuilder();
52+
for (Character c : word) {
53+
sb.append(c);
54+
}
55+
return sb.toString();
56+
}
57+
58+
private boolean intervalEquals(Character[] word, String str2, int i, int m) {
59+
for (int j = 0; j < m; j++) {
60+
if (word[i + j] == null || word[i + j] != str2.charAt(j)) {
61+
return false;
62+
}
63+
}
64+
return true;
65+
}
66+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
3474\. Lexicographically Smallest Generated String
2+
3+
Hard
4+
5+
You are given two strings, `str1` and `str2`, of lengths `n` and `m`, respectively.
6+
7+
A string `word` of length `n + m - 1` is defined to be **generated** by `str1` and `str2` if it satisfies the following conditions for **each** index `0 <= i <= n - 1`:
8+
9+
* If `str1[i] == 'T'`, the **substring** of `word` with size `m` starting at index `i` is **equal** to `str2`, i.e., `word[i..(i + m - 1)] == str2`.
10+
* If `str1[i] == 'F'`, the **substring** of `word` with size `m` starting at index `i` is **not equal** to `str2`, i.e., `word[i..(i + m - 1)] != str2`.
11+
12+
Return the **lexicographically smallest** possible string that can be **generated** by `str1` and `str2`. If no string can be generated, return an empty string `""`.
13+
14+
**Example 1:**
15+
16+
**Input:** str1 = "TFTF", str2 = "ab"
17+
18+
**Output:** "ababa"
19+
20+
**Explanation:**
21+
22+
#### The table below represents the string `"ababa"`
23+
24+
| Index | T/F | Substring of length `m` |
25+
|-------|-----|-------------------------|
26+
| 0 | 'T' | "ab" |
27+
| 1 | 'F' | "ba" |
28+
| 2 | 'T' | "ab" |
29+
| 3 | 'F' | "ba" |
30+
31+
The strings `"ababa"` and `"ababb"` can be generated by `str1` and `str2`.
32+
33+
Return `"ababa"` since it is the lexicographically smaller string.
34+
35+
**Example 2:**
36+
37+
**Input:** str1 = "TFTF", str2 = "abc"
38+
39+
**Output:** ""
40+
41+
**Explanation:**
42+
43+
No string that satisfies the conditions can be generated.
44+
45+
**Example 3:**
46+
47+
**Input:** str1 = "F", str2 = "d"
48+
49+
**Output:** "a"
50+
51+
**Constraints:**
52+
53+
* <code>1 <= n == str1.length <= 10<sup>4</sup></code>
54+
* `1 <= m == str2.length <= 500`
55+
* `str1` consists only of `'T'` or `'F'`.
56+
* `str2` consists only of lowercase English characters.

0 commit comments

Comments
 (0)