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 240159d

Browse files
committed
3555-3565-3571-3581-3595-3596-3610-3616-3631-3641 (10)
1 parent 08f1ff1 commit 240159d

20 files changed

+691
-0
lines changed
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
import java.util.*;
2+
3+
public class SolutionP3555 {
4+
static class V1 {
5+
public int[] minSubarraySort(int[] nums, int k) {
6+
int n = nums.length;
7+
int[] ans = new int[n - k + 1];
8+
for (int i = 0; i < n - k + 1; i++) {
9+
ans[i] = findUnsortedSubarray(nums, i, i + k - 1);
10+
}
11+
return ans;
12+
}
13+
14+
// solution581
15+
private int findUnsortedSubarray(int[] nums, int start, int end) {
16+
int n = end - start + 1;
17+
int[] sorted = new int[n];
18+
System.arraycopy(nums, start, sorted, 0, n);
19+
Arrays.sort(sorted);
20+
int l = 0, r = n - 1;
21+
while (l <= r && nums[l + start] == sorted[l]) l++;
22+
while (l <= r && nums[r + start] == sorted[r]) r--;
23+
return r - l + 1;
24+
}
25+
}
26+
27+
static class V2 {
28+
record Pair(int val, int idx) {
29+
}
30+
31+
public int[] minSubarraySort(int[] nums, int k) {
32+
int n = nums.length;
33+
if (k == 1) return new int[n];
34+
35+
int preEnd = 1;
36+
Deque<Pair> unOrderMinStack = new ArrayDeque<>();
37+
Deque<Pair> unOrderMaxStack = new ArrayDeque<>();
38+
int postStart = 1;
39+
List<Integer> result = new ArrayList<>();
40+
41+
for (int i = 1; i < n; i++) {
42+
if (i - k >= 0) {
43+
// 移除最左边的元素 (i-k)
44+
if (!unOrderMaxStack.isEmpty() && i - k == unOrderMaxStack.getFirst().idx) {
45+
unOrderMaxStack.removeFirst();
46+
if (!unOrderMaxStack.isEmpty()) {
47+
while (postStart - 1 >= 0 && nums[postStart - 1] >= unOrderMaxStack.getFirst().val) {
48+
postStart--;
49+
}
50+
}
51+
}
52+
if (!unOrderMinStack.isEmpty() && i - k + 1 == unOrderMinStack.getFirst().idx) {
53+
unOrderMinStack.removeFirst();
54+
preEnd = i - k + 2;
55+
if (unOrderMinStack.isEmpty()) {
56+
preEnd = i;
57+
postStart = i;
58+
} else {
59+
while (preEnd < i && nums[preEnd] <= unOrderMinStack.getFirst().val) {
60+
preEnd++;
61+
}
62+
}
63+
} else {
64+
preEnd = Math.max(preEnd, i - k + 1);
65+
}
66+
}
67+
if (nums[i] < nums[i - 1]) {
68+
// 发现逆序对
69+
postStart = i + 1;
70+
while (!unOrderMinStack.isEmpty() && unOrderMinStack.getLast().val > nums[i]) {
71+
unOrderMinStack.removeLast();
72+
}
73+
unOrderMinStack.addLast(new Pair(nums[i], i));
74+
while (!unOrderMaxStack.isEmpty() && unOrderMaxStack.getLast().val < nums[i - 1]) {
75+
unOrderMaxStack.removeLast();
76+
}
77+
unOrderMaxStack.addLast(new Pair(nums[i - 1], i - 1));
78+
int limit = Math.max(0, i + 1 - k);
79+
while (preEnd > limit && nums[preEnd - 1] > unOrderMinStack.getFirst().val) {
80+
preEnd--;
81+
}
82+
} else if (unOrderMaxStack.isEmpty()) {
83+
preEnd = i + 1;
84+
postStart = i + 1;
85+
} else if (nums[i] < unOrderMaxStack.getFirst().val) {
86+
postStart = i + 1;
87+
}
88+
if (i >= k - 1) {
89+
result.add(k - (preEnd - (i - k + 1)) - (i + 1 - postStart));
90+
}
91+
}
92+
return result.stream().mapToInt(Integer::intValue).toArray();
93+
}
94+
}
95+
}
96+
/*
97+
3555ドル. 排序每个滑动窗口中最小的子数组
98+
https://leetcode.cn/problems/smallest-subarray-to-sort-in-every-sliding-window/description/
99+
100+
给定一个整数数组 nums 和一个整数 k。
101+
对于每个长度为 k 的连续 子数组,确定必须排序的连续段的最小长度,以便整个窗口成为 非递减 的;如果窗口已经排序,则其所需长度为零。
102+
返回一个长度为 n − k + 1 的数组,其中每个元素对应其窗口的答案。
103+
提示:
104+
1 <= nums.length <= 1000
105+
1 <= k <= nums.length
106+
1 <= nums[i] <= 10^6
107+
108+
O(n)单调队列解法 https://leetcode.cn/problems/smallest-subarray-to-sort-in-every-sliding-window/
109+
相似题目: 581. 最短无序连续子数组
110+
https://leetcode.cn/problems/shortest-unsorted-continuous-subarray/description/
111+
*/
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.List;
3+
4+
public class SolutionP3565 {
5+
private static final int[][] DIRECTIONS = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
6+
private int[][] grid;
7+
private int m, n;
8+
private boolean[][] vis;
9+
private List<List<Integer>> ans;
10+
11+
public List<List<Integer>> findPath(int[][] grid, int k) {
12+
this.grid = grid;
13+
m = grid.length;
14+
n = grid[0].length;
15+
vis = new boolean[m][n];
16+
ans = new ArrayList<>();
17+
for (int i = 0; i < m; i++) {
18+
for (int j = 0; j < n; j++) {
19+
if (dfs(i, j, 0, 1)) {
20+
return ans;
21+
}
22+
}
23+
}
24+
return new ArrayList<>();
25+
}
26+
27+
private boolean dfs(int row, int col, int visCnt, int target) {
28+
if (row < 0 || row >= m || col < 0 || col >= n || vis[row][col]
29+
|| (grid[row][col] != 0 && grid[row][col] != target)) {
30+
return false;
31+
}
32+
if (grid[row][col] == target) {
33+
target++;
34+
}
35+
vis[row][col] = true;
36+
ans.add(List.of(row, col));
37+
visCnt++;
38+
if (visCnt == m * n) {
39+
return true;
40+
}
41+
for (int[] d : DIRECTIONS) {
42+
if (dfs(row + d[0], col + d[1], visCnt, target)) {
43+
return true;
44+
}
45+
}
46+
vis[row][col] = false;
47+
ans.removeLast();
48+
return false;
49+
}
50+
}
51+
/*
52+
3565ドル. 顺序网格路径覆盖
53+
https://leetcode.cn/problems/sequential-grid-path-cover/description/
54+
55+
给定一个 m x n 大小的 2 维数组 grid,和一个整数 k。grid 中有 k 个单元格包含从 1 到 k 的值,每个值恰好出现一次,其余单元格的值为 0。
56+
你可以从任何单元格开始,并且从一个单元格移动到相邻的单元格(上,下,左,右)。你必须找到一条 grid 中的路径,满足:
57+
- 访问 grid 中的每个单元格 恰好一次。
58+
- 按顺序 访问值为 1 到 k 的单元格。
59+
返回一个大小为 (m * n) 的二维数组 result,其中 result[i] = [xi, yi] 表示路径中访问的第 i 个单元格。如果存在多条这样的路径,你可以返回 任何 一条。
60+
如果不存在这样的路径,返回一个 空 数组。
61+
提示:
62+
1 <= m == grid.length <= 5
63+
1 <= n == grid[i].length <= 5
64+
1 <= k <= m * n
65+
0 <= grid[i][j] <= k
66+
grid 包含 1 到 k 的所有整数 恰好 一次。
67+
68+
回溯。
69+
https://leetcode.cn/problems/sequential-grid-path-cover/solutions/3690857/3565-shun-xu-wang-ge-lu-jing-fu-gai-by-s-c50l/
70+
时间复杂度: O(mn * 3^mn)。
71+
*/
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
public class SolutionP3571 {
2+
public String shortestSuperstring(String s1, String s2) {
3+
if (s1.length() < s2.length()) return shortestSuperstring(s2, s1);
4+
if (s1.contains(s2)) return s1;
5+
int m = s2.length();
6+
for (int L = m - 1; L >= 1; L--) { // 由大到小枚举重叠长度
7+
if (s1.endsWith(s2.substring(0, L))) {
8+
return s1 + s2.substring(L);
9+
}
10+
if (s2.endsWith(s1.substring(0, L))) {
11+
return s2 + s1.substring(L);
12+
}
13+
}
14+
return s1 + s2;
15+
}
16+
}
17+
/*
18+
3571ドル. 最短超级串 II
19+
https://leetcode.cn/problems/find-the-shortest-superstring-ii/description/
20+
21+
给定 两 个字符串,s1 和 s2。返回同时包含 s1 和 s2 作为子串的 最短 字符串。如果有多个合法的答案,返回任意一个。
22+
子串 是字符串中连续的字符序列。
23+
提示:
24+
1 <= s1.length <= 100
25+
1 <= s2.length <= 100
26+
s1 和 s2 只包含小写英文字母。
27+
28+
由大到小枚举重叠长度。
29+
时间复杂度 O(mn)。
30+
*/
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
public class SolutionP3581 {
2+
private static final String[] MP = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
3+
4+
public int countOddLetters(int n) {
5+
int xor = 0;
6+
while (n > 0) {
7+
int digit = n % 10;
8+
for (char c : MP[digit].toCharArray()) xor ^= 1 << (c - 'a');
9+
n /= 10;
10+
}
11+
return Integer.bitCount(xor);
12+
}
13+
}
14+
/*
15+
3581ドル. 计算数字中的奇数字母数量
16+
https://leetcode.cn/problems/count-odd-letters-from-number/description/
17+
18+
你被给定一个整数 n,执行以下步骤:
19+
- 将 n 的每个数位转换为它的小写英文单词(例如 4 → "four", 1 → "one")。
20+
- 将那些单词按照 原始数字顺序 连接 起来形成一个字符串 s。
21+
返回字符串 s 中出现 奇数 次的 不同 字符的数量。
22+
提示:
23+
1 <= n <= 10^9
24+
25+
模拟。
26+
时间复杂度: O(logn)
27+
*/
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import java.util.HashMap;
2+
import java.util.Map;
3+
4+
public class SolutionP3595 {
5+
public int[] onceTwice(int[] nums) {
6+
Map<Integer, Integer> cnt = new HashMap<>();
7+
for (int v : nums) cnt.merge(v, 1, Integer::sum);
8+
int[] ans = new int[2];
9+
for (Map.Entry<Integer, Integer> entry : cnt.entrySet()) {
10+
Integer c = entry.getValue();
11+
if (c == 1) ans[0] = entry.getKey();
12+
else if (c == 2) ans[1] = entry.getKey();
13+
}
14+
return ans;
15+
}
16+
}
17+
/*
18+
3595ドル. 一次或两次
19+
https://leetcode.cn/problems/once-twice/description/
20+
21+
给定一个整数数组 nums。在这个数组中:
22+
- 有一个元素出现了 恰好 1 次。
23+
- 有一个元素出现了 恰好 2 次。
24+
- 其它所有元素都出现了 恰好 3 次。
25+
返回一个长度为 2 的整数数组,其中第一个元素是只出现 1 次 的那个元素,第二个元素是只出现 2 次 的那个元素。
26+
你的解决方案必须在 O(n) 时间 与 O(1) 空间中运行。
27+
提示:
28+
3 <= nums.length <= 10^5
29+
-2^31 <= nums[i] <= 2^31 - 1
30+
nums.length 是 3 的倍数。
31+
恰好有一个元素出现 1 次,一个元素出现 2 次,其余所有元素都出现了 3 次。
32+
33+
哈希表统计频次。
34+
时间复杂度 O(n)。
35+
相似题目: 137. 只出现一次的数字 II
36+
https://leetcode.cn/problems/single-number-ii/
37+
*/
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
public class SolutionP3596 {
2+
public int minCost(int m, int n) {
3+
if (m == 1 && n == 1) return 1;
4+
if (m == 1 && n == 2 || m == 2 && n == 1) return 3;
5+
return -1;
6+
}
7+
}
8+
/*
9+
3596ドル. 最小花费路径交替方向 I
10+
https://leetcode.cn/problems/minimum-cost-path-with-alternating-directions-i/description/
11+
12+
给定两个整数 m 和 n 分别表示一个网格的行数和列数。
13+
进入单元格 (i, j) 的花费定义为 (i + 1) * (j + 1)。
14+
路径始终从第 1 步进入单元格 (0, 0) 并支付入场花费开始。
15+
在每一步,你移动到 相邻 的单元格,遵循交替的模式:
16+
- 在 奇数次 移动,你必须向 右方 或 下方 移动。
17+
- 在 偶数次 移动,你必须向 左方 或 上方 移动。
18+
返回到达 (m - 1, n - 1) 的最小总花费。如果不可能到达,返回 -1。
19+
提示:
20+
1 <= m, n <= 10^6
21+
22+
脑筋急转弯。分类讨论即可。
23+
时间复杂度 O(1)。
24+
*/
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import org.junit.jupiter.api.Assertions;
2+
import org.junit.jupiter.api.Test;
3+
4+
public class SolutionP3555Tests {
5+
private final SolutionP3555.V1 solutionP3555_v1 = new SolutionP3555.V1();
6+
private final SolutionP3555.V2 solutionP3555_v2 = new SolutionP3555.V2();
7+
8+
@Test
9+
public void example1() {
10+
int[] nums = {1, 3, 2, 4, 5};
11+
int k = 3;
12+
int[] expected = {2, 2, 0};
13+
Assertions.assertArrayEquals(expected, solutionP3555_v1.minSubarraySort(nums, k));
14+
Assertions.assertArrayEquals(expected, solutionP3555_v2.minSubarraySort(nums, k));
15+
}
16+
17+
@Test
18+
public void example2() {
19+
int[] nums = {5, 4, 3, 2, 1};
20+
int k = 4;
21+
int[] expected = {4, 4};
22+
Assertions.assertArrayEquals(expected, solutionP3555_v1.minSubarraySort(nums, k));
23+
Assertions.assertArrayEquals(expected, solutionP3555_v2.minSubarraySort(nums, k));
24+
}
25+
}
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+
import java.util.List;
5+
6+
public class SolutionP3565Tests {
7+
private final SolutionP3565 solutionP3565 = new SolutionP3565();
8+
9+
@Test
10+
public void example1() {
11+
int[][] grid = UtUtils.stringToInts2("[[0,0,0],[0,1,2]]");
12+
int k = 2;
13+
List<List<Integer>> expected = UtUtils.stringToIntegerList2("[[0,0],[1,0],[1,1],[1,2],[0,2],[0,1]]");
14+
Assertions.assertEquals(expected, solutionP3565.findPath(grid, k));
15+
}
16+
17+
@Test
18+
public void example2() {
19+
int[][] grid = UtUtils.stringToInts2("[[1,0,4],[3,0,2]]");
20+
int k = 4;
21+
List<List<Integer>> expected = UtUtils.stringToIntegerList2("[]");
22+
Assertions.assertEquals(expected, solutionP3565.findPath(grid, k));
23+
}
24+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import org.junit.jupiter.api.Assertions;
2+
import org.junit.jupiter.api.Test;
3+
4+
public class SolutionP3571Tests {
5+
private final SolutionP3571 solutionP3571 = new SolutionP3571();
6+
7+
@Test
8+
public void example1() {
9+
String s1 = "aba";
10+
String s2 = "bab";
11+
String expected = "abab";
12+
Assertions.assertEquals(expected, solutionP3571.shortestSuperstring(s1, s2));
13+
}
14+
15+
@Test
16+
public void example2() {
17+
String s1 = "aa";
18+
String s2 = "aaa";
19+
String expected = "aaa";
20+
Assertions.assertEquals(expected, solutionP3571.shortestSuperstring(s1, s2));
21+
}
22+
}

0 commit comments

Comments
(0)

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