문제 : https://www.acmicpc.net/problem/2346
코드
from collections import deque
import sys
input = sys.stdin.readline
n = int(input())
q = deque(enumerate(map(int, input().split())))
ans =[]
while q:
idx, num = q.popleft()
ans.append(idx + 1)
if num > 0:
q.rotate(-(num - 1))
elif num < 0:
q.rotate(-num)
print(' '.join(map(str, ans)))
deque.rotate()
deque는 rotate() 함수를 사용하여 회전시킬 수 있다.
- rotate(1): 인자 값이 양수이면 오른쪽 회전
- rotate(-1): 인자 값이 음수이면 왼쪽 회전
q.rotate(-(num-1)) : 덱을 왼쪽으로 회전시킴
q.rotate(-num) : 덱을 오른쪽으로 회전시킴
'코딩테스트 > 백준 (python)' 카테고리의 다른 글
백준 / 28278번 / 스택 2 / python 파이썬 (0) | 2024.09.12 |
---|---|
백준 / 1629번 / 곱셈 / python 파이썬 (0) | 2024.09.09 |
백준 / 7576번 / 토마토 / python 파이썬 (0) | 2024.09.05 |
백준 / 1463번 / 1로 만들기 / DP / python 파이썬 (0) | 2024.09.05 |
백준 / 2740번 / 행렬 곱셈 / python 파이썬 (0) | 2024.09.04 |