practivceAlgorithm/programmers

[Programmers][Python][2021 카카오 인턴십] 표 편집

findTheValue 2021. 11. 14. 06:40

우선순위 큐 두개를 이용해 가운데값을 k로 운용했습니다.

마지막에 left를 right로 굳이 안옮겨도됐을텐데 조금 아쉽습니다.

또 set을 썼으면 좀 더 출력부분이 깔끔한 반복문으로 처리됐을 것 같습니다.

다른 분들은 연결리스트를 구현하신분들이 많았습니다.

 

from heapq import heappop, heappush

def solution(n, k, cmd):
    left, right, delete = [-i for i in range(k-1, -1, -1)], [i for i in range(k, n)], []
    for command in cmd:
        c, *num = command.split()
        if c == 'U':
            cnt = int(num.pop())
            for _ in range(cnt):
                heappush(right, -heappop(left))
        elif c == 'D':
            cnt = int(num.pop())
            for _ in range(cnt):
                heappush(left, -heappop(right))
        elif c == 'C':
            delete.append(heappop(right))
            if not right:
                heappush(right, -heappop(left))
        else:
            now = delete.pop()
            if now > right[0]:
                heappush(right, now)
            else:
                heappush(left, -now)
    answer, idx = '', 0
    while left:
        heappush(right, -heappop(left))
    while right:
        now = heappop(right)
        while idx != now:
            idx += 1
            answer += 'X'
        answer += 'O'
        idx += 1
    while idx != n:
        answer += 'X'
        idx += 1
    return answer