[ํ๋ก๊ทธ๋๋จธ์ค-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)]๋ฅผ ๋ฐํํ๋ค.
'๐ Problem Solving > Programmers' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ํ๋ก๊ทธ๋๋จธ์ค-Lv1] ์ซ์ ๋ฌธ์์ด๊ณผ ์๋จ์ด / Python (0) | 2021.07.23 |
---|---|
[ํ๋ก๊ทธ๋๋จธ์ค-Lv3] ๋ฑ๊ตฃ๊ธธ / Python (0) | 2021.06.18 |
[ํ๋ก๊ทธ๋๋จธ์ค-Lv3] ๊ฐ์ฅ ๋จผ ๋ ธ๋ / Python (0) | 2021.06.18 |
[ํ๋ก๊ทธ๋๋จธ์ค-Lv3] ๋คํธ์ํฌ / Python (0) | 2021.06.17 |
[ํ๋ก๊ทธ๋๋จธ์ค-Lv3] ๋์คํฌ ์ปจํธ๋กค๋ฌ / Python (0) | 2021.06.17 |