[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค-Lv3] ์ด์ค‘์šฐ์„ ์ˆœ์œ„ํ / Python

๐Ÿ“š Problem Solving/Programmers

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ์ด์ค‘์šฐ์„ ์ˆœ์œ„ํ

 

programmers.co.kr

import sys
import heapq

input = sys.stdin.readline

operations = [input().rstrip() for _ in range(int(input()))]


def solution(operations):
    q = []
    for s in operations:
        oper, num = s.split()[0], int(s.split()[1])
        if oper == "I":
            heapq.heappush(q, num)
        elif q and oper == "D":
            if num == 1:
                q.pop()
            else:
                heapq.heappop(q)
    if len(q) == 0:
        return [0, 0]
    return [max(q), min(q)]


print(solution(operations))

 

ํ•ด์„ค

ํž™์„ ์ด์šฉํ•œ๋‹ค. ์ตœ์†Ÿ๊ฐ’์„ ๋นผ์•ผํ•˜๋ฉด heappop, ์ตœ๋Œ“๊ฐ’์„ ๋นผ์•ผํ•˜๋ฉด pop์„ ์‹œ์ผœ์ค€๋‹ค ๋ชจ๋“  ๋ช…๋ น์–ด ์ˆ˜ํ–‰์ด ๋๋‚˜๋ฉด ๋ฆฌ์ŠคํŠธ ๊ธธ์ด์— ๋”ฐ๋ผ [0,0] ๋˜๋Š” [max(q), min(q)]๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.