Skip to content

Commit 5f4a2f5

Browse files
committed
update 324
1 parent 879793b commit 5f4a2f5

File tree

4 files changed

+187
-15
lines changed

4 files changed

+187
-15
lines changed

README.md

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
4545
| [] | [9c-dp.md](./list/9c-dp.md) | 42/45 | 3 vips |
4646
| [] | [geekbang.md](./list/geekbang.md) | 55/55 | - |
4747
| [] | [leetcode101.md](./list/leetcode101.md) | 183/184 | 1 vip |
48-
| [🔲] | [9c-advanced.md](./list/9c-advanced.md) | 69/92 | 17 vips |
48+
| [🔲] | [9c-advanced.md](./list/9c-advanced.md) | 70/92 | 17 vips |
4949
| [🔲] | [leetcode-contest.md](./list/leetcode-contest.md) | 10/40 | - |
5050
| [🔲] | [leetcode-google.md](./list/leetcode-google.md) | 0/7 | - |
5151

@@ -188,7 +188,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
188188

189189
## Greedy
190190

191-
| Link | Problem(22) | Solution | Tag | Time | Space | Ref |
191+
| Link | Problem(23) | Solution | Tag | Time | Space | Ref |
192192
| ----- | ----- | ----- | ----- | ----- | ----- | ----- |
193193
| [Leetcode-870](https://leetcode.com/problems/advantage-shuffle/) | Advantage Shuffle | [c++](./leetcode/870.advantage-shuffle.cpp), [python3](./leetcode/870.advantage-shuffle.py) | Greedy | O\(NlogN\) | O\(N\) | - |
194194
| [Leetcode-455](https://leetcode.com/problems/assign-cookies/) | Assign Cookies | [c++](./leetcode/455.assign-cookies.cpp), [python3](./leetcode/455.assign-cookies.py) | Greedy | O\(NlogN\) | O\(1\) | - |
@@ -208,6 +208,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
208208
| [Leetcode-763](https://leetcode.com/problems/partition-labels/) | Partition Labels | [c++](./leetcode/763.partition-labels.cpp), [python3](./leetcode/763.partition-labels.py) | Greedy | O\(N\) | O\(N\) | - |
209209
| [Leetcode-3282](https://leetcode.com/problems/reach-end-of-array-with-max-score/) | Reach End Of Array With Max Score | [c++](./leetcode/3282.reach-end-of-array-with-max-score.cpp), [python3](./leetcode/3282.reach-end-of-array-with-max-score.py) | Greedy | O\(N\) | O\(1\) | - |
210210
| [Leetcode-680](https://leetcode.com/problems/valid-palindrome-ii/) | Valid Palindrome II | [c++](./leetcode/680.valid-palindrome-ii.cpp), [python3](./leetcode/680.valid-palindrome-ii.py) | Greedy | O\(N\) | O\(1\) | - |
211+
| [Leetcode-324](https://leetcode.com/problems/wiggle-sort-ii/) | Wiggle Sort II | [c++](./leetcode/324.wiggle-sort-ii.cpp), [python3](./leetcode/324.wiggle-sort-ii.py) | Greedy | \- | \- | - |
211212
| [Leetcode-376](https://leetcode.com/problems/wiggle-subsequence/) | Wiggle Subsequence | [c++](./leetcode/376.wiggle-subsequence.cpp), [python3](./leetcode/376.wiggle-subsequence.py) | Greedy | O\(N\) | O\(N\) | - |
212213
| [Leetcode-44](https://leetcode.com/problems/wildcard-matching/) | Wildcard Matching | [c++](./leetcode/44.wildcard-matching.cpp), [python3](./leetcode/44.wildcard-matching.py) | Greedy | O\(NM\) | O\(NM\) | - |
213214
| [Lintcode-3672](https://www.lintcode.com/problem/connecting-cities-with-minimum-cost/) | Connecting Cities With Minimum Cost | [c++](./lintcode/3672.connecting-cities-with-minimum-cost.cpp), [python3](./lintcode/3672.connecting-cities-with-minimum-cost.py) | Greedy | O\(ElogE\) | O\(E\+V\) | Leetcode-1135 |
@@ -528,7 +529,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
528529

529530
## Sorting
530531

531-
| Link | Problem(29) | Solution | Tag | Time | Space | Ref |
532+
| Link | Problem(30) | Solution | Tag | Time | Space | Ref |
532533
| ----- | ----- | ----- | ----- | ----- | ----- | ----- |
533534
| [Leetcode-15](https://leetcode.com/problems/3sum/) | 3Sum | [c++](./leetcode/15.3sum.cpp), [python3](./leetcode/15.3sum.py) | Sorting | O\(N^2\) | O\(1\) | - |
534535
| [Leetcode-18](https://leetcode.com/problems/4sum/) | 4Sum | [c++](./leetcode/18.4sum.cpp), [python3](./leetcode/18.4sum.py) | Sorting | O\(N^3\) | O\(1\) | - |
@@ -556,6 +557,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
556557
| [Leetcode-75](https://leetcode.com/problems/sort-colors/) | Sort Colors | [c++](./leetcode/75.sort-colors.cpp), [python3](./leetcode/75.sort-colors.py) | Sorting | O\(N\) | O\(1\) | - |
557558
| [Leetcode-148](https://leetcode.com/problems/sort-list/) | Sort List | [c++](./leetcode/148.sort-list.cpp), [python3](./leetcode/148.sort-list.py) | Sorting | O\(NlogN\) | O\(logN\) | - |
558559
| [Leetcode-242](https://leetcode.com/problems/valid-anagram/) | Valid Anagram | [c++](./leetcode/242.valid-anagram.cpp), [python3](./leetcode/242.valid-anagram.py) | Sorting | O\(N\) | O\(1\) | - |
560+
| [Leetcode-324](https://leetcode.com/problems/wiggle-sort-ii/) | Wiggle Sort II | [c++](./leetcode/324.wiggle-sort-ii.cpp), [python3](./leetcode/324.wiggle-sort-ii.py) | Sorting | \- | \- | - |
559561
| [Lintcode-919](https://www.lintcode.com/problem/meeting-rooms-ii/) | Meeting Rooms II | [c++](./lintcode/919.meeting-rooms-ii.cpp), [python3](./lintcode/919.meeting-rooms-ii.py) | Sorting | O\(NlogN\) | O\(N\) | Leetcode-253 |
560562
| [Lintcode-39](https://www.lintcode.com/problem/recover-rotated-sorted-array/) | Recover Rotated Sorted Array | [c++](./lintcode/39.recover-rotated-sorted-array.cpp), [python3](./lintcode/39.recover-rotated-sorted-array.py) | Sorting | O\(N\) | O\(1\) | - |
561563
| [Lintcode-508](https://www.lintcode.com/problem/wiggle-sort/) | Wiggle Sort | [c++](./lintcode/508.wiggle-sort.cpp), [python3](./lintcode/508.wiggle-sort.py) | Sorting | O\(N\) | O\(1\) | Leetcode-280 |
@@ -572,14 +574,15 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
572574

573575
## Quickselect
574576

575-
| Link | Problem(2) | Solution | Tag | Time | Space | Ref |
577+
| Link | Problem(3) | Solution | Tag | Time | Space | Ref |
576578
| ----- | ----- | ----- | ----- | ----- | ----- | ----- |
577579
| [Leetcode-215](https://leetcode.com/problems/kth-largest-element-in-an-array/) | Kth Largest Element In An Array | [c++](./leetcode/215.kth-largest-element-in-an-array.cpp), [python3](./leetcode/215.kth-largest-element-in-an-array.py) | Quickselect | O\(N\) ~ O\(N^2\) | O\(1\) | - |
578580
| [Leetcode-347](https://leetcode.com/problems/top-k-frequent-elements/) | Top K Frequent Elements | [c++](./leetcode/347.top-k-frequent-elements.cpp), [python3](./leetcode/347.top-k-frequent-elements.py) | Quickselect | O\(N\), O\(C\*logK\) | O\(N\) | - |
581+
| [Leetcode-324](https://leetcode.com/problems/wiggle-sort-ii/) | Wiggle Sort II | [c++](./leetcode/324.wiggle-sort-ii.cpp), [python3](./leetcode/324.wiggle-sort-ii.py) | Quickselect | \- | \- | - |
579582

580583
## Divide and Conquer
581584

582-
| Link | Problem(18) | Solution | Tag | Time | Space | Ref |
585+
| Link | Problem(19) | Solution | Tag | Time | Space | Ref |
583586
| ----- | ----- | ----- | ----- | ----- | ----- | ----- |
584587
| [Leetcode-932](https://leetcode.com/problems/beautiful-array/) | Beautiful Array | [c++](./leetcode/932.beautiful-array.cpp), [python3](./leetcode/932.beautiful-array.py) | Divide and Conquer | O\(N\) | O\(N\) | - |
585588
| [Leetcode-106](https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/) | Construct Binary Tree From Inorder And Postorder Traversal | [c++](./leetcode/106.construct-binary-tree-from-inorder-and-postorder-traversal.cpp), [python3](./leetcode/106.construct-binary-tree-from-inorder-and-postorder-traversal.py) | Divide and Conquer | O\(N\) | O\(N\) | - |
@@ -597,6 +600,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
597600
| [Leetcode-148](https://leetcode.com/problems/sort-list/) | Sort List | [c++](./leetcode/148.sort-list.cpp), [python3](./leetcode/148.sort-list.py) | Divide and Conquer | O\(NlogN\) | O\(logN\) | - |
598601
| [Leetcode-218](https://leetcode.com/problems/the-skyline-problem/) | The Skyline Problem | [c++](./leetcode/218.the-skyline-problem.cpp), [python3](./leetcode/218.the-skyline-problem.py) | Divide and Conquer | O\(NlogN\) | O\(N\) | - |
599602
| [Leetcode-347](https://leetcode.com/problems/top-k-frequent-elements/) | Top K Frequent Elements | [c++](./leetcode/347.top-k-frequent-elements.cpp), [python3](./leetcode/347.top-k-frequent-elements.py) | Divide and Conquer | O\(N\), O\(C\*logK\) | O\(N\) | - |
603+
| [Leetcode-324](https://leetcode.com/problems/wiggle-sort-ii/) | Wiggle Sort II | [c++](./leetcode/324.wiggle-sort-ii.cpp), [python3](./leetcode/324.wiggle-sort-ii.py) | Divide and Conquer | \- | \- | - |
600604
| [Lintcode-1534](https://www.lintcode.com/problem/convert-binary-search-tree-to-sorted-doubly-linked-list/) | Convert Binary Search Tree To Sorted Doubly Linked List | [c++](./lintcode/1534.convert-binary-search-tree-to-sorted-doubly-linked-list.cpp), [python3](./lintcode/1534.convert-binary-search-tree-to-sorted-doubly-linked-list.py) | Divide and Conquer | O\(N\) | O\(H\) | - |
601605
| [Lintcode-11](https://www.lintcode.com/problem/search-range-in-binary-search-tree/) | Search Range In Binary Search Tree | [c++](./lintcode/11.search-range-in-binary-search-tree.cpp), [python3](./lintcode/11.search-range-in-binary-search-tree.py) | Divide and Conquer | O\(N\) | O\(H\) | - |
602606

@@ -817,7 +821,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
817821

818822
## Other
819823

820-
| Link | Problem(136) | Solution | Tag | Time | Space | Ref |
824+
| Link | Problem(135) | Solution | Tag | Time | Space | Ref |
821825
| ----- | ----- | ----- | ----- | ----- | ----- | ----- |
822826
| [Leetcode-16](https://leetcode.com/problems/3sum-closest/) | 3Sum Closest | [c++](./leetcode/16.3sum-closest.cpp), [python3](./leetcode/16.3sum-closest.py) | Other | \- | \- | - |
823827
| [Leetcode-454](https://leetcode.com/problems/4sum-ii/) | 4Sum II | [c++](./leetcode/454.4sum-ii.cpp) | Other | \- | \- | - |
@@ -940,7 +944,6 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
940944
| [Leetcode-125](https://leetcode.com/problems/valid-palindrome/) | Valid Palindrome | [c++](./leetcode/125.valid-palindrome.cpp), [python3](./leetcode/125.valid-palindrome.py) | Other | \- | \- | - |
941945
| [Leetcode-408](https://leetcode.com/problems/valid-word-abbreviation/) | Valid Word Abbreviation | [c++](./leetcode/408.valid-word-abbreviation.cpp), [python3](./leetcode/408.valid-word-abbreviation.py) | Other | \- | \- | - |
942946
| [Leetcode-286](https://leetcode.com/problems/walls-and-gates/) | Walls And Gates | [c++](./leetcode/286.walls-and-gates.cpp), [python3](./leetcode/286.walls-and-gates.py) | Other | \- | \- | - |
943-
| [Leetcode-324](https://leetcode.com/problems/wiggle-sort-ii/) | Wiggle Sort II | [python3](./leetcode/324.wiggle-sort-ii.py) | Other | \- | \- | - |
944947
| [Leetcode-527](https://leetcode.com/problems/word-abbreviation/) | Word Abbreviation | [c++](./leetcode/527.word-abbreviation.cpp), [python3](./leetcode/527.word-abbreviation.py) | Other | \- | \- | - |
945948
| [Leetcode-127](https://leetcode.com/problems/word-ladder/) | Word Ladder | [c++](./leetcode/127.word-ladder.cpp), [python3](./leetcode/127.word-ladder.py) | Other | \- | \- | - |
946949
| [Leetcode-6](https://leetcode.com/problems/zigzag-conversion/) | Zigzag Conversion | [python3](./leetcode/6.zigzag-conversion.py) | Other | \- | \- | - |

leetcode/324.wiggle-sort-ii.cpp

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// Tag: Array, Divide and Conquer, Greedy, Sorting, Quickselect
2+
// Time: -
3+
// Space: -
4+
// Ref: -
5+
// Note: -
6+
7+
// Given an integer array nums, reorder it such that nums[0] < nums[1] > nums[2] < nums[3]....
8+
// You may assume the input array always has a valid answer.
9+
//  
10+
// Example 1:
11+
//
12+
// Input: nums = [1,5,1,1,6,4]
13+
// Output: [1,6,1,5,1,4]
14+
// Explanation: [1,4,1,5,1,6] is also accepted.
15+
//
16+
// Example 2:
17+
//
18+
// Input: nums = [1,3,2,2,3,1]
19+
// Output: [2,3,1,3,1,2]
20+
//
21+
//  
22+
// Constraints:
23+
//
24+
// 1 <= nums.length <= 5 * 104
25+
// 0 <= nums[i] <= 5000
26+
// It is guaranteed that there will be an answer for the given input nums.
27+
//
28+
//  
29+
// Follow Up: Can you do it in O(n) time and/or in-place with O(1) extra space?
30+
31+
class Solution {
32+
public:
33+
void wiggleSort(vector<int>& nums) {
34+
int n = nums.size();
35+
vector<int> tmp = nums;
36+
sort(tmp.begin(), tmp.end());
37+
int index = n - 1;
38+
39+
for (int i = 1; i < n; i+=2) {
40+
nums[i] = tmp[index--];
41+
}
42+
43+
for (int i = 0; i < n; i+=2) {
44+
nums[i] = tmp[index--];
45+
}
46+
}
47+
};
48+
49+
// follow-up
50+
class Solution {
51+
public:
52+
void wiggleSort(vector<int>& nums) {
53+
int n = nums.size();
54+
55+
int left = 0;
56+
int right = n - 1;
57+
58+
auto midptr = nums.begin() + right / 2;
59+
nth_element(nums.begin(), midptr, nums.end());
60+
int medean = *midptr;
61+
62+
auto V = [&](int i) {
63+
return (1 + 2 * i) % (n | 1);
64+
};
65+
66+
int i = 0;
67+
while (i <= right) {
68+
if (nums[V(i)] > medean) {
69+
swap(nums[V(i)], nums[V(left)]);
70+
i ++;
71+
left ++;
72+
} else if (nums[V(i)] < medean) {
73+
swap(nums[V(i)], nums[V(right)]);
74+
right --;
75+
} else {
76+
i ++;
77+
}
78+
}
79+
}
80+
};

leetcode/324.wiggle-sort-ii.py

Lines changed: 96 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,102 @@
1-
class Solution(object):
2-
def wiggleSort(self, nums):
1+
# Tag: Array, Divide and Conquer, Greedy, Sorting, Quickselect
2+
# Time: -
3+
# Space: -
4+
# Ref: -
5+
# Note: -
6+
7+
# Given an integer array nums, reorder it such that nums[0] < nums[1] > nums[2] < nums[3]....
8+
# You may assume the input array always has a valid answer.
9+
#  
10+
# Example 1:
11+
#
12+
# Input: nums = [1,5,1,1,6,4]
13+
# Output: [1,6,1,5,1,4]
14+
# Explanation: [1,4,1,5,1,6] is also accepted.
15+
#
16+
# Example 2:
17+
#
18+
# Input: nums = [1,3,2,2,3,1]
19+
# Output: [2,3,1,3,1,2]
20+
#
21+
#  
22+
# Constraints:
23+
#
24+
# 1 <= nums.length <= 5 * 104
25+
# 0 <= nums[i] <= 5000
26+
# It is guaranteed that there will be an answer for the given input nums.
27+
#
28+
#  
29+
# Follow Up: Can you do it in O(n) time and/or in-place with O(1) extra space?
30+
31+
class Solution:
32+
def wiggleSort(self, nums: List[int]) -> None:
333
"""
4-
:type nums: List[int]
5-
:rtype: void Do not return anything, modify nums in-place instead.
34+
Do not return anything, modify nums in-place instead.
635
"""
36+
n = len(nums)
737
arr = sorted(nums)
838

9-
for i in range(1, len(nums), 2):
39+
for i in range(1, n, 2):
1040
nums[i] = arr.pop()
1141

12-
for i in range(0, len(nums), 2):
13-
nums[i] = arr.pop()
42+
for i in range(0, n, 2):
43+
nums[i] = arr.pop()
44+
45+
# follow up
46+
class Solution:
47+
def wiggleSort(self, nums: List[int]) -> None:
48+
"""
49+
Do not return anything, modify nums in-place instead.
50+
"""
51+
n = len(nums)
52+
left = 0
53+
right = n - 1
54+
medean = self.quick_select(nums, right // 2)
55+
56+
def virtual(i:int):
57+
return (1 + 2 * i) % (n | 1)
58+
59+
i = 0
60+
while i <= right:
61+
if nums[virtual(i)] > medean:
62+
nums[virtual(i)], nums[virtual(left)] = nums[virtual(left)], nums[virtual(i)]
63+
i += 1
64+
left += 1
65+
elif nums[virtual(i)] < medean:
66+
nums[virtual(i)], nums[virtual(right)] = nums[virtual(right)], nums[virtual(i)]
67+
right -= 1
68+
else:
69+
i += 1
70+
71+
def quick_select(self, nums: list, k: int) -> int:
72+
left = 0
73+
right = len(nums) - 1
74+
while left < right:
75+
pick = self.partition(nums, left, right)
76+
if pick == k:
77+
break
78+
79+
if pick < k:
80+
left = pick + 1
81+
else:
82+
right = pick - 1
83+
84+
return nums[k]
85+
86+
def partition(self, nums:list, left: int, right: int) -> int:
87+
pivot = nums[right]
88+
l = left
89+
r = right - 1
90+
91+
while l <= r:
92+
if nums[l] > pivot and nums[r] < pivot:
93+
nums[l], nums[r] = nums[r], nums[l]
94+
95+
if nums[l] <= pivot:
96+
l += 1
97+
98+
if nums[r] >= pivot:
99+
r -= 1
100+
101+
nums[l], nums[right] = nums[right], nums[l]
102+
return l

list/9c-advanced.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@
103103
84. https://www.lintcode.com/problem/continuous-subarray-sum-ii/
104104
85. https://leetcode.com/problems/kth-largest-element-in-an-array/
105105
86. https://www.lintcode.com/problem/wiggle-sort/
106+
87. https://leetcode.com/problems/wiggle-sort-ii/
106107

107-
- https://www.lintcode.com/problem/wiggle-sort-ii/
108108
- https://www.lintcode.com/problem/nuts-bolts-problem/
109109
- https://www.lintcode.com/problem/flatten-list/
110110
- https://www.lintcode.com/problem/flatten-nested-list-iterator/

0 commit comments

Comments
 (0)