forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpancake-sorting.py
122 lines (111 loc) · 3.81 KB
/
pancake-sorting.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
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
# Time: O(nlogn)
# Space: O(n)
class BIT(object): # 0-indexed.
def __init__(self, n):
self.__bit = [0]*(n+1) # Extra one for dummy node.
def add(self, i, val):
i += 1 # Extra one for dummy node.
while i < len(self.__bit):
self.__bit[i] += val
i += (i & -i)
def query(self, i):
i += 1 # Extra one for dummy node.
ret = 0
while i > 0:
ret += self.__bit[i]
i -= (i & -i)
return ret
# bit solution
class Solution(object):
def pancakeSort(self, arr):
"""
:type arr: List[int]
:rtype: List[int]
"""
bit = BIT(len(arr))
result = []
for i in xrange(len(arr)):
n = bit.query((arr[i]-1)-1)
bit.add(arr[i]-1, 1)
if n == i: # already sorted
continue
if n == 0: # (0..i-1)i
if i > 1:
result.append(i) # (i-1..0)i
result.append(i+1) # i(0..i-1)
else: # (0..n-1)n(n+1..i-1)i
if n > 1:
result.append(n) # (n-1..0)n(n+1..i-1)i
result.append(i) # (i-1..n+1)n(0..n-1)i
result.append(i+1) # i(n-1..0)n(n+1..i-1)
result.append(n+1) # (0..n-1)in(n+1..i-1)
return result
# Time: O(nlogn)
# Space: O(n)
# merge sort solution
class Solution2(object):
def pancakeSort(self, arr):
"""
:type arr: List[int]
:rtype: List[int]
"""
def smallerMergeSort(idxs, start, end, counts):
if end - start <= 0: # The size of range [start, end] less than 2 is always with count 0.
return 0
mid = start + (end - start) // 2
smallerMergeSort(idxs, start, mid, counts)
smallerMergeSort(idxs, mid + 1, end, counts)
r = start
tmp = []
for i in xrange(mid+1, end + 1):
# Merge the two sorted arrays into tmp.
while r <= mid and idxs[r][0] < idxs[i][0]:
tmp.append(idxs[r])
r += 1
if r <= mid:
tmp.append(idxs[i])
counts[idxs[i][1]] += r - start
while r <= mid:
tmp.append(idxs[r])
r += 1
# Copy tmp back to idxs
idxs[start:start+len(tmp)] = tmp
idxs = []
smaller_counts = [0]*len(arr)
for i, x in enumerate(arr):
idxs.append((x, i))
smallerMergeSort(idxs, 0, len(idxs)-1, smaller_counts)
result = []
for i, n in enumerate(smaller_counts):
if n == i: # already sorted
continue
if n == 0: # (0..i-1)i
if i > 1:
result.append(i) # (i-1..0)i
result.append(i+1) # i(0..i-1)
else: # (0..n-1)n(n+1..i-1)i
if n > 1:
result.append(n) # (n-1..0)n(n+1..i-1)i
result.append(i) # (i-1..n+1)n(0..n-1)i
result.append(i+1) # i(n-1..0)n(n+1..i-1)
result.append(n+1) # (0..n-1)in(n+1..i-1)
return result
# Time: O(n^2)
# Space: O(1)
class Solution3(object):
def pancakeSort(self, A):
"""
:type A: List[int]
:rtype: List[int]
"""
def reverse(l, begin, end):
for i in xrange((end-begin) // 2):
l[begin+i], l[end-1-i] = l[end-1-i], l[begin+i]
result = []
for n in reversed(xrange(1, len(A)+1)):
i = A.index(n)
reverse(A, 0, i+1)
result.append(i+1)
reverse(A, 0, n)
result.append(n)
return result