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 |
Tags
- DesignPattern
- jwt
- 개발자부트캠프추천
- 프로그래머스
- infcon 2024
- TiL
- 전략패턴 #StrategyPattern #디자인패턴
- @FeignClient
- 파이썬
- 1주일회고
- 취업리부트코스
- 프로그래머스 이중우선순위큐
- 디자인패턴
- Python
- KPT회고
- 디자인 패턴
- 99클럽
- Spring multimodule
- jwttoken
- 빈 충돌
- 코딩테스트 준비
- 항해99
- 단기개발자코스
- 인프콘 2024
- 구글 OAuth login
- JavaScript
- 빈 조회 2개 이상
- 커스텀 헤더
- 개발자 취업
- spring batch 5.0
Archives
- Today
- Total
m1ndy5's coding blog
프로그래머스 이중우선순위큐 python 본문
heap을 사용해서 푸는 문제였다.
import heapq
def solution(operations):
answer = []
heap = []
for o in operations:
op, num = o.split()
if op == 'I':
heapq.heappush(heap, int(num))
elif op == 'D' and num == '1':
if len(heap) == 0:
continue
heap.remove(max(heap))
elif op == 'D' and num == '-1':
if len(heap) == 0:
continue
heapq.heappop(heap)
if len(heap) == 0:
answer = [0, 0]
else:
answer = [max(heap), heapq.heappop(heap)]
return answer
heap을 사용할 때는 import heap을 사용
heapq.heappush()를 통해 값을 넣고
heapq.heappop()을 하면 기본적으로는 최솟값이 나온다.
최대값을 뺄 때는 그냥 max()를 사용하여 빼주었다.
'알고리즘 with python > 알고리즘 스터디' 카테고리의 다른 글
프로그래머스 수식 최대화 with Python (1) | 2023.12.12 |
---|---|
유클리드 호제법으로 최대공약수, 최소공배수 구하기 (1) | 2023.12.06 |
Permutation & Combination 식 (0) | 2023.03.16 |
알고리즘 스터디 2, 3일차 정리(permutation, backtracking) (0) | 2023.03.01 |
알고리즘 스터디 1일차 정리 (0) | 2023.02.27 |