-
Notifications
You must be signed in to change notification settings - Fork 24
/
280 - Wiggle Sort.py
57 lines (49 loc) · 1.77 KB
/
280 - Wiggle Sort.py
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
# Solution 1 - Sort and swap every other index that isn't wiggle sorted
# Runtime: O(nlogn)
class Solution(object):
def wiggleSort(self, nums):
"""
:type nums: List[int]
:rtype: void Do not return anything, modify nums in-place instead.
"""
nums.sort()
i = 1
while i+1 < len(nums):
if nums[i] < nums[i+1]:
self.swap(nums, i, i+1)
i += 2
def swap(self, nums, i, j):
tmp = nums[i]
nums[i] = nums[j]
nums[j] = tmp
# Solution 2 - Observe that the greedy solution works
# Let's say we have array that is already wiggly sorted up to, but not including, index i
# What can we do to nums[i] in order to be wiggly sorted?
# We have two cases:
# 1. i is odd
# In this case, nums[i-1] <= nums[i-2]
# If nums[i] >= nums[i-1], perfect nums[i] is already wiggly sorted
# If nums[i] < nums[i-1] <= nums[i-2] (by default),
# we can swap nums[i] with nums[i-1]and we'll be wiggly sorted!
# 2. i is even
# In this case, nums[i-1] >= nums[i-2]
# If nums[i] <= nums[i-1], perfect nums[i] is already wiggly sorted
# If nums[i] > nums[i-1] >= nums[i-2] (by default),
# we can swap nums[i] with nums[i-1] and we'll be wiggly sorted!
# Runtime: O(n)
class Solution(object):
def wiggleSort(self, nums):
"""
:type nums: List[int]
:rtype: void Do not return anything, modify nums in-place instead.
"""
for i in range(1, len(nums)):
odd = i & 1
if odd and nums[i] < nums[i-1]:
self.swap(nums, i, i-1)
elif not odd and nums[i] > nums[i-1]:
self.swap(nums, i, i-1)
def swap(self, nums, i, j):
tmp = nums[i]
nums[i] = nums[j]
nums[j] = tmp