목차

표 편집

ps
링크programmers.co.kr/…
출처프로그래머스
문제 번호81303
문제명표 편집
레벨Level 3
분류

Order Statistic Tree

시간복잡도O(n+m+q) or O(n+qlogn)
인풋사이즈n<=1,000,000, q<=200,000, m<=1,000,000
사용한 언어Python
해결날짜2021/07/10
출처

ps:problems:programmers:2021_카카오_채용연계형_인턴십

풀이

코드

"""Solution code for "Programmers 81303. 표 편집".

- Problem link: https://programmers.co.kr/learn/courses/30/lessons/81303
- Solution link: http://www.teferi.net/ps/problems/programmers/81303
"""

from teflib import segmenttree


def solution(n, k, cmd):
    table = segmenttree.OrderStatisticTree([1] * n)
    stack = []
    k += 1
    for cur_cmd in cmd:
        if cur_cmd == 'C':
            num = table.kth(k)
            table.add(num, -1)
            stack.append(num)
            k = min(k, table.size())
        elif cur_cmd == 'Z':
            num = stack.pop()            
            if num < table.kth(k):
                k += 1
            table.add(num)
        else:
            c, x = cur_cmd.split()
            k += int(x) * (1 if c =='D' else -1)

    return ''.join('O' if table.count(i) > 0 else 'X' for i in range(n))