Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3627e25

Browse files
committed
第439场周赛T1~T4 (4)
1 parent fe8a245 commit 3627e25

File tree

4 files changed

+236
-0
lines changed

4 files changed

+236
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import java.util.Arrays;
2+
3+
public class Solution3471 {
4+
public int largestInteger(int[] nums, int k) {
5+
int n = nums.length;
6+
int[] cnt = new int[51];
7+
for (int v : nums) cnt[v]++;
8+
9+
int ans = -1;
10+
if (k == n) {
11+
return Arrays.stream(nums).max().orElseThrow();
12+
} else if (k == 1) {
13+
for (int v : nums) {
14+
if (cnt[v] == 1) ans = Math.max(ans, v);
15+
}
16+
return ans;
17+
} else {
18+
if (cnt[nums[0]] == 1) ans = Math.max(ans, nums[0]);
19+
if (cnt[nums[n - 1]] == 1) ans = Math.max(ans, nums[n - 1]);
20+
return ans;
21+
}
22+
}
23+
}
24+
/*
25+
3471. 找出最大的几近缺失整数
26+
https://leetcode.cn/problems/find-the-largest-almost-missing-integer/description/
27+
28+
第 439 场周赛 T1。
29+
30+
给你一个整数数组 nums 和一个整数 k 。
31+
如果整数 x 恰好仅出现在 nums 中的一个大小为 k 的子数组中,则认为 x 是 nums 中的几近缺失(almost missing)整数。
32+
返回 nums 中 最大的几近缺失 整数,如果不存在这样的整数,返回 -1 。
33+
子数组 是数组中的一个连续元素序列。
34+
提示:
35+
1 <= nums.length <= 50
36+
0 <= nums[i] <= 50
37+
1 <= k <= nums.length
38+
39+
分类讨论。特别容易漏掉 k == n 的情况。
40+
时间复杂度 O(n)。
41+
*/
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import java.util.Arrays;
2+
3+
public class Solution3472 {
4+
private char[] s;
5+
private int[][][] memo;
6+
7+
public int longestPalindromicSubsequence(String S, int k) {
8+
this.s = S.toCharArray();
9+
int n = s.length;
10+
memo = new int[n][n][k + 1];
11+
for (int i = 0; i < n; i++) {
12+
for (int j = 0; j < n; j++) {
13+
Arrays.fill(memo[i][j], -1);
14+
}
15+
}
16+
return dfs(0, n - 1, k);
17+
}
18+
19+
private int dfs(int i, int j, int k) {
20+
if (i >= j) return j - i + 1; // i=j+1 时返回 0,i=j 时返回 1
21+
if (memo[i][j][k] != -1) return memo[i][j][k];
22+
int res = Math.max(dfs(i + 1, j, k), dfs(i, j - 1, k)); // 枚举哪个不选
23+
int d = Math.abs(s[i] - s[j]);
24+
int cost = Math.min(d, 26 - d);
25+
if (k - cost >= 0) {
26+
res = Math.max(res, dfs(i + 1, j - 1, k - cost) + 2);
27+
}
28+
return memo[i][j][k] = res;
29+
}
30+
}
31+
/*
32+
3472. 至多 K 次操作后的最长回文子序列
33+
https://leetcode.cn/problems/longest-palindromic-subsequence-after-at-most-k-operations/description/
34+
35+
第 439 场周赛 T2。
36+
37+
给你一个字符串 s 和一个整数 k。
38+
在一次操作中,你可以将任意位置的字符替换为字母表中相邻的字符(字母表是循环的,因此 'z' 的下一个字母是 'a')。例如,将 'a' 替换为下一个字母结果是 'b',将 'a' 替换为上一个字母结果是 'z';同样,将 'z' 替换为下一个字母结果是 'a',替换为上一个字母结果是 'y'。
39+
返回在进行 最多 k 次操作后,s 的 最长回文子序列 的长度。
40+
子序列 是一个 非空 字符串,可以通过删除原字符串中的某些字符(或不删除任何字符)并保持剩余字符的相对顺序得到。
41+
回文 是正着读和反着读都相同的字符串。
42+
提示:
43+
1 <= s.length <= 200
44+
1 <= k <= 200
45+
s 仅由小写英文字母组成。
46+
47+
区间 DP。
48+
时间复杂度 O(n^2 * k)。
49+
*/
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
public class Solution3473 {
2+
public int maxSum(int[] nums, int k, int m) {
3+
int n = nums.length;
4+
long[] ps = new long[n + 1];
5+
for (int i = 0; i < n; i++) {
6+
ps[i + 1] = ps[i] + nums[i];
7+
}
8+
9+
long[][] f = new long[k + 1][n + 1];
10+
for (int i = 1; i <= k; i++) {
11+
long mx = Long.MIN_VALUE / 2;
12+
// 3077 version
13+
// long w = (i % 2 > 0 ? 1 : -1) * (k - i + 1);
14+
// f[i][i - 1] = mx;
15+
// for (int j = i; j <= n - (k - i); j++) {
16+
// mx = Math.max(mx, f[i - 1][j - 1] - ps[j - 1] * w);
17+
// f[i][j] = Math.max(f[i][j - 1], ps[j] * w + mx);
18+
// }
19+
for (int j = i * m - m; j < i * m; j++) {
20+
f[i][j] = mx;
21+
}
22+
for (int j = i * m; j <= n - (k - i) * m; j++) {
23+
mx = Math.max(mx, f[i - 1][j - m] - ps[j - m]);
24+
f[i][j] = Math.max(f[i][j - 1], ps[j] + mx);
25+
}
26+
}
27+
return (int) f[k][n];
28+
}
29+
}
30+
/*
31+
3473. 长度至少为 M 的 K 个子数组之和
32+
https://leetcode.cn/problems/sum-of-k-subarrays-with-length-at-least-m/description/
33+
34+
第 439 场周赛 T3。
35+
36+
给你一个整数数组 nums 和两个整数 k 和 m。
37+
返回数组 nums 中 k 个不重叠子数组的 最大 和,其中每个子数组的长度 至少 为 m。
38+
子数组 是数组中的一个连续序列。
39+
提示:
40+
1 <= nums.length <= 2000
41+
-10^4 <= nums[i] <= 10^4
42+
1 <= k <= floor(nums.length / m)
43+
1 <= m <= 3
44+
45+
划分型 DP + 前缀和优化。
46+
和 3077 题 几乎是一样的题目,一年前的 T4 hard,时过境迁,今年竟然放 T3 medium 了,
47+
(作为 T4 时候 clist rating 分是 2586,今天 T3 rating 分是 2460)
48+
时间复杂度 O(nk)。
49+
相似题目: 3077. K 个不相交子数组的最大能量值
50+
https://leetcode.cn/problems/maximum-strength-of-k-disjoint-subarrays/description/
51+
*/
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import java.util.Arrays;
2+
3+
public class Solution3474 {
4+
public String generateString(String str1, String str2) {
5+
int n = str1.length();
6+
int m = str2.length();
7+
char[] s1 = str1.toCharArray();
8+
char[] s2 = str2.toCharArray();
9+
int total_len = n + m - 1;
10+
char[] res = new char[total_len];
11+
Arrays.fill(res, '#');
12+
boolean[] fixed = new boolean[total_len];
13+
14+
for (int i = 0; i < n; i++) {
15+
if (s1[i] == 'T') {
16+
for (int j = 0; j < m; j++) {
17+
int pos = i + j;
18+
if (pos >= total_len) return "";
19+
if (res[pos] != '#' && res[pos] != s2[j]) return "";
20+
res[pos] = s2[j];
21+
fixed[pos] = true;
22+
}
23+
}
24+
}
25+
for (int i = 0; i < total_len; i++) {
26+
if (res[i] == '#') res[i] = 'a';
27+
}
28+
29+
for (int i = 0; i < n; i++) {
30+
if (s1[i] == 'F') {
31+
boolean equal = true;
32+
for (int j = 0; j < m; j++) {
33+
int pos = i + j;
34+
if (res[pos] != s2[j]) {
35+
equal = false;
36+
break;
37+
}
38+
}
39+
if (!equal) continue;
40+
boolean modified = false;
41+
for (int j = 0; j < m; j++) {
42+
int pos = (i + m - 1) - j;
43+
if (!fixed[pos]) {
44+
res[pos] = (s2[j] != 'a') ? 'a' : 'b';
45+
modified = true;
46+
break;
47+
}
48+
}
49+
if (!modified) return "";
50+
}
51+
}
52+
53+
for (int i = 0; i < n; i++) {
54+
if (s1[i] == 'F') {
55+
boolean equal = true;
56+
for (int j = 0; j < m; j++) {
57+
int pos = i + j;
58+
if (res[pos] != s2[j]) {
59+
equal = false;
60+
break;
61+
}
62+
}
63+
if (equal) return "";
64+
}
65+
}
66+
return new String(res);
67+
}
68+
}
69+
/*
70+
3474. 字典序最小的生成字符串
71+
https://leetcode.cn/problems/lexicographically-smallest-generated-string/description/
72+
73+
第 439 场周赛 T4。
74+
75+
给你两个字符串,str1 和 str2,其长度分别为 n 和 m 。
76+
如果一个长度为 n + m - 1 的字符串 word 的每个下标 0 <= i <= n - 1 都满足以下条件,则称其由 str1 和 str2 生成:
77+
- 如果 str1[i] == 'T',则长度为 m 的 子字符串(从下标 i 开始)与 str2 相等,即 word[i..(i + m - 1)] == str2。
78+
- 如果 str1[i] == 'F',则长度为 m 的 子字符串(从下标 i 开始)与 str2 不相等,即 word[i..(i + m - 1)] != str2。
79+
返回可以由 str1 和 str2 生成 的 字典序最小 的字符串。如果不存在满足条件的字符串,返回空字符串 ""。
80+
如果字符串 a 在第一个不同字符的位置上比字符串 b 的对应字符在字母表中更靠前,则称字符串 a 的 字典序 小于 字符串 b。
81+
如果前 min(a.length, b.length) 个字符都相同,则较短的字符串字典序更小。
82+
子字符串 是字符串中的一个连续、非空 的字符序列。
83+
提示:
84+
1 <= n == str1.length <= 10^4
85+
1 <= m == str2.length <= 500
86+
str1 仅由 'T' 或 'F' 组成。
87+
str2 仅由小写英文字母组成。
88+
89+
贪心 + 暴力匹配。
90+
1. 处理 'T' 条件:遍历第一个字符串的所有位置,如果遇到 'T',则设置生成的字符串对应位置的字符为第二个字符串的对应字符,并标记这些位置为固定。
91+
2. 填充默认值:将所有未被固定的位置填充为 'a',以确保字典序最小。
92+
3. 处理 'F' 条件:检查每个 'F' 位置的子字符串是否等于第二个字符串。如果相等,找到最后一个未被固定的位置,将其修改为最小的可能字符,使其不等于对应位置的字符。
93+
4. 最终验证:再次检查所有 'F' 条件,确保所有条件均被满足,否则返回空字符串。
94+
时间复杂度 O(nm)。
95+
*/

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /