My Code (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)
Practice(To make rotate Que)
10, 3
1, 2, 3
0
10, 3
2, 9, 5
8
I already made rotate que by list.
But I want to make rotate que by deque.
So my question is 2.
What is the time complexity of my code?
How can I make rotate que by deque?
# 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)
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments