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 93c7dc1

Browse files
committed
第151场双周赛T1~T4 (4)
1 parent 915eef0 commit 93c7dc1

File tree

8 files changed

+302
-0
lines changed

8 files changed

+302
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import java.util.Arrays;
2+
3+
public class Solution3467 {
4+
public int[] transformArray(int[] nums) {
5+
int n = nums.length;
6+
int even = 0;
7+
for (int v : nums) {
8+
if (v % 2 == 0) even++;
9+
}
10+
int[] ans = new int[n];
11+
Arrays.fill(ans, 1);
12+
for (int i = 0; i < even; i++) {
13+
ans[i] = 0;
14+
}
15+
return ans;
16+
}
17+
}
18+
/*
19+
3467. 将数组按照奇偶性转化
20+
https://leetcode.cn/problems/transform-array-by-parity/description/
21+
22+
第 151 场双周赛 T1。
23+
24+
给你一个整数数组 nums。请你按照以下顺序 依次 执行操作,转换 nums:
25+
1.将每个偶数替换为 0。
26+
2.将每个奇数替换为 1。
27+
3.按 非递减 顺序排序修改后的数组。
28+
执行完这些操作后,返回结果数组。
29+
提示:
30+
1 <= nums.length <= 100
31+
1 <= nums[i] <= 1000
32+
33+
遍历计数。
34+
时间复杂度 O(n)。
35+
*/
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
public class Solution3468 {
2+
public int countArrays(int[] original, int[][] bounds) {
3+
int n = original.length;
4+
int minUp = Integer.MAX_VALUE;
5+
int minDown = Integer.MAX_VALUE;
6+
for (int i = 0; i < n; i++) {
7+
int ui = bounds[i][0], vi = bounds[i][1];
8+
int up = vi - original[i]; // 上移多少
9+
int down = original[i] - ui; // 下移多少
10+
minUp = Math.min(minUp, up);
11+
minDown = Math.min(minDown, down);
12+
}
13+
return Math.max(0, minUp + minDown + 1);
14+
}
15+
}
16+
/*
17+
3468. 可行数组的数目
18+
https://leetcode.cn/problems/find-the-number-of-copy-arrays/description/
19+
20+
第 151 场双周赛 T2。
21+
22+
给你一个长度为 n 的数组 original 和一个长度为 n x 2 的二维数组 bounds,其中 bounds[i] = [ui, vi]。
23+
你需要找到长度为 n 且满足以下条件的 可能的 数组 copy 的数量:
24+
1.对于 1 <= i <= n - 1 ,都有 (copy[i] - copy[i - 1]) == (original[i] - original[i - 1]) 。
25+
2.对于 0 <= i <= n - 1 ,都有 ui <= copy[i] <= vi 。
26+
返回满足这些条件的数组数目。
27+
提示:
28+
2 <= n == original.length <= 10^5
29+
1 <= original[i] <= 10^9
30+
bounds.length == n
31+
bounds[i].length == 2
32+
1 <= bounds[i][0] <= bounds[i][1] <= 10^9
33+
34+
移项得 copy[i]=copy[0]+original[i]−original[0]
35+
求这 n 个 copy[0] 的交集。
36+
时间复杂度 O(n)。
37+
*/
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 Solution3469 {
4+
private int n;
5+
private int[] nums;
6+
private int[][] memo;
7+
8+
public int minCost(int[] nums) {
9+
n = nums.length;
10+
this.nums = nums;
11+
memo = new int[n][n];
12+
for (int i = 0; i < n; i++) {
13+
Arrays.fill(memo[i], -1);
14+
}
15+
return dfs(1, 0);
16+
}
17+
18+
private int dfs(int i, int j) {
19+
if (i == n) return nums[j];
20+
if (i == n - 1) return Math.max(nums[j], nums[i]);
21+
if (memo[i][j] != -1) return memo[i][j];
22+
int a = nums[j], b = nums[i], c = nums[i + 1];
23+
// 移除 a,b
24+
int res = dfs(i + 2, i + 1) + Math.max(a, b);
25+
// 移除 a,c
26+
res = Math.min(res, dfs(i + 2, i) + Math.max(a, c));
27+
// 移除 b,c
28+
res = Math.min(res, dfs(i + 2, j) + Math.max(b, c));
29+
return memo[i][j] = res;
30+
}
31+
}
32+
/*
33+
3469. 移除所有数组元素的最小代价
34+
https://leetcode.cn/problems/find-minimum-cost-to-remove-array-elements/description/
35+
36+
第 151 场双周赛 T3。
37+
38+
给你一个整数数组 nums。你的任务是在每一步中执行以下操作之一,直到 nums 为空,从而移除 所有元素 :
39+
- 从 nums 的前三个元素中选择任意两个元素并移除它们。此操作的成本为移除的两个元素中的 最大值 。
40+
- 如果 nums 中剩下的元素少于三个,则一次性移除所有剩余元素。此操作的成本为剩余元素中的 最大值 。
41+
返回移除所有元素所需的最小成本。
42+
提示:
43+
1 <= nums.length <= 1000
44+
1 <= nums[i] <= 10^6
45+
46+
记忆化搜索,边界挺难写的。
47+
时间复杂度 O(n^2)。
48+
rating 2115 (clist.by)
49+
*/
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import java.util.ArrayList;
2+
import java.util.Arrays;
3+
import java.util.List;
4+
5+
public class Solution3470 {
6+
// 预处理交替排列的方案数
7+
private static final List<Long> F = new ArrayList<>();
8+
9+
static {
10+
F.add(1L);
11+
for (int i = 1; F.getLast() < 1e15; i++) {
12+
F.add(F.getLast() * i);
13+
F.add(F.getLast() * i);
14+
}
15+
}
16+
17+
public int[] permute(int n, long k) {
18+
// k 改成从 0 开始,方便计算
19+
k--;
20+
if (n < F.size() && k >= F.get(n) * (2 - n % 2)) { // n 是偶数的时候,方案数乘以 2
21+
return new int[0];
22+
}
23+
24+
// cand 表示剩余未填入 ans 的数字
25+
// cand[0] 保存偶数,cand[1] 保存奇数
26+
List<Integer>[] cand = new ArrayList[2];
27+
Arrays.setAll(cand, e -> new ArrayList<>());
28+
for (int i = 1; i <= n; i++) {
29+
cand[i % 2].add(i);
30+
}
31+
32+
int[] ans = new int[n];
33+
int parity = 1; // 当前要填入 ans[i] 的数的奇偶性
34+
for (int i = 0; i < n; i++) {
35+
int j = 0;
36+
if (n - 1 - i < F.size()) {
37+
// 比如示例 1,按照第一个数分组,每一组的大小都是 size=2
38+
// 知道 k 和 size 就知道我们要去哪一组
39+
long size = F.get(n - 1 - i);
40+
j = (int) (k / size); // 去第 j 组
41+
k %= size;
42+
// n 是偶数的情况,第一个数既可以填奇数又可以填偶数,要特殊处理
43+
if (n % 2 == 0 && i == 0) {
44+
parity = 1 - j % 2;
45+
j /= 2;
46+
}
47+
} // else j=0,在 n 很大的情况下,只能按照 1,2,3,... 的顺序填
48+
ans[i] = cand[parity].remove(j);
49+
parity ^= 1; // 下一个数的奇偶性
50+
}
51+
return ans;
52+
}
53+
}
54+
/*
55+
3470. 全排列 IV
56+
https://leetcode.cn/problems/permutations-iv/description/
57+
58+
第 151 场双周赛 T4。
59+
60+
给你两个整数 n 和 k,一个 交替排列 是前 n 个正整数的排列,且任意相邻 两个 元素不都为奇数或都为偶数。
61+
返回第 k 个 交替排列 ,并按 字典序 排序。如果有效的 交替排列 少于 k 个,则返回一个空列表。
62+
提示:
63+
1 <= n <= 100
64+
1 <= k <= 10^15
65+
66+
从左往右构造。
67+
时间复杂度 O(n^2)。
68+
相似题目: 60. 排列序列
69+
https://leetcode.cn/problems/permutation-sequence/
70+
rating 2496 (clist.by)
71+
*/
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 Solution3467Tests {
5+
private final Solution3467 solution3467 = new Solution3467();
6+
7+
@Test
8+
public void example1() {
9+
int[] nums = {4, 3, 2, 1};
10+
int[] expected = {0, 0, 1, 1};
11+
Assertions.assertArrayEquals(expected, solution3467.transformArray(nums));
12+
}
13+
14+
@Test
15+
public void example2() {
16+
int[] nums = {1, 5, 1, 4, 2};
17+
int[] expected = {0, 0, 1, 1, 1};
18+
Assertions.assertArrayEquals(expected, solution3467.transformArray(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 Solution3468Tests {
5+
private final Solution3468 solution3468 = new Solution3468();
6+
7+
@Test
8+
public void example1() {
9+
int[] original = {1, 2, 3, 4};
10+
int[][] bounds = UtUtils.stringToInts2("[[1,2],[2,3],[3,4],[4,5]]");
11+
int expected = 2;
12+
Assertions.assertEquals(expected, solution3468.countArrays(original, bounds));
13+
}
14+
15+
@Test
16+
public void example2() {
17+
int[] original = {1, 2, 3, 4};
18+
int[][] bounds = UtUtils.stringToInts2("[[1,10],[2,9],[3,8],[4,7]]");
19+
int expected = 4;
20+
Assertions.assertEquals(expected, solution3468.countArrays(original, bounds));
21+
}
22+
23+
@Test
24+
public void example3() {
25+
int[] original = {1, 2, 1, 2};
26+
int[][] bounds = UtUtils.stringToInts2("[[1,1],[2,3],[3,3],[2,3]]");
27+
int expected = 0;
28+
Assertions.assertEquals(expected, solution3468.countArrays(original, bounds));
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 Solution3469Tests {
5+
private final Solution3469 solution3469 = new Solution3469();
6+
7+
@Test
8+
public void example1() {
9+
int[] nums = {6, 2, 8, 4};
10+
int expected = 12;
11+
Assertions.assertEquals(expected, solution3469.minCost(nums));
12+
}
13+
14+
@Test
15+
public void example2() {
16+
int[] nums = {2, 1, 3, 3};
17+
int expected = 5;
18+
Assertions.assertEquals(expected, solution3469.minCost(nums));
19+
}
20+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import org.junit.jupiter.api.Assertions;
2+
import org.junit.jupiter.api.Test;
3+
4+
public class Solution3470Tests {
5+
private final Solution3470 solution3470 = new Solution3470();
6+
7+
@Test
8+
public void example1() {
9+
int n = 4;
10+
long k = 6;
11+
int[] expected = {3, 4, 1, 2};
12+
Assertions.assertArrayEquals(expected, solution3470.permute(n, k));
13+
}
14+
15+
@Test
16+
public void example2() {
17+
int n = 3;
18+
long k = 2;
19+
int[] expected = {3, 2, 1};
20+
Assertions.assertArrayEquals(expected, solution3470.permute(n, k));
21+
}
22+
23+
@Test
24+
public void example3() {
25+
int n = 2;
26+
long k = 3;
27+
int[] expected = {};
28+
Assertions.assertArrayEquals(expected, solution3470.permute(n, k));
29+
}
30+
31+
// 补充用例
32+
@Test
33+
public void example4() {
34+
// https://leetcode.cn/problems/permutations-iv/submissions/608196751/
35+
int n = 41;
36+
long k = 872502217664402L;
37+
int[] expected = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 32, 21, 24, 33, 38, 31, 28, 37, 26, 27, 34, 23, 22, 29, 30, 25, 20, 35, 36, 41, 40, 39};
38+
Assertions.assertArrayEquals(expected, solution3470.permute(n, k));
39+
}
40+
}

0 commit comments

Comments
(0)

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