forked from JojoYang666/Leetcode-301-600
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_324_WiggleSortII.java
More file actions
130 lines (112 loc) · 3.74 KB
/
_324_WiggleSortII.java
File metadata and controls
130 lines (112 loc) · 3.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package leetcode_1To300;
import java.util.Arrays;
/**
* 本代码来自 Cspiration,由 @Cspiration 提供
* 题目来源:http://leetcode.com
* - Cspiration 致力于在 CS 领域内帮助中国人找到工作,让更多海外国人受益
* - 现有课程:Leetcode Java 版本视频讲解(1-900题)(上)(中)(下)三部
* - 算法基础知识(上)(下)两部;题型技巧讲解(上)(下)两部
* - 节省刷题时间,效率提高2-3倍,初学者轻松一天10题,入门者轻松一天20题
* - 讲师:Edward Shi
* - 官方网站:https://cspiration.com
* - 版权所有,转发请注明出处
*/
public class _324_WiggleSortII {
/**
* 324. Wiggle Sort II
* Given an unsorted array nums, reorder it such that nums[0] < nums[1] > nums[2] < nums[3]....
Example:
(1) Given nums = [1, 5, 1, 1, 6, 4], one possible answer is [1, 4, 1, 5, 1, 6].
(2) Given nums = [1, 3, 2, 2, 3, 1], one possible answer is [2, 3, 1, 3, 1, 2].
Note:
You may assume all input has valid answer.
Follow Up:
Can you do it in O(n) time and/or in-place with O(1) extra space?
time : O(nlogn)
space : O(n)
* @param nums
*/
public void wiggleSort(int[] nums) {
Arrays.sort(nums);
int n = nums.length;
int mid = (n - 1) / 2;
int index = 0;
int[] temp = new int[n];
for (int i = 0; i <= mid; i++) {
temp[index] = nums[mid - i];
if (index + 1 < n) {
temp[index + 1] = nums[n - 1 - i];
}
index += 2;
}
System.arraycopy(temp, 0, nums, 0, n);
}
/**
Original idx: 0 1 2 3 4 5
Mapped idx: 1 3 5 0 2 4
Array: 13 6 4 5 2 5
5 6 4 13 2 5
r
i
l
median = 5
nums[] = 5
大于中位数,左 - 右,奇
小于中位数,右 - 左,偶
time : O(n)
space : O(1)
* @param nums
*/
public void wiggleSort2(int[] nums) {
int median = findKthLargest(nums, (nums.length + 1) / 2);
int n = nums.length;
int left = 0, right = n - 1;
int index = 0;
while (index <= right) {
if (nums[newIndex(index, n)] > median) {
swap(nums, newIndex(left++, n), newIndex(index++, n));
} else if (nums[newIndex(index, n)] < median) {
swap(nums, newIndex(right--, n), newIndex(index, n));
} else {
index++;
}
}
}
private int newIndex(int index, int n) {
return (1 + 2 * index) % (n | 1);
}
public int findKthLargest(int[] nums, int k) {
if (nums == null || nums.length == 0) return 0;
int left = 0;
int right = nums.length - 1;
while (true) {
int pos = partition(nums, left, right);
if (pos + 1 == k) {
return nums[pos];
} else if (pos + 1 > k) {
right = pos - 1;
} else {
left = pos + 1;
}
}
}
private int partition(int[] nums, int left, int right) {
int pivot = nums[left];
int l = left + 1;
int r = right;
while (l <= r) {
if (nums[l] < pivot && nums[r] > pivot) {
swap(nums, l++, r--);
}
if (nums[l] >= pivot) l++;
if (nums[r] <= pivot) r--;
}
swap(nums, left, r);
return r;
}
private void swap(int[] nums, int i, int j) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}