如何通过双端队列进行旋转队列?
How can I make rotate que by deque?
我的代码(by.List)
n, m = map(int, input().split())
x = list(map(int,input().split()))
a = [z for z in range(1, n+1)]
print(a)
# x = [d, e, f]
f_idx = 0
b_idx = 0
for i in range(len(x)-1):
if a.index(x[i]) == 0 :
a.pop(0)
print(a)
elif a.index(x[i]) <= a.index(x[i+1]) :
while True:
a.append(a.pop(0))
f_idx += 1
if a.index(x[i]) == 0 :
a.pop(0)
print(a)
break
elif a.index(x[i]) > a.index(x[i+1]) :
while True:
a.insert(0,a.pop(-1))
b_idx +=1
if a.index(x[i]) == 0 :
a.pop(0)
print(a)
break
print(a)
break
if i == len(x)-2 :
if a.index(x[i+1]) == 0 :
a.pop(0)
elif a.index(x[i+1]) < len(a)/2:
while True:
a.append(a.pop(0))
f_idx += 1
if a.index(x[i+1]) == 0 :
a.pop(0)
print(a)
break
else:
while True:
a.insert(0,a.pop(-1))
b_idx +=1
if a.index(x[i+1]) == 0 :
a.pop(0)
print(a)
break
print("f_idx :", f_idx)
print("b_idx :", b_idx)
print(a)
练习(制作旋转阙)
- 输入1:
10、3
1, 2, 3
- 输出 1 : (f.idx + b.idx)
0
- 输入2:
10、3
2, 9, 5
- 输出 2 : (f.idx + b.idx)
8
我已经按列表轮换问题了。
但是我想通过双端队列来轮换队列。
所以我的问题是2.
我的代码的时间复杂度是多少?
如何通过双端队列实现轮转队列?
# deque
from collections import deque
n, m = map(int, input().split())
x = list(map(int,input().split()))
a = deque(z for z in range(1, n+1))
print(a)
# x = [d, e, f]
f_idx = 0
b_idx = 0
for i in range(len(x)-1):
if a.index(x[i]) == 0 :
a.popleft()
print(a)
elif a.index(x[i]) <= a.index(x[i+1]) :
while True:
a.append(a.popleft())
f_idx += 1
if a.index(x[i]) == 0 :
a.popleft()
print(a)
break
elif a.index(x[i]) > a.index(x[i+1]) :
while True:
a.appendleft(a.pop())
b_idx +=1
if a.index(x[i]) == 0 :
a.popleft()
print(a)
break
if i == len(x)-2 :
if a.index(x[i+1]) == 0 :
a.popleft()
elif a.index(x[i+1]) < len(a)/2:
while True:
a.append(a.popleft())
f_idx += 1
if a.index(x[i+1]) == 0 :
a.popleft()
print(a)
break
else:
while True:
a.appendleft(a.pop())
b_idx +=1
if a.index(x[i+1]) == 0 :
a.popleft()
print(a)
break
print("f_idx :", f_idx)
print("b_idx :", b_idx)
print(a)
我的代码(by.List)
n, m = map(int, input().split())
x = list(map(int,input().split()))
a = [z for z in range(1, n+1)]
print(a)
# x = [d, e, f]
f_idx = 0
b_idx = 0
for i in range(len(x)-1):
if a.index(x[i]) == 0 :
a.pop(0)
print(a)
elif a.index(x[i]) <= a.index(x[i+1]) :
while True:
a.append(a.pop(0))
f_idx += 1
if a.index(x[i]) == 0 :
a.pop(0)
print(a)
break
elif a.index(x[i]) > a.index(x[i+1]) :
while True:
a.insert(0,a.pop(-1))
b_idx +=1
if a.index(x[i]) == 0 :
a.pop(0)
print(a)
break
print(a)
break
if i == len(x)-2 :
if a.index(x[i+1]) == 0 :
a.pop(0)
elif a.index(x[i+1]) < len(a)/2:
while True:
a.append(a.pop(0))
f_idx += 1
if a.index(x[i+1]) == 0 :
a.pop(0)
print(a)
break
else:
while True:
a.insert(0,a.pop(-1))
b_idx +=1
if a.index(x[i+1]) == 0 :
a.pop(0)
print(a)
break
print("f_idx :", f_idx)
print("b_idx :", b_idx)
print(a)
练习(制作旋转阙)
- 输入1:
10、3
1, 2, 3
- 输出 1 : (f.idx + b.idx)
0
- 输入2:
10、3
2, 9, 5
- 输出 2 : (f.idx + b.idx)
8
我已经按列表轮换问题了。
但是我想通过双端队列来轮换队列。
所以我的问题是2.
我的代码的时间复杂度是多少?
如何通过双端队列实现轮转队列?
# deque
from collections import deque
n, m = map(int, input().split())
x = list(map(int,input().split()))
a = deque(z for z in range(1, n+1))
print(a)
# x = [d, e, f]
f_idx = 0
b_idx = 0
for i in range(len(x)-1):
if a.index(x[i]) == 0 :
a.popleft()
print(a)
elif a.index(x[i]) <= a.index(x[i+1]) :
while True:
a.append(a.popleft())
f_idx += 1
if a.index(x[i]) == 0 :
a.popleft()
print(a)
break
elif a.index(x[i]) > a.index(x[i+1]) :
while True:
a.appendleft(a.pop())
b_idx +=1
if a.index(x[i]) == 0 :
a.popleft()
print(a)
break
if i == len(x)-2 :
if a.index(x[i+1]) == 0 :
a.popleft()
elif a.index(x[i+1]) < len(a)/2:
while True:
a.append(a.popleft())
f_idx += 1
if a.index(x[i+1]) == 0 :
a.popleft()
print(a)
break
else:
while True:
a.appendleft(a.pop())
b_idx +=1
if a.index(x[i+1]) == 0 :
a.popleft()
print(a)
break
print("f_idx :", f_idx)
print("b_idx :", b_idx)
print(a)