문제 링크
정답 코드
def solution(n, k, cmd):
prev_of = {i: i - 1 for i in range(n)}
post_of = {i: i + 1 for i in range(n)}
post_of[n - 1] = None
prev_of[0] = None
deleted = []
idx = k
for command in cmd:
if command[0] == 'U':
X = int(command.split()[1])
for _ in range(X):
idx = prev_of[idx]
elif command[0] == 'D':
X = int(command.split()[1])
for _ in range(X):
idx = post_of[idx]
elif command[0] == 'C':
deleted.append((idx, prev_of[idx], post_of[idx]))
original_prev = prev_of[idx]
original_post = post_of[idx]
if original_prev is not None:
post_of[original_prev] = original_post
if original_post is not None:
prev_of[original_post] = original_prev
idx = original_post if original_post is not None else original_prev
elif command[0] == 'Z':
restore_idx, restore_prev, restore_post = deleted.pop()
if restore_prev is not None:
post_of[restore_prev] = restore_idx
if restore_post is not None:
prev_of[restore_post] = restore_idx
result = ['O'] * n
while deleted:
restore_idx, _, _ = deleted.pop()
result[restore_idx] = 'X'
return ''.join(result)