https://school.programmers.co.kr/learn/courses/30/lessons/42628
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
아이디어
heap의 heappop과 list의 pop을 사용해서 풀었다.
elif operation == "D 1": # 최댓값 삭제
queue.sort()
queue.pop()
최댓값을 삭제할 때는 리스트 정렬 후 pop 해주고,
elif operation == "D -1": # 최솟값 삭제
heapify(queue)
heappop(queue)
최솟값을 삭제할 때는 heapify로 리스트를 힙 구조로 바꾼 뒤 heappop 해주었다.
전체 코드
from heapq import heappush, heappop, heapify
def solution(operations):
queue = []
for operation in operations:
if operation[0] == "I":
heappush(queue, int(operation.split()[1]))
elif len(queue) == 0: continue
elif operation == "D 1": # 최댓값 삭제
queue.sort()
queue.pop()
elif operation == "D -1": # 최솟값 삭제
heapify(queue)
heappop(queue)
if len(queue) == 0 : return [0,0]
return [max(queue), min(queue)]
'Coding Test > Python' 카테고리의 다른 글
[프로그래머스] 가장 큰 수 (level2, python) (0) | 2024.01.16 |
---|---|
[프로그래머스] K번째수 (level1, python) (0) | 2024.01.16 |
[프로그래머스] 디스크 컨트롤러 (level3, python) (1) | 2024.01.16 |
[LeetCode] 290. Word Pattern (easy, python) (0) | 2024.01.15 |
[LeetCode] 383. Ransom Note (easy, python) (0) | 2024.01.15 |