알고리즘/sw
[SW Expert Academy] 5097번 회전 python
HAN_PY
2020. 4. 3. 18:29
반응형
문제의 저작권은 SW Expert Academy에 있습니다.
import collections
TC = int(input())
for tc in range(1, TC+1):
N, M = map(int, input().split())
data = list(map(int, input().split()))
deq = collections.deque()
for i in data:
deq.append(i)
while M > 0:
a = deq.popleft()
deq.append(a)
M -= 1
print("#%d %d"%(tc, deq[0]))
Queue의 매우 기본 문제다
이때 포인트는 파이썬에서는 deque를 활용해야 좀더 빠르게 풀 수 있다.
queue라이브러리 같은 경우는 쓰래드 같은 복잡한 경우도 포함 되어있어서 deque 보다 더 오래 걸린다.
deque쓰는 법은 import collections
로 불러와서 deq=collections.deque()
를 적고 deq를 쓰면 된다.
여기서는 deq.append()
와 deq.popleft()
를 썼다.
추가적인 개념은 알고리즘의 queue 풀이 방법을 참고 하자.
index를 이용한 초간단 풀이
TC = int(input())
for tc in range(1, TC+1):
N, M = map(int, input().split())
data = list(map(int, input().split()))
res = M%N
sol = res
print("#%d %d"%(tc, data[sol]))
사실 이게 더 쉬운 풀이다.
반응형