Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 | 29 |
30 |
Tags
- 스택
- 그리디
- 다이나믹 프로그래밍
- DP
- 가상메모리
- BOJ
- MYSQL
- 운영체제
- python
- 딕셔너리
- 재귀
- level0
- BFS
- 다익스트라
- 브루트포스
- 수학
- level1
- 백준
- programmers
- level2
- dfs
- dict
- 구현
- 코딩테스트
- 힙
- 파이썬
- N과M
- 가상메모리 관리
- level3
- 프로그래머스
Archives
- Today
- Total
동캄의 코딩도장
프로그래머스 level3 [이중우선순위큐] 파이썬 본문
https://programmers.co.kr/learn/courses/30/lessons/42628
코딩테스트 연습 - 이중우선순위큐
programmers.co.kr
# 프로그래머스 이중우선순위큐
import heapq
def solution(operations):
answer = []
heap = []
for operation in operations:
oper, digit = map(str, operation.split())
if oper == 'I':
heapq.heappush(heap, int(digit))
elif heap and oper == 'D' and digit == '1':
heap = heapq.nlargest(len(heap), heap)[1:]
heapq.heapify(heap)
elif heap and oper == 'D' and digit == '-1':
heapq.heappop(heap)
if heap:
M = heapq.nlargest(len(heap), heap)[0]
heapq.heapify(heap)
answer.append(M)
answer.append(heap[0])
else:
answer.append(0)
answer.append(0)
return answer
heapq 라이브러리를 이용하여 문제를 간단히 해결할 수 있었다.
(이번 문제를 통해 heapq의 nlargest 함수의 사용법에 대해 알게 되었다. 기억해두자!)
'코테 > 프로그래머스' 카테고리의 다른 글
프로그래머스 level3 [여행경로] 파이썬 (0) | 2022.04.08 |
---|---|
프로그래머스 level3 [섬 연결하기] 파이썬 (0) | 2022.04.08 |
프로그래머스 level3 [디스크 컨트롤] 파이썬 (0) | 2022.04.07 |
프로그래머스 level3 [베스트앨범] 파이썬 (0) | 2022.04.07 |
프로그래머스 level3 [입국심사] 파이썬 (0) | 2022.04.07 |