-
Notifications
You must be signed in to change notification settings - Fork 116
/
Wiggle Sort II.py
40 lines (38 loc) · 889 Bytes
/
Wiggle Sort II.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
class Heap:
def __init__(self):
self.q = []
def push(self,data):
i = len(self.q)
self.q.append(data)
while i>0:
if self.q[i] > self.q[(i-1)//2]:
self.q[i], self.q[(i-1)//2] = self.q[(i-1)//2], self.q[i]
i = (i-1)//2
else: return
def pop(self):
if len(self.q)==0:return
self.q[0] = self.q[-1]
self.q.pop()
def heapify(i):
ind = i
l = 2*i+1
r = 2*i+2
if r<len(self.q) and self.q[ind] < self.q[r]: ind = r
if l<len(self.q) and self.q[ind] < self.q[l]: ind = l
if ind != i:
self.q[i], self.q[ind] = self.q[ind], self.q[i]
heapify(ind)
heapify(0)
def top(self):
return self.q[0]
class Solution:
def wiggleSort(self, nums: List[int]) -> None:
n = len(nums)
h = Heap()
for i in nums: h.push(i)
for i in range(1,n,2):
nums[i] = h.top()
h.pop()
for i in range(0,n,2):
nums[i] = h.top()
h.pop()