Deque
Stack으로 사용하는 법
- dq.append(값)
- dq.pop()
- 오른쪽이 일반 기준이라고 생각하면 됨
Queue로 사용하는 법
- dq.append(값)
- dq.popleft()
rotate()
- rotate 값이 음수인 경우
- 왼쪽으로 큐가 돌게 됨 -> 앞쪽에 있는 요소가 뒤로 가게 됨
- rotate 값이 양수인 경우
- 왼쪽으로 큐가 돌게 됨 -> 뒤쪽에 있는 요소가 앞으로 가게 됨
정답코드
import collections
n, k = map(int, input().split())
deque = collections.deque()
ans = []
for i in range(1, n+1):
deque.append(i)
idx = 0
while 0 < len(deque):
idx = (idx + 2) % len(deque)
#rotate가 음수라면 앞쪽에 있는 요소가 해당 갯수만큼 뒤쪽으로 감
deque.rotate(-(k-1))
ans.append(deque.popleft())
print("<", end="")
for i in ans[:-1]:
print(str(i)+", ", end="")
print(ans[-1], end="")
print(">", end="")
'Algorithm' 카테고리의 다른 글
1966 프린터 큐 / 큐 (0) | 2025.01.16 |
---|---|
이상한 문자 만들기 / 프로그래머스 (0) | 2025.01.15 |
2609 최대공약수와 최소공배수 / 정수론 및 조합론 (1) | 2025.01.14 |
행렬의 덧셈 / 프로그래머스 (1) | 2025.01.14 |
자바 알고리즘 연습(8) (2) | 2025.01.13 |