-
Notifications
You must be signed in to change notification settings - Fork 0
/
Orderly Queue.py
132 lines (106 loc) · 2.72 KB
/
Orderly Queue.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
123
124
125
126
127
128
129
130
131
132
# k = 3
# baaca move b
# aacab move c
# aaabc
# k = 3
# cbaaca move b
# caacab move c
# aacabc move c
# aaabcc
# if the (n+1)th chr is smaller than the any previous chr
# do the mv things
# 2 cases:
# k = 3
# acbaa -> acaab -> aaabc
# acbaa -> abaac -> aaacb
# but what if it's acba only
# acba -> acab -> aabc
# acba -> abac -> aacb
# principle is to let the former chr as small as possible
class Solution:
def orderlyQueue(self, s: str, k: int) -> str:
pass
from collections import deque
class Solution:
def orderlyQueue(self, s: str, k: int) -> str:
left, right = s[:k], s[k:]
max_left, min_right = max(left), min(right)
while True:
pass
# brute force log(n^2)
# baaca
# aacab
# aaabc
from collections import deque
class Solution:
def orderlyQueue(self, s: str, k: int) -> str:
s_q = deque(s)
i = 1
while i <= k:
min_chr = min(s_q[i-1:])
while s_q[i-1] != min_chr:
s_q.append(s_q.popleft())
i += 1
return ''.join(s_q)
# brute force log(n^2)
# baaca
# aacab
# aaabc
from collections import deque
class Solution:
def orderlyQueue(self, s: str, k: int) -> str:
s_q = deque(s)
def find_min(q, i):
my_min = False
for j in range(i, len(q)):
if not my_min:
my_min = q[j]
else:
if q[j] < my_min:
my_min = q[j]
return my_min
i = 1
while i <= k:
min_chr = find_min(s_q, i-1)
while s_q[i-1] != min_chr:
s_q.append(s_q.popleft())
i += 1
return ''.join(s_q)
# brute force log(n^2)
# baaca
# aacab
# aaabc
from collections import deque
class Solution:
def orderlyQueue(self, s: str, k: int) -> str:
s_q = deque(s)
i = 1
output_list = []
while i <= k:
min_chr = min(s_q)
while s_q[0] != min_chr:
s_q.append(s_q.popleft())
output_list.append(s_q.popleft())
i += 1
for s in s_q:
output_list.append(s)
return ''.join(output_list)
### failed at print(Solution().orderlyQueue("xmvzi", 2)) imvzx should be "imvxz"
# "xmvzi" k=2
s_list = []
for s in "xmvzi":
s_list.append(ord(s))
print(s_list)
# [120, 109, 118, 122, 105]
# [105, 120, 109, 118, 122]
# [105, 109, 118, 122, 120]
print([chr(i) for i in [105, 109, 118, 122, 120]])
# which is ['i', 'm', 'v', 'z', 'x']
# "xmvzi"
# mvzix
# mzixv
# mixvz
# ixvzm
# imxvz
print(Solution().orderlyQueue("baaca", 3))
print(Solution().orderlyQueue("xmvzi", 2))