Skip to content

Commit 07ecc0e

Browse files
committed
Added tasks 3471-3474
1 parent e5ced96 commit 07ecc0e

File tree

13 files changed

+483
-1
lines changed

13 files changed

+483
-1
lines changed

src/main/kotlin/g3401_3500/s3462_maximum_sum_with_at_most_k_elements/Solution.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package g3401_3500.s3462_maximum_sum_with_at_most_k_elements
22

3-
// #Medium #Array #Sorting #Greedy #Matrix #Heap_(Priority_Queue)
3+
// #Medium #Array #Sorting #Greedy #Matrix #Heap_Priority_Queue
44
// #2025_02_25_Time_139_ms_(100.00%)_Space_88.84_MB_(79.31%)
55

66
class Solution {
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package g3401_3500.s3471_find_the_largest_almost_missing_integer
2+
3+
// #Easy #2025_03_02_Time_10_ms_(100.00%)_Space_37.57_MB_(100.00%)
4+
5+
class Solution {
6+
fun largestInteger(nums: IntArray, k: Int): Int {
7+
val freq = IntArray(51)
8+
for (i in 0..nums.size - k) {
9+
val set: MutableSet<Int> = HashSet<Int>()
10+
for (j in i..<i + k) {
11+
set.add(nums[j])
12+
}
13+
for (key in set) {
14+
freq[key]++
15+
}
16+
}
17+
for (i in 50 downTo 0) {
18+
if (freq[i] == 1) {
19+
return i
20+
}
21+
}
22+
return -1
23+
}
24+
}
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: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package g3401_3500.s3472_longest_palindromic_subsequence_after_at_most_k_operations
2+
3+
// #Medium #2025_03_02_Time_137_ms_(100.00%)_Space_99.18_MB_(100.00%)
4+
5+
import kotlin.math.abs
6+
import kotlin.math.max
7+
import kotlin.math.min
8+
9+
class Solution {
10+
fun longestPalindromicSubsequence(s: String, k: Int): Int {
11+
val n = s.length
12+
val arr = Array<IntArray>(26) { IntArray(26) }
13+
for (i in 0..25) {
14+
for (j in 0..25) {
15+
arr[i][j] = min(abs(i - j), (26 - abs(i - j)))
16+
}
17+
}
18+
val dp = Array<Array<IntArray>>(n) { Array<IntArray>(n) { IntArray(k + 1) } }
19+
for (i in 0..<n) {
20+
for (it in 0..k) {
21+
dp[i][i][it] = 1
22+
}
23+
}
24+
for (length in 2..n) {
25+
for (i in 0..n - length) {
26+
val j = i + length - 1
27+
for (it in 0..k) {
28+
if (s[i] == s[j]) {
29+
dp[i][j][it] = 2 + dp[i + 1][j - 1][it]
30+
} else {
31+
val num1 = dp[i + 1][j][it]
32+
val num2 = dp[i][j - 1][it]
33+
val c = arr[s[i].code - 'a'.code][s[j].code - 'a'.code]
34+
val num3 = if (it >= c) 2 + dp[i + 1][j - 1][it - c] else 0
35+
dp[i][j][it] = max(max(num1, num2), num3)
36+
}
37+
}
38+
}
39+
}
40+
return dp[0][n - 1][k]
41+
}
42+
}
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: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package g3401_3500.s3473_sum_of_k_subarrays_with_length_at_least_m
2+
3+
// #Medium #2025_03_02_Time_305_ms_(100.00%)_Space_76.92_MB_(100.00%)
4+
5+
import kotlin.math.max
6+
7+
class Solution {
8+
fun maxSum(nums: IntArray, k: Int, m: Int): Int {
9+
val n = nums.size
10+
// Calculate prefix sums
11+
val prefixSum = IntArray(n + 1)
12+
for (i in 0..<n) {
13+
prefixSum[i + 1] = prefixSum[i] + nums[i]
14+
}
15+
// using elements from nums[0...i-1]
16+
val dp = Array<IntArray>(n + 1) { IntArray(k + 1) }
17+
// Initialize dp array
18+
for (j in 1..k) {
19+
for (i in 0..n) {
20+
dp[i][j] = Int.Companion.MIN_VALUE / 2
21+
}
22+
}
23+
// Fill dp array
24+
for (j in 1..k) {
25+
val maxPrev = IntArray(n + 1)
26+
for (i in 0..<n + 1) {
27+
maxPrev[i] =
28+
if (i == 0) {
29+
dp[0][j - 1] - prefixSum[0]
30+
} else {
31+
max(maxPrev[i - 1], dp[i][j - 1] - prefixSum[i])
32+
}
33+
}
34+
for (i in m..n) {
35+
// Option 1: Don't include the current element in any new subarray
36+
dp[i][j] = dp[i - 1][j]
37+
// Option 2: Form a new subarray ending at position i
38+
// Find the best starting position for the subarray
39+
dp[i][j] = max(dp[i][j], prefixSum[i] + maxPrev[i - m])
40+
}
41+
}
42+
return dp[n][k]
43+
}
44+
}
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: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package g3401_3500.s3474_lexicographically_smallest_generated_string
2+
3+
// #Hard #2025_03_02_Time_41_ms_(100.00%)_Space_39.75_MB_(100.00%)
4+
5+
class Solution {
6+
fun generateString(str1: String, str2: String): String {
7+
val n = str1.length
8+
val m = str2.length
9+
val l = n + m - 1
10+
val word = arrayOfNulls<Char>(l)
11+
for (i in 0..<n) {
12+
if (str1[i] == 'T') {
13+
for (j in 0..<m) {
14+
val pos = i + j
15+
if (word[pos] != null && word[pos] != str2[j]) {
16+
return ""
17+
}
18+
word[pos] = str2[j]
19+
}
20+
}
21+
}
22+
val free = BooleanArray(l)
23+
for (i in 0..<l) {
24+
if (word[i] == null) {
25+
word[i] = 'a'
26+
free[i] = true
27+
}
28+
}
29+
if (n == 0) {
30+
return "a".repeat(l)
31+
}
32+
for (i in 0..<n) {
33+
if (str1[i] == 'F' && intervalEquals(word, str2, i, m)) {
34+
var fixed = false
35+
for (j in m - 1 downTo 0) {
36+
val pos = i + j
37+
if (free[pos]) {
38+
word[pos] = 'b'
39+
free[pos] = false
40+
fixed = true
41+
break
42+
}
43+
}
44+
if (!fixed) {
45+
return ""
46+
}
47+
}
48+
}
49+
val sb = StringBuilder()
50+
for (c in word) {
51+
sb.append(c)
52+
}
53+
return sb.toString()
54+
}
55+
56+
private fun intervalEquals(word: Array<Char?>, str2: String, i: Int, m: Int): Boolean {
57+
for (j in 0..<m) {
58+
if (word[i + j] == null || word[i + j] != str2[j]) {
59+
return false
60+
}
61+
}
62+
return true
63+
}
64+
}
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)