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 a220e4c

Browse files
committed
第464场周赛T1~T4 (4)
1 parent b1df751 commit a220e4c

File tree

8 files changed

+329
-0
lines changed

8 files changed

+329
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
public class Solution3658 {
2+
public int gcdOfOddEvenSums(int n) {
3+
long sumOdd = 0, sumEven = 0;
4+
int odd = 1, even = 2;
5+
for (int i = 0; i < n; i++) {
6+
sumOdd += odd;
7+
sumEven += even;
8+
odd += 2;
9+
even += 2;
10+
}
11+
return (int) getGCD(sumOdd, sumEven);
12+
}
13+
14+
private long getGCD(long num1, long num2) {
15+
return num1 == 0 ? num2 : getGCD(num2 % num1, num1);
16+
}
17+
}
18+
/*
19+
3658. 奇数和与偶数和的最大公约数
20+
https://leetcode.cn/problems/gcd-of-odd-and-even-sums/description/
21+
22+
第 464 场周赛 T1。
23+
24+
给你一个整数 n。请你计算以下两个值的 最大公约数(GCD):
25+
- sumOdd:前 n 个奇数的总和。
26+
- sumEven:前 n 个偶数的总和。
27+
返回 sumOdd 和 sumEven 的 GCD。
28+
提示:
29+
1 <= n <= 1000
30+
31+
模拟 / 数学。
32+
sumOdd = n^2
33+
sumEven = n(n+1)
34+
最大公约数为 n。
35+
时间复杂度 O(1)。
36+
*/
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import java.util.HashMap;
2+
import java.util.Map;
3+
4+
public class Solution3659 {
5+
public boolean partitionArray(int[] nums, int k) {
6+
int n = nums.length;
7+
if (n % k != 0) return false;
8+
9+
Map<Integer, Integer> mp = new HashMap<>();
10+
int max_cnt = 0;
11+
for (int v : nums) {
12+
max_cnt = Math.max(max_cnt, mp.merge(v, 1, Integer::sum));
13+
}
14+
return max_cnt <= n / k;
15+
}
16+
}
17+
/*
18+
3659. 数组元素分组
19+
https://leetcode.cn/problems/partition-array-into-k-distinct-groups/description/
20+
21+
第 464 场周赛 T2。
22+
23+
给你一个整数数组 nums 和一个整数 k。
24+
请你判断是否可以将 nums 中的所有元素分成一个或多个组,使得:
25+
- 每个组 恰好 包含 k 个 不同的 元素。
26+
- nums 中的每个元素 必须 被分配到 恰好一个 组中。
27+
如果可以完成这样的分组,返回 true;否则,返回 false。
28+
提示:
29+
1 <= nums.length <= 10^5
30+
1 <= nums[i] <= 10^5
31+
1 <= k <= nums.length
32+
33+
构造。n 必须能整除 k,且频次最高元素不能超过 k。
34+
时间复杂度 O(n)。
35+
*/
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
public class Solution3660 {
2+
public int[] maxValue(int[] nums) {
3+
int n = nums.length;
4+
int[] preMax = new int[n];
5+
preMax[0] = nums[0];
6+
for (int i = 1; i < n; i++) {
7+
preMax[i] = Math.max(preMax[i - 1], nums[i]);
8+
}
9+
10+
int sufMin = Integer.MAX_VALUE;
11+
int mx = 0;
12+
for (int i = n - 1; i >= 0; i--) {
13+
if (preMax[i] <= sufMin) {
14+
mx = preMax[i]; // 无法跳到 [i+1,n-1] 中,只能跳到 [0,i] 中的最大值
15+
}
16+
sufMin = Math.min(sufMin, nums[i]);
17+
nums[i] = mx;
18+
}
19+
return nums;
20+
}
21+
}
22+
/*
23+
3660. 跳跃游戏 9
24+
https://leetcode.cn/problems/jump-game-ix/description/
25+
26+
第 464 场周赛 T3。
27+
28+
给你一个整数数组 nums。
29+
从任意下标 i 出发,你可以根据以下规则跳跃到另一个下标 j:
30+
- 仅当 nums[j] < nums[i] 时,才允许跳跃到下标 j,其中 j > i。
31+
- 仅当 nums[j] > nums[i] 时,才允许跳跃到下标 j,其中 j < i。
32+
对于每个下标 i,找出从 i 出发且可以跳跃 任意 次,能够到达 nums 中的 最大值 是多少。
33+
返回一个数组 ans,其中 ans[i] 是从下标 i 出发可以到达的最大值。
34+
提示:
35+
1 <= nums.length <= 10^5
36+
1 <= nums[i] <= 10^9
37+
38+
结论 + 动态规划。
39+
题目很绕。翻译一下就是 如果左边比右边大,可以从右跳到左,也可以从左跳到右;
40+
设 f(i) 表示前 i 个元素的最大值,g(i) 表示第 i 到第 n 个元素的最小值。
41+
每个 f(i) <= g(i+1) 的位置就把整个序列分成了很多段,每一段的答案就是当前段的最大值。
42+
时间复杂度 O(n)。
43+
rating 2173 (clist.by)
44+
*/
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import java.util.Arrays;
2+
import java.util.Comparator;
3+
4+
public class Solution3661 {
5+
private int[][] a;
6+
private int[] walls;
7+
private int[][] memo;
8+
9+
public int maxWalls(int[] robots, int[] distance, int[] walls) {
10+
int n = robots.length;
11+
this.walls = walls;
12+
a = new int[n][2];
13+
for (int i = 0; i < n; i++) {
14+
a[i][0] = robots[i];
15+
a[i][1] = distance[i];
16+
}
17+
Arrays.sort(a, Comparator.comparingInt(p -> p[0]));
18+
Arrays.sort(walls);
19+
20+
memo = new int[n][2];
21+
for (int[] row : memo) {
22+
Arrays.fill(row, -1); // -1 表示没有计算过
23+
}
24+
return dfs(n - 1, 1);
25+
}
26+
27+
private int dfs(int i, int j) {
28+
if (i < 0) return 0;
29+
// 之前计算过
30+
if (memo[i][j] != -1) return memo[i][j];
31+
32+
// 往左射
33+
int leftX = a[i][0] - a[i][1];
34+
if (i > 0) {
35+
leftX = Math.max(leftX, a[i - 1][0] + 1); // 不能到达左边那个机器人
36+
}
37+
int left = lowerBound(walls, leftX);
38+
int cur = lowerBound(walls, a[i][0] + 1);
39+
int res = dfs(i - 1, 0) + cur - left; // [left, cur-1] 中的墙都能摧毁
40+
41+
// 往右射
42+
int rightX = a[i][0] + a[i][1];
43+
if (i + 1 < a.length) {
44+
int x2 = a[i + 1][0];
45+
if (j == 0) { // 右边那个机器人往左射
46+
x2 -= a[i + 1][1];
47+
}
48+
rightX = Math.min(rightX, x2 - 1); // 不能到达右边那个机器人(或者他往左射到的墙)
49+
}
50+
int right = lowerBound(walls, rightX + 1);
51+
cur = lowerBound(walls, a[i][0]);
52+
res = Math.max(res, dfs(i - 1, 1) + right - cur); // [cur, right-1] 中的墙都能摧毁
53+
return memo[i][j] = res; // 记忆化
54+
}
55+
56+
private int lowerBound(int[] a, int key) {
57+
int l = 0, r = a.length;
58+
while (l < r) {
59+
int m = l + (r - l) / 2;
60+
if (a[m] >= key) r = m;
61+
else l = m + 1;
62+
}
63+
return l;
64+
}
65+
}
66+
/*
67+
3661. 可以被机器人摧毁的最大墙壁数目
68+
https://leetcode.cn/problems/maximum-walls-destroyed-by-robots/description/
69+
70+
第 464 场周赛 T4。
71+
72+
一条无限长的直线上分布着一些机器人和墙壁。给你整数数组 robots ,distance 和 walls:
73+
- robots[i] 是第 i 个机器人的位置。
74+
- distance[i] 是第 i 个机器人的子弹可以行进的 最大 距离。
75+
- walls[j] 是第 j 堵墙的位置。
76+
每个机器人有 一颗 子弹,可以向左或向右发射,最远距离为 distance[i] 米。
77+
子弹会摧毁其射程内路径上的每一堵墙。机器人是固定的障碍物:如果子弹在到达墙壁前击中另一个机器人,它会 立即 在该机器人处停止,无法继续前进。
78+
返回机器人可以摧毁墙壁的 最大 数量。
79+
注意:
80+
- 墙壁和机器人可能在同一位置;该位置的墙壁可以被该位置的机器人摧毁。
81+
- 机器人不会被子弹摧毁。
82+
提示:
83+
1 <= robots.length == distance.length <= 10^5
84+
1 <= walls.length <= 10^5
85+
1 <= robots[i], walls[j] <= 10^9
86+
1 <= distance[i] <= 10^5
87+
robots 中的所有值都是 互不相同 的
88+
walls 中的所有值都是 互不相同 的
89+
90+
记忆化搜索 + 二分。
91+
时间复杂度 O(nlogn + mlogm + nlogm),其中 n 是 robots 的长度,m 是 walls 的长度。
92+
rating 2505 (clist.by)
93+
*/
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 Solution3658Tests {
5+
private final Solution3658 solution3658 = new Solution3658();
6+
7+
@Test
8+
public void example1() {
9+
int n = 4;
10+
int expected = 4;
11+
Assertions.assertEquals(expected, solution3658.gcdOfOddEvenSums(n));
12+
}
13+
14+
@Test
15+
public void example2() {
16+
int n = 5;
17+
int expected = 5;
18+
Assertions.assertEquals(expected, solution3658.gcdOfOddEvenSums(n));
19+
}
20+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import org.junit.jupiter.api.Assertions;
2+
import org.junit.jupiter.api.Test;
3+
4+
public class Solution3659Tests {
5+
private final Solution3659 solution3659 = new Solution3659();
6+
7+
@Test
8+
public void example1() {
9+
int[] nums = {1, 2, 3, 4};
10+
int k = 2;
11+
Assertions.assertTrue(solution3659.partitionArray(nums, k));
12+
}
13+
14+
@Test
15+
public void example2() {
16+
int[] nums = {3, 5, 2, 2};
17+
int k = 2;
18+
Assertions.assertTrue(solution3659.partitionArray(nums, k));
19+
}
20+
21+
@Test
22+
public void example3() {
23+
int[] nums = {1, 5, 2, 3};
24+
int k = 3;
25+
Assertions.assertFalse(solution3659.partitionArray(nums, k));
26+
}
27+
}
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 Solution3660Tests {
5+
private final Solution3660 solution3660 = new Solution3660();
6+
7+
@Test
8+
public void example1() {
9+
int[] nums = {2, 1, 3};
10+
int[] expected = {2, 2, 3};
11+
Assertions.assertArrayEquals(expected, solution3660.maxValue(nums));
12+
}
13+
14+
@Test
15+
public void example2() {
16+
int[] nums = {2, 3, 1};
17+
int[] expected = {3, 3, 3};
18+
Assertions.assertArrayEquals(expected, solution3660.maxValue(nums));
19+
}
20+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import org.junit.jupiter.api.Assertions;
2+
import org.junit.jupiter.api.Test;
3+
4+
public class Solution3661Tests {
5+
private final Solution3661 solution3661 = new Solution3661();
6+
7+
@Test
8+
public void example1() {
9+
int[] robots = {4};
10+
int[] distance = {3};
11+
int[] walls = {1, 10};
12+
int expected = 1;
13+
Assertions.assertEquals(expected, solution3661.maxWalls(robots, distance, walls));
14+
}
15+
16+
@Test
17+
public void example2() {
18+
int[] robots = {10, 2};
19+
int[] distance = {5, 1};
20+
int[] walls = {5, 2, 7};
21+
int expected = 3;
22+
Assertions.assertEquals(expected, solution3661.maxWalls(robots, distance, walls));
23+
}
24+
25+
@Test
26+
public void example3() {
27+
int[] robots = {1, 2};
28+
int[] distance = {100, 1};
29+
int[] walls = {10};
30+
int expected = 0;
31+
Assertions.assertEquals(expected, solution3661.maxWalls(robots, distance, walls));
32+
}
33+
34+
// 补充用例
35+
@Test
36+
public void example4() {
37+
// https://leetcode.cn/problems/maximum-walls-destroyed-by-robots/submissions/655939484/
38+
int[] robots = {31, 36, 18, 39, 10, 21, 40, 69, 57, 51, 19, 32, 50, 53, 3, 28, 9, 59, 46, 22, 13, 63, 33, 14, 25, 52, 64, 5, 44, 17, 68, 45};
39+
int[] distance = {5, 3, 4, 6, 3, 4, 2, 4, 6, 5, 4, 3, 1, 4, 3, 4, 5, 3, 1, 3, 2, 2, 4, 1, 3, 2, 4, 2, 2, 1, 4, 6};
40+
int[] walls = {10, 41, 54, 6, 7, 33, 14, 30, 9, 12, 38, 27, 39, 52, 42, 46, 45, 17, 2, 15, 55, 44, 4, 18, 20, 34, 28, 51, 11, 13, 37, 31, 23, 24, 5, 56, 22, 57, 35, 29, 32, 26, 48, 47, 16, 43, 36, 25, 53, 19, 40, 49, 21, 1, 50, 3, 8};
41+
int expected = 56;
42+
Assertions.assertEquals(expected, solution3661.maxWalls(robots, distance, walls));
43+
}
44+
45+
@Test
46+
public void example5() {
47+
// https://leetcode.cn/problems/maximum-walls-destroyed-by-robots/submissions/655951952/
48+
int[] robots = {12, 6, 34, 28, 58, 10, 42, 4, 55, 48, 3, 7, 26, 40, 54, 17, 32, 23, 56, 43, 31, 1, 24};
49+
int[] distance = {2, 5, 1, 6, 3, 8, 6, 4, 5, 6, 9, 3, 3, 8, 6, 3, 5, 7, 5, 7, 5, 7, 7};
50+
int[] walls = {25, 37, 48, 94, 56, 55, 30, 28, 40, 35, 15, 97, 12, 6, 85, 14, 52, 60, 27, 71, 5, 19, 73, 9, 92, 32, 11, 63, 13, 8, 39, 88, 44, 79, 7, 17, 78, 93, 33, 80, 26, 46, 68, 47, 95, 24, 36, 49, 90, 70, 64, 18, 57, 50, 66, 51, 54, 2, 21, 86, 16, 61, 91, 75, 58, 38, 65, 45, 29, 69, 3, 53, 1, 87, 83, 59, 23, 10, 62, 76, 43, 96, 77, 31, 4, 34, 42, 67, 89, 41, 82, 72, 22, 20, 81, 84, 74};
51+
int expected = 61;
52+
Assertions.assertEquals(expected, solution3661.maxWalls(robots, distance, walls));
53+
}
54+
}

0 commit comments

Comments
(0)

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