코딩테스트/백준 (python)
백준 / 2346번 / 풍선 터뜨리기 / python 파이썬
seulll
2024. 9. 9. 19:30
문제 : 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) : 덱을 오른쪽으로 회전시킴