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 7d69b5c

Browse files
committed
第446场周赛T1~T4 (4)
1 parent 5cf7709 commit 7d69b5c

File tree

8 files changed

+360
-0
lines changed

8 files changed

+360
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
public class Solution3522 {
2+
public long calculateScore(String[] instructions, int[] values) {
3+
int n = instructions.length;
4+
boolean[] vis = new boolean[n];
5+
long ans = 0;
6+
int i = 0;
7+
while (i >= 0 && i < n) {
8+
if (vis[i]) break;
9+
vis[i] = true;
10+
if ("add".equals(instructions[i])) {
11+
ans += values[i];
12+
i++;
13+
} else {
14+
i += values[i];
15+
}
16+
}
17+
return ans;
18+
}
19+
}
20+
/*
21+
3522. 执行指令后的得分
22+
https://leetcode.cn/problems/calculate-score-after-performing-instructions/description/
23+
24+
第 446 场周赛 T1。
25+
26+
给你两个数组:instructions 和 values,数组的长度均为 n。
27+
你需要根据以下规则模拟一个过程:
28+
- 从下标 i = 0 的第一个指令开始,初始得分为 0。
29+
- 如果 instructions[i] 是 "add":
30+
- 将 values[i] 加到你的得分中。
31+
- 移动到下一个指令 (i + 1)。
32+
如果 instructions[i] 是 "jump":
33+
移动到下标为 (i + values[i]) 的指令,但不修改你的得分。
34+
当以下任一情况发生时,过程会终止:
35+
- 越界(即 i < 0 或 i >= n),或
36+
- 尝试再次执行已经执行过的指令。被重复访问的指令不会再次执行。
37+
返回过程结束时的得分。
38+
提示:
39+
n == instructions.length == values.length
40+
1 <= n <= 10^5
41+
instructions[i] 只能是 "add" 或 "jump"。
42+
-10^5 <= values[i] <= 10^5
43+
44+
中国时间 2025年04月20日 周日 10:30
45+
广州·中肿。前1天。
46+
模拟。
47+
时间复杂度 O(n)。
48+
*/
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
public class Solution3523 {
2+
public int maximumPossibleSize(int[] nums) {
3+
int ans = 0;
4+
int mx = 0;
5+
for (int v : nums) {
6+
if (v >= mx) {
7+
mx = v;
8+
ans++;
9+
}
10+
}
11+
return ans;
12+
}
13+
}
14+
/*
15+
3523. 非递减数组的最大长度
16+
https://leetcode.cn/problems/make-array-non-decreasing/description/
17+
18+
第 446 场周赛 T2。
19+
20+
给你一个整数数组 nums。在一次操作中,你可以选择一个子数组,并将其替换为一个等于该子数组 最大值 的单个元素。
21+
返回经过零次或多次操作后,数组仍为 非递减 的情况下,数组 可能的最大长度。
22+
子数组 是数组中一个连续、非空 的元素序列。
23+
提示:
24+
1 <= nums.length <= 2 * 10^5
25+
1 <= nums[i] <= 2 * 10^5
26+
27+
贪心。
28+
如果在元素 x=nums[i] 的左边,有比 x 大的数 y,那么 x 必须去掉。
29+
时间复杂度 O(n)。
30+
*/
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
public class Solution3524 {
2+
public long[] resultArray(int[] nums, int k) {
3+
int n = nums.length;
4+
long[] ans = new long[k];
5+
// 定义 f[i+1][x] 表示右端点为 i 的、元素积模 k 等于 x 的子数组的个数。+1 是为了方便用 f[0] 表示初始值。
6+
int[][] f = new int[n + 1][k];
7+
for (int i = 0; i < n; i++) {
8+
int v = nums[i] % k;
9+
f[i + 1][v] = 1;
10+
for (int y = 0; y < k; y++) {
11+
f[i + 1][y * v % k] += f[i][y]; // 刷表法
12+
}
13+
for (int x = 0; x < k; x++) {
14+
ans[x] += f[i + 1][x];
15+
}
16+
}
17+
return ans;
18+
}
19+
}
20+
/*
21+
3524. 求出数组的 X 值 I
22+
https://leetcode.cn/problems/find-x-value-of-array-i/description/
23+
24+
第 446 场周赛 T3。
25+
26+
给你一个由 正 整数组成的数组 nums,以及一个 正 整数 k。
27+
你可以对 nums 执行 一次 操作,该操作中可以移除任意 不重叠 的前缀和后缀,使得 nums 仍然 非空 。
28+
你需要找出 nums 的 x 值,即在执行操作后,剩余元素的 乘积 除以 k 后的 余数 为 x 的操作数量。
29+
返回一个大小为 k 的数组 result,其中 result[x] 表示对于 0 <= x <= k - 1,nums 的 x 值。
30+
数组的 前缀 指从数组起始位置开始到数组中任意位置的一段连续子数组。
31+
数组的 后缀 是指从数组中任意位置开始到数组末尾的一段连续子数组。
32+
子数组 是数组中一段连续的元素序列。
33+
注意,在操作中选择的前缀和后缀可以是 空的 。
34+
提示:
35+
1 <= nums[i] <= 10^9
36+
1 <= nums.length <= 10^5
37+
1 <= k <= 5
38+
39+
子数组 DP + 刷表法。
40+
https://leetcode.cn/problems/find-x-value-of-array-i/solutions/3656580/zi-shu-zu-dp-shua-biao-fa-pythonjavacgo-8lonk/
41+
题意:对于 x=0,1,2,...,k−1,计算有多少个非空连续子数组的元素积模 k 等于 x。
42+
时间复杂度 O(nk)。
43+
*/
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
public class Solution3525 {
2+
public int[] resultArray(int[] nums, int k, int[][] queries) {
3+
int n = nums.length;
4+
int q = queries.length;
5+
6+
InfoSegmentTree seg = new InfoSegmentTree(n, k);
7+
seg.build(nums, 1, 0, n - 1);
8+
9+
int[] ans = new int[q];
10+
for (int qi = 0; qi < q; qi++) {
11+
int[] p = queries[qi];
12+
seg.modify(1, 0, n - 1, p[0], p[1]);
13+
int x = p[3];
14+
ans[qi] = seg.query(1, 0, n - 1, p[2], n - 1).cnt[x];
15+
}
16+
return ans;
17+
}
18+
19+
// 线段树模板,只需要实现 mergeInfo 和 _do,其余都是固定的
20+
static class InfoSegmentTree {
21+
static class Info {
22+
int mul; // 整个区间 [l,r] 的元素积模 k 的结果。这可以用左右子树的 mul 相乘模 k 得到。
23+
int[] cnt; // 其中 cnt[x] 表示在左端点为 l,右端点为 l,l+1,l+2,...,r 的子数组中,元素积模 k 等于 x 的子数组的个数。计算规则就是上文的分治。
24+
25+
public Info(int mul, int[] cnt) {
26+
this.mul = mul;
27+
this.cnt = cnt;
28+
}
29+
}
30+
31+
Info mergeInfo(Info a, Info b) {
32+
int[] cnt = a.cnt.clone();
33+
for (int rx = 0; rx < k; rx++) {
34+
cnt[a.mul * rx % k] += b.cnt[rx];
35+
}
36+
return new Info(a.mul * b.mul % k, cnt);
37+
}
38+
39+
Info newData(int val) {
40+
int mul = val % k;
41+
int[] cnt = new int[k];
42+
cnt[mul] = 1;
43+
return new Info(mul, cnt);
44+
}
45+
46+
int n;
47+
Info[] info;
48+
int k;
49+
50+
public InfoSegmentTree(int n, int k) {
51+
this.n = n;
52+
info = new Info[4 * n];
53+
this.k = k;
54+
}
55+
56+
void build(int[] A, int p, int l, int r) {
57+
if (l == r) {
58+
info[p] = newData(A[l]);
59+
return;
60+
}
61+
int m = (l + r) >> 1;
62+
build(A, p << 1, l, m);
63+
build(A, p << 1 | 1, m + 1, r);
64+
maintain(p, l, r);
65+
}
66+
67+
void maintain(int p, int l, int r) {
68+
info[p] = mergeInfo(info[p << 1], info[p << 1 | 1]);
69+
}
70+
71+
void modify(int p, int l, int r, int i, int qv) {
72+
if (l == r) {
73+
info[p] = newData(qv);
74+
return;
75+
}
76+
int m = (l + r) >> 1;
77+
if (i <= m) modify(p << 1, l, m, i, qv);
78+
else modify(p << 1 | 1, m + 1, r, i, qv);
79+
maintain(p, l, r);
80+
}
81+
82+
Info query(int p, int l, int r, int ql, int qr) {
83+
if (ql <= l && r <= qr) {
84+
return info[p];
85+
}
86+
int m = (l + r) >> 1;
87+
if (qr <= m) return query(p << 1, l, m, ql, qr);
88+
if (ql > m) return query(p << 1 | 1, m + 1, r, ql, qr);
89+
return mergeInfo(query(p << 1, l, m, ql, qr), query(p << 1 | 1, m + 1, r, ql, qr));
90+
}
91+
}
92+
}
93+
/*
94+
3525. 求出数组的 X 值 II
95+
https://leetcode.cn/problems/find-x-value-of-array-ii/description/
96+
97+
第 446 场周赛 T4。
98+
99+
给你一个由 正整数 组成的数组 nums 和一个 正整数 k。同时给你一个二维数组 queries,其中 queries[i] = [indexi, valuei, starti, xi]。
100+
你可以对 nums 执行 一次 操作,移除 nums 的任意 后缀 ,使得 nums 仍然非空。
101+
给定一个 x,nums 的 x值 定义为执行以上操作后剩余元素的 乘积 除以 k 的 余数 为 x 的方案数。
102+
对于 queries 中的每个查询,你需要执行以下操作,然后确定 xi 对应的 nums 的 x值:
103+
- 将 nums[indexi] 更新为 valuei。仅这个更改在接下来的所有查询中保留。
104+
- 移除 前缀 nums[0..(starti - 1)](nums[0..(-1)] 表示 空前缀 )。
105+
返回一个长度为 queries.length 的数组 result,其中 result[i] 是第 i 个查询的答案。
106+
数组的一个 前缀 是从数组开始位置到任意位置的子数组。
107+
数组的一个 后缀 是从数组中任意位置开始直到结束的子数组。
108+
子数组 是数组中一段连续的元素序列。
109+
注意:操作中所选的前缀或后缀可以是 空的 。
110+
注意:x值在本题中与问题 I 有不同的定义。
111+
提示:
112+
1 <= nums[i] <= 10^9
113+
1 <= nums.length <= 10^5
114+
1 <= k <= 5
115+
1 <= queries.length <= 2 * 10^4
116+
queries[i] == [indexi, valuei, starti, xi]
117+
0 <= indexi <= nums.length - 1
118+
1 <= valuei <= 10^9
119+
0 <= starti <= nums.length - 1
120+
0 <= xi <= k - 1
121+
122+
注意本题的子数组和上一题不一样,本题只能移除后缀。
123+
https://leetcode.cn/problems/find-x-value-of-array-ii/solutions/3656583/dan-dian-xiu-gai-xian-duan-shu-by-endles-i3jy/
124+
时间复杂度 O((n+qlogn)k)。
125+
rating 2647 (clist.by)
126+
*/
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import org.junit.jupiter.api.Assertions;
2+
import org.junit.jupiter.api.Test;
3+
4+
public class Solution3522Tests {
5+
private final Solution3522 solution3522 = new Solution3522();
6+
7+
@Test
8+
public void example1() {
9+
String[] instructions = {"jump", "add", "add", "jump", "add", "jump"};
10+
int[] values = {2, 1, 3, 1, -2, -3};
11+
long expected = 1;
12+
Assertions.assertEquals(expected, solution3522.calculateScore(instructions, values));
13+
}
14+
15+
@Test
16+
public void example2() {
17+
String[] instructions = {"jump", "add", "add"};
18+
int[] values = {3, 1, 1};
19+
long expected = 0;
20+
Assertions.assertEquals(expected, solution3522.calculateScore(instructions, values));
21+
}
22+
23+
@Test
24+
public void example3() {
25+
String[] instructions = {"jump"};
26+
int[] values = {0};
27+
long expected = 0;
28+
Assertions.assertEquals(expected, solution3522.calculateScore(instructions, values));
29+
}
30+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import org.junit.jupiter.api.Assertions;
2+
import org.junit.jupiter.api.Test;
3+
4+
public class Solution3523Tests {
5+
private final Solution3523 solution3523 = new Solution3523();
6+
7+
@Test
8+
public void example1() {
9+
int[] nums = {4, 2, 5, 3, 5};
10+
int expected = 3;
11+
Assertions.assertEquals(expected, solution3523.maximumPossibleSize(nums));
12+
}
13+
14+
@Test
15+
public void example2() {
16+
int[] nums = {1, 2, 3};
17+
int expected = 3;
18+
Assertions.assertEquals(expected, solution3523.maximumPossibleSize(nums));
19+
}
20+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import org.junit.jupiter.api.Assertions;
2+
import org.junit.jupiter.api.Test;
3+
4+
public class Solution3524Tests {
5+
private final Solution3524 solution3524 = new Solution3524();
6+
7+
@Test
8+
public void example1() {
9+
int[] nums = {1, 2, 3, 4, 5};
10+
int k = 3;
11+
long[] expected = {9, 2, 4};
12+
Assertions.assertArrayEquals(expected, solution3524.resultArray(nums, k));
13+
}
14+
15+
@Test
16+
public void example2() {
17+
int[] nums = {1, 2, 4, 8, 16, 32};
18+
int k = 4;
19+
long[] expected = {18, 1, 2, 0};
20+
Assertions.assertArrayEquals(expected, solution3524.resultArray(nums, k));
21+
}
22+
23+
@Test
24+
public void example3() {
25+
int[] nums = {1, 1, 2, 1, 1};
26+
int k = 2;
27+
long[] expected = {9, 6};
28+
Assertions.assertArrayEquals(expected, solution3524.resultArray(nums, k));
29+
}
30+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import org.junit.jupiter.api.Assertions;
2+
import org.junit.jupiter.api.Test;
3+
4+
public class Solution3525Tests {
5+
private final Solution3525 solution3525 = new Solution3525();
6+
7+
@Test
8+
public void example1() {
9+
int[] nums = {1, 2, 3, 4, 5};
10+
int k = 3;
11+
int[][] queries = UtUtils.stringToInts2("[[2,2,0,2],[3,3,3,0],[0,1,0,1]]");
12+
int[] expected = {2, 2, 2};
13+
Assertions.assertArrayEquals(expected, solution3525.resultArray(nums, k, queries));
14+
}
15+
16+
@Test
17+
public void example2() {
18+
int[] nums = {1, 2, 4, 8, 16, 32};
19+
int k = 4;
20+
int[][] queries = UtUtils.stringToInts2("[[0,2,0,2],[0,2,0,1]]");
21+
int[] expected = {1, 0};
22+
Assertions.assertArrayEquals(expected, solution3525.resultArray(nums, k, queries));
23+
}
24+
25+
@Test
26+
public void example3() {
27+
int[] nums = {1, 1, 2, 1, 1};
28+
int k = 2;
29+
int[][] queries = UtUtils.stringToInts2("[[2,1,0,1]]");
30+
int[] expected = {5};
31+
Assertions.assertArrayEquals(expected, solution3525.resultArray(nums, k, queries));
32+
}
33+
}

0 commit comments

Comments
(0)

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