표와 표를 제어하는 명령어들을 입력받고 명령어들을 모두 실행한 뒤의 표와 초기 표를 비교하여 어떤 행이 남아있는지 구하는 문제입니다. 명령어의 종류는 위로 X칸 이동(U X), 아래로 X칸 이동(D X), 행 삭제(C), 가장 최근에 삭제한 행 복구(Z)가 있고 명령어들을 모두 실행한 뒤에 남아있는 행은 O로 표시하고 삭제된 행은 X로 표시합니다.
from collections import deque
import heapq
def solution(n, k, cmd):
answer = ''
up = []
down = []
temp = deque()
for i in range(n):
heapq.heappush(down, i)
for i in range(k):
now = heapq.heappop(down)
heapq.heappush(up, -now)
for c in cmd:
if c == "C":
temp.append(heapq.heappop(down))
if not down:
heapq.heappush(down, -heapq.heappop(up))
elif c == "Z":
if temp[-1] < down[0]:
heapq.heappush(up, -temp.pop())
else:
heapq.heappush(down, temp.pop())
else:
c1, c2 = c.split(" ")
if c1 == "D":
for i in range(int(c2)):
heapq.heappush(up, -heapq.heappop(down))
else:
for i in range(int(c2)):
heapq.heappush(down, -heapq.heappop(up))
total = [0] * n
while up:
total[-heapq.heappop(up)] = 1
while down:
total[heapq.heappop(down)] = 1
for i in total:
if i == 1:
answer += "O"
else:
answer += "X"
return answer