Skip to content

Commit 88135a9

Browse files
committed
✨feat: add 324
1 parent 290f708 commit 88135a9

File tree

3 files changed

+160
-0
lines changed

3 files changed

+160
-0
lines changed

Index/排序.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
| [41. 缺失的第一个正数](https://leetcode-cn.com/problems/first-missing-positive/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/first-missing-positive/solution/yan-ge-on-de-tong-pai-xu-si-lu-yi-ji-wei-wm8d/) | 困难 | 🤩🤩🤩 |
44
| [220. 存在重复元素 III](https://leetcode-cn.com/problems/contains-duplicate-iii/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/contains-duplicate-iii/solution/gong-shui-san-xie-yi-ti-shuang-jie-hua-d-dlnv/) | 中等 | 🤩🤩🤩 |
55
| [268. 丢失的数字](https://leetcode-cn.com/problems/missing-number/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/missing-number/solution/gong-shui-san-xie-yi-ti-wu-jie-pai-xu-ji-te3s/) | 简单 | 🤩🤩🤩🤩 |
6+
| [324. 摆动排序 II](https://leetcode.cn/problems/wiggle-sort-ii/) | [LeetCode 题解链接](https://leetcode.cn/problems/wiggle-sort-ii/solution/by-ac_oier-22bq/) | 中等 | 🤩🤩🤩🤩 |
67
| [406. 根据身高重建队列](https://leetcode.cn/problems/queue-reconstruction-by-height/) | [LeetCode 题解链接](https://leetcode.cn/problems/queue-reconstruction-by-height/solution/by-ac_oier-fda2/) | 中等 | 🤩🤩🤩 |
78
| [414. 第三大的数](https://leetcode-cn.com/problems/third-maximum-number/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/third-maximum-number/solution/gong-shui-san-xie-yi-ti-shuang-jie-pai-x-pmln/) | 中等 | 🤩🤩🤩🤩 |
89
| [448. 找到所有数组中消失的数字](https://leetcode-cn.com/problems/find-all-numbers-disappeared-in-an-array/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/find-all-numbers-disappeared-in-an-array/solution/li-yong-tong-pai-xu-de-si-lu-ni-huan-ke-e3t4w/) | 简单 | 🤩🤩🤩 |

Index/构造.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
| 题目 | 题解 | 难度 | 推荐指数 |
22
| ------------------------------------------------------------ | ------------------------------------------------------------ | ---- | -------- |
3+
| [324. 摆动排序 II](https://leetcode.cn/problems/wiggle-sort-ii/) | [LeetCode 题解链接](https://leetcode.cn/problems/wiggle-sort-ii/solution/by-ac_oier-22bq/) | 中等 | 🤩🤩🤩🤩 |
34
| [406. 根据身高重建队列](https://leetcode.cn/problems/queue-reconstruction-by-height/) | [LeetCode 题解链接](https://leetcode.cn/problems/queue-reconstruction-by-height/solution/by-ac_oier-fda2/) | 中等 | 🤩🤩🤩🤩 |
45
| [942. 增减字符串匹配](https://leetcode.cn/problems/di-string-match/) | [LeetCode 题解链接](https://leetcode.cn/problems/di-string-match/solution/by-ac_oier-pvjk/) | 简单 | 🤩🤩🤩🤩🤩 |
56
| [961. 在长度 2N 的数组中找出重复 N 次的元素](https://leetcode.cn/problems/n-repeated-element-in-size-2n-array/) | [LeetCode 题解链接](https://leetcode.cn/problems/n-repeated-element-in-size-2n-array/solution/by-ac_oier-bslq/) | 简单 | 🤩🤩🤩🤩 |
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[324. 摆动排序 II](https://leetcode.cn/problems/wiggle-sort-ii/solution/by-ac_oier-22bq/)** ,难度为 **中等**
4+
5+
Tag : 「构造」、「排序」、「快速选择」
6+
7+
8+
9+
给你一个整数数组 `nums`,将它重新排列成 `nums[0] < nums[1] > nums[2] < nums[3]...` 的顺序。
10+
11+
你可以假设所有输入数组都可以得到满足题目要求的结果。
12+
13+
示例 1:
14+
```
15+
输入:nums = [1,5,1,1,6,4]
16+
17+
输出:[1,6,1,5,1,4]
18+
19+
解释:[1,4,1,5,1,6] 同样是符合题目要求的结果,可以被判题程序接受。
20+
```
21+
示例 2:
22+
```
23+
输入:nums = [1,3,2,2,3,1]
24+
25+
输出:[2,3,1,3,1,2]
26+
```
27+
28+
提示:
29+
* $1 <= nums.length <= 5 \times 10^4$
30+
* $0 <= nums[i] <= 5000$
31+
* 题目数据保证,对于给定的输入 `nums`,总能产生满足题目要求的结果
32+
33+
34+
进阶:你能用 $O(n)$ 时间复杂度和 / 或原地 $O(1)$ 额外空间来实现吗?
35+
36+
---
37+
38+
### 构造(快选 + 三数排序)
39+
40+
这道题即使不考虑空间 $O(1)$ 的进阶要求,只要求做到 $O(n)$ 时间的话,在 LC 上也属于难题了。如果大家是第一次做,并且希望在有限时间(不超过 $20$ 分钟)内做出来,可以说是难上加难。
41+
42+
本质上,题目要我们实现一种构造方法,能够将 `nums` 调整为满足「摆动」要求。
43+
44+
具体的构造方法:
45+
46+
1. 找到 `nums` 的中位数,这一步可以通过「快速选择」算法来做,时间复杂度为 $O(n)$,空间复杂度为 $O(\log{n})$,假设找到的中位数为 `x`
47+
48+
2. 根据 $nums[i]$ 与 `x` 的大小关系,将 $nums[i]$ 分为三类(小于/等于/大于),划分三类的操作可以采用「三数排序」的做法,复杂度为 $O(n)$。
49+
50+
这一步做完之后,我们的数组调整为:$[a_1, a_2, a_3, ... , b_1, b_2, b_3, ... , c_1, c_2, c_3]$ ,即分成「小于 `x` / 等于 `x` / 大于 `x`」三段。
51+
52+
3. 构造:先放「奇数」下标,再放「偶数」下标,放置方向都是「从左到右」(即可下标从小到大进行放置),放置的值是则是「从大到小」。
53+
54+
到这一步之前,我们使用到的空间上界是 $O(\log{n})$,如果对空间上界没有要求的话,我们可以简单对 `nums` 进行拷贝,然后按照对应逻辑进行放置即可,但这样最终的空间复杂度为 $O(n)$(代码见 $P2$);如果不希望影响到原有的空间上界,我们需要额外通过「找规律/数学」的方式,找到原下标和目标下标的映射关系(函数 `getIdx` 中)。
55+
56+
容易证明该构造过程的正确性(即该构造过程必然能顺利进行):由于我们是按照值「从大到小」进行放置,如果构造出来的方案不合法,必然是相邻的两个值为相等(“应当递增实际递减”或者“应当递减实际递增”的情况已被「从大到小」进行放置所否决),而当相邻位置放置了相同的值,即存在某个奇数下标,以及其相邻的偶数下标都放置了相同的值,这等价于该值出现次数超过总个数的一半,这与「题目本身保证数据能够构造出摆动数组」所冲突。
57+
58+
代码:
59+
```Java
60+
class Solution {
61+
int[] nums;
62+
int n;
63+
int qselect(int l, int r, int k) {
64+
if (l == r) return nums[l];
65+
int x = nums[l + r >> 1], i = l - 1, j = r + 1;
66+
while (i < j) {
67+
do i++; while (nums[i] < x);
68+
do j--; while (nums[j] > x);
69+
if (i < j) swap(i, j);
70+
}
71+
int cnt = j - l + 1;
72+
if (k <= cnt) return qselect(l, j, k);
73+
else return qselect(j + 1, r, k - cnt);
74+
}
75+
void swap(int a, int b) {
76+
int c = nums[a];
77+
nums[a] = nums[b];
78+
nums[b] = c;
79+
}
80+
int getIdx(int x) {
81+
return (2 * x + 1) % (n | 1);
82+
}
83+
public void wiggleSort(int[] _nums) {
84+
nums = _nums;
85+
n = nums.length;
86+
int x = qselect(0, n - 1, n + 1 >> 1);
87+
int l = 0, r = n - 1, loc = 0;
88+
while (loc <= r) {
89+
if (nums[getIdx(loc)] > x) swap(getIdx(loc++), getIdx(l++));
90+
else if (nums[getIdx(loc)] < x) swap(getIdx(loc), getIdx(r--));
91+
else loc++;
92+
}
93+
}
94+
}
95+
```
96+
97+
-
98+
99+
```Java
100+
class Solution {
101+
int[] nums;
102+
int n;
103+
int qselect(int l, int r, int k) {
104+
if (l == r) return nums[l];
105+
int x = nums[l + r >> 1], i = l - 1, j = r + 1;
106+
while (i < j) {
107+
do i++; while (nums[i] < x);
108+
do j--; while (nums[j] > x);
109+
if (i < j) swap(i, j);
110+
}
111+
int cnt = j - l + 1;
112+
if (k <= cnt) return qselect(l, j, k);
113+
else return qselect(j + 1, r, k - cnt);
114+
}
115+
void swap(int a, int b) {
116+
int c = nums[a];
117+
nums[a] = nums[b];
118+
nums[b] = c;
119+
}
120+
public void wiggleSort(int[] _nums) {
121+
nums = _nums;
122+
n = nums.length;
123+
int x = qselect(0, n - 1, n + 1 >> 1);
124+
int l = 0, r = n - 1, loc = 0;
125+
while (loc <= r) {
126+
if (nums[loc] < x) swap(loc++, l++);
127+
else if (nums[loc] > x) swap(loc, r--);
128+
else loc++;
129+
}
130+
int[] clone = nums.clone();
131+
int idx = 1; loc = n - 1;
132+
while (idx < n) {
133+
nums[idx] = clone[loc--];
134+
idx += 2;
135+
}
136+
idx = 0;
137+
while (idx < n) {
138+
nums[idx] = clone[loc--];
139+
idx += 2;
140+
}
141+
}
142+
}
143+
```
144+
* 时间复杂度:快选的时间复杂度为 $O(n)$;三数排序复杂度为 $O(n)$。整体复杂度为 $O(n)$
145+
* 空间复杂度:我的习惯是不算递归带来的额外空间消耗的,但如果是题目指定 $O(1)$ 空间的话,显然是不能按照习惯来,快选的空间复杂度为 $O(\log{n})$。整体复杂度为 $O(\log{n})$
146+
147+
---
148+
149+
### 最后
150+
151+
这是我们「刷穿 LeetCode」系列文章的第 `No.324` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
152+
153+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
154+
155+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
156+
157+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
158+

0 commit comments

Comments
 (0)