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 893268c

Browse files
committed
第442场周赛T1~T4 (4)
1 parent d7eea25 commit 893268c

File tree

8 files changed

+280
-0
lines changed

8 files changed

+280
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
public class Solution3492 {
2+
public int maxContainers(int n, int w, int maxWeight) {
3+
return Math.min(maxWeight / w, n * n);
4+
}
5+
}
6+
/*
7+
3492. 船上可以装载的最大集装箱数量
8+
https://leetcode.cn/problems/maximum-containers-on-a-ship/description/
9+
10+
第 442 场周赛 T1。
11+
12+
给你一个正整数 n,表示船上的一个 n x n 的货物甲板。甲板上的每个单元格可以装载一个重量 恰好 为 w 的集装箱。
13+
然而,如果将所有集装箱装载到甲板上,其总重量不能超过船的最大承载重量 maxWeight。
14+
请返回可以装载到船上的 最大 集装箱数量。
15+
提示:
16+
1 <= n <= 1000
17+
1 <= w <= 1000
18+
1 <= maxWeight <= 10^9
19+
20+
数学。
21+
时间复杂度 O(1)。
22+
*/
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import java.util.HashSet;
2+
import java.util.Set;
3+
4+
public class Solution3493 {
5+
public int numberOfComponents(int[][] properties, int k) {
6+
int n = properties.length;
7+
DSU dsu = new DSU(n);
8+
for (int i = 0; i < n; i++) {
9+
for (int j = i + 1; j < n; j++) {
10+
if (intersectGeK(properties[i], properties[j], k)) {
11+
dsu.union(i, j);
12+
}
13+
}
14+
}
15+
return dsu.sz;
16+
}
17+
18+
// intersect >= k
19+
private boolean intersectGeK(int[] property1, int[] property2, int k) {
20+
Set<Integer> set = new HashSet<>();
21+
for (int v : property1) set.add(v);
22+
Set<Integer> set2 = new HashSet<>();
23+
for (int v : property2) set2.add(v);
24+
set.retainAll(set2);
25+
return set.size() >= k;
26+
}
27+
28+
static class DSU {
29+
int[] fa;
30+
int sz;
31+
32+
public DSU(int n) {
33+
fa = new int[n];
34+
for (int i = 0; i < n; i++) fa[i] = i;
35+
sz = n;
36+
}
37+
38+
int find(int x) { // 查找
39+
return x == fa[x] ? fa[x] : (fa[x] = find(fa[x]));
40+
}
41+
42+
void union(int p, int q) { // 合并
43+
p = find(p);
44+
q = find(q);
45+
if (p == q) return;
46+
fa[q] = p;
47+
sz--;
48+
}
49+
}
50+
}
51+
/*
52+
3493. 属性图
53+
https://leetcode.cn/problems/properties-graph/description/
54+
55+
第 442 场周赛 T2。
56+
57+
给你一个二维整数数组 properties,其维度为 n x m,以及一个整数 k。
58+
定义一个函数 intersect(a, b),它返回数组 a 和 b 中 共有的不同整数的数量 。
59+
构造一个 无向图,其中每个索引 i 对应 properties[i]。如果且仅当 intersect(properties[i], properties[j]) >= k(其中 i 和 j 的范围为 [0, n - 1] 且 i != j),节点 i 和节点 j 之间有一条边。
60+
返回结果图中 连通分量 的数量。
61+
提示:
62+
1 <= n == properties.length <= 100
63+
1 <= m == properties[i].length <= 100
64+
1 <= properties[i][j] <= 100
65+
1 <= k <= m
66+
67+
并查集。
68+
时间复杂度 O(n^2 * m)。
69+
*/
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
public class Solution3494 {
2+
public long minTime(int[] skill, int[] mana) {
3+
int n = skill.length;
4+
int m = mana.length;
5+
6+
// f[i][j] 表示 药水 i 被巫师 j 处理完的时间,答案为 f[m][n]
7+
long[][] f = new long[m + 1][n + 1];
8+
for (int i = 1; i <= m; i++) {
9+
for (int j = 1; j <= n; j++) {
10+
f[i][j] = Math.max(f[i][j - 1], f[i - 1][j]) + (long) skill[j - 1] * mana[i - 1];
11+
}
12+
// 校准
13+
for (int j = n - 1; j >= 1; j--) {
14+
f[i][j] = f[i][j + 1] - (long) skill[j + 1 - 1] * mana[i - 1];
15+
}
16+
}
17+
return f[m][n];
18+
}
19+
}
20+
/*
21+
3494. 酿造药水需要的最少总时间
22+
https://leetcode.cn/problems/find-the-minimum-amount-of-time-to-brew-potions/description/
23+
24+
第 442 场周赛 T3。
25+
26+
给你两个长度分别为 n 和 m 的整数数组 skill 和 mana 。
27+
在一个实验室里,有 n 个巫师,他们必须按顺序酿造 m 个药水。每个药水的法力值为 mana[j],并且每个药水 必须 依次通过 所有 巫师处理,才能完成酿造。第 i 个巫师在第 j 个药水上处理需要的时间为 timeij = skill[i] * mana[j]。
28+
由于酿造过程非常精细,药水在当前巫师完成工作后 必须 立即传递给下一个巫师并开始处理。这意味着时间必须保持 同步,确保每个巫师在药水到达时 马上 开始工作。
29+
返回酿造所有药水所需的 最短 总时间。
30+
提示:
31+
n == skill.length
32+
m == mana.length
33+
1 <= n, m <= 5000
34+
1 <= mana[i], skill[i] <= 5000
35+
36+
正向贪心 + 反向校准。
37+
时间复杂度 O(mn)。
38+
*/
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
public class Solution3495 {
2+
public long minOperations(int[][] queries) {
3+
long ans = 0;
4+
for (int[] q : queries) {
5+
int l = q[0], r = q[1];
6+
long sum_level = 0;
7+
int k = 1;
8+
int prev = 1;
9+
int curr = 4;
10+
while (prev <= r) {
11+
int start_k = prev;
12+
int end_k = curr - 1;
13+
int overlap_start = Math.max(l, start_k);
14+
int overlap_end = Math.min(r, end_k);
15+
if (overlap_start <= overlap_end) {
16+
long count = overlap_end - overlap_start + 1;
17+
sum_level += count * k;
18+
}
19+
prev = curr;
20+
curr *= 4;
21+
k++;
22+
}
23+
ans += (sum_level + 1) / 2;
24+
}
25+
return ans;
26+
}
27+
}
28+
/*
29+
3495. 使数组元素都变为零的最少操作次数
30+
https://leetcode.cn/problems/minimum-operations-to-make-array-elements-zero/description/
31+
32+
第 442 场周赛 T4。
33+
34+
给你一个二维数组 queries,其中 queries[i] 形式为 [l, r]。每个 queries[i] 表示了一个元素范围从 l 到 r (包括 l 和 r )的整数数组 nums 。
35+
在一次操作中,你可以:
36+
选择一个查询数组中的两个整数 a 和 b。
37+
将它们替换为 floor(a / 4) 和 floor(b / 4)。
38+
你的任务是确定对于每个查询,将数组中的所有元素都变为零的 最少 操作次数。返回所有查询结果的总和。
39+
提示:
40+
1 <= queries.length <= 10^5
41+
queries[i].length == 2
42+
queries[i] == [l, r]
43+
1 <= l < r <= 10^9
44+
45+
数学。
46+
时间复杂度 O(qlogR)。
47+
*/
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import org.junit.jupiter.api.Assertions;
2+
import org.junit.jupiter.api.Test;
3+
4+
public class Solution3492Tests {
5+
private final Solution3492 solution3492 = new Solution3492();
6+
7+
@Test
8+
public void example1() {
9+
int n = 2;
10+
int w = 3;
11+
int maxWeight = 15;
12+
int expected = 4;
13+
Assertions.assertEquals(expected, solution3492.maxContainers(n, w, maxWeight));
14+
}
15+
16+
@Test
17+
public void example2() {
18+
int n = 3;
19+
int w = 5;
20+
int maxWeight = 20;
21+
int expected = 4;
22+
Assertions.assertEquals(expected, solution3492.maxContainers(n, w, maxWeight));
23+
}
24+
}
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 Solution3493Tests {
5+
private final Solution3493 solution3493 = new Solution3493();
6+
7+
@Test
8+
public void example1() {
9+
int[][] properties = UtUtils.stringToInts2("[[1,2],[1,1],[3,4],[4,5],[5,6],[7,7]]");
10+
int k = 1;
11+
int expected = 3;
12+
Assertions.assertEquals(expected, solution3493.numberOfComponents(properties, k));
13+
}
14+
15+
@Test
16+
public void example2() {
17+
int[][] properties = UtUtils.stringToInts2("[[1,2,3],[2,3,4],[4,3,5]]");
18+
int k = 2;
19+
int expected = 1;
20+
Assertions.assertEquals(expected, solution3493.numberOfComponents(properties, k));
21+
}
22+
23+
@Test
24+
public void example3() {
25+
int[][] properties = UtUtils.stringToInts2("[[1,1],[1,1]]");
26+
int k = 2;
27+
int expected = 2;
28+
Assertions.assertEquals(expected, solution3493.numberOfComponents(properties, k));
29+
}
30+
}
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 Solution3494Tests {
5+
private final Solution3494 solution3494 = new Solution3494();
6+
7+
@Test
8+
public void example1() {
9+
int[] skill = {1, 5, 2, 4};
10+
int[] mana = {5, 1, 4, 2};
11+
long expected = 110;
12+
Assertions.assertEquals(expected, solution3494.minTime(skill, mana));
13+
}
14+
15+
@Test
16+
public void example2() {
17+
int[] skill = {1, 1, 1};
18+
int[] mana = {1, 1, 1};
19+
long expected = 5;
20+
Assertions.assertEquals(expected, solution3494.minTime(skill, mana));
21+
}
22+
23+
@Test
24+
public void example3() {
25+
int[] skill = {1, 2, 3, 4};
26+
int[] mana = {1, 2};
27+
long expected = 21;
28+
Assertions.assertEquals(expected, solution3494.minTime(skill, mana));
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 Solution3495Tests {
5+
private final Solution3495 solution3495 = new Solution3495();
6+
7+
@Test
8+
public void example1() {
9+
int[][] queries = UtUtils.stringToInts2("[[1,2],[2,4]]");
10+
long expected = 3;
11+
Assertions.assertEquals(expected, solution3495.minOperations(queries));
12+
}
13+
14+
@Test
15+
public void example2() {
16+
int[][] queries = UtUtils.stringToInts2("[[2,6]]");
17+
long expected = 4;
18+
Assertions.assertEquals(expected, solution3495.minOperations(queries));
19+
}
20+
}

0 commit comments

Comments
(0)

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