[BOJ]2346

zzarbttoo·2025년 7월 25일
0

알고리즘

목록 보기
48/52

백준 2346 - 풍선 터트리기

  • 풍선의 갯수 N < 1000 으로 메모리 이슈가 크게 있을 것 같지는 않음
  • deque의 rotate 메소드를 이용하는 문제

문제 풀이

from collections import deque

N = int(input())
balloon = list(map(int, input().split()))
answer = []

b = deque([(i + 1, num) for i, num in enumerate(balloon)])

while True:
    (idx, cur) = b.popleft()
    answer.append(idx)

    if cur > 0:
        b.rotate(-(cur - 1))
    else:
        b.rotate(-cur)
    if not b:
        break

print(' '.join(map(str, answer)))

테스트 케이스

balloon = [3, 2, 1, -3, -1] -> 1 4 5 3 2
balloon = [1, 1, 1, 1, 1] -> 1 2 3 4 5 
balloon = [-1, -1, -1, -1, -1] -> 1 5 4 3 2 
profile
나는야 누워있는 개발머신

0개의 댓글