Notice
                              
                          
                        
                          
                          
                            Recent Posts
                            
                        
                          
                          
                            Recent Comments
                            
                        
                          
                          
                            Link
                            
                        250x250
    
    
  | 일 | 월 | 화 | 수 | 목 | 금 | 토 | 
|---|---|---|---|---|---|---|
| 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
                            
                        
                          
                          - LEVEL 2
- 소프티어
- 티스토리챌린지
- Lv. 0
- Dynamic Programming
- 자바스크립트
- Baekjoon
- Lv. 2
- Lv. 3
- DP
- 동적계획법
- 파이썬
- group by
- SQL 고득점 KIT
- 깊이 우선 탐색
- 프로그래머스
- 백준
- Java
- 너비 우선 탐색
- 오블완
- programmers
- Lv. 1
- SQL
- Python
- bfs
- javascript
- level 3
- join
- softeer
- dfs
                            Archives
                            
                        
                          
                          - Today
- Total
몸과 마음이 건전한 SW 개발자
프로그래머스 Lv3 이중우선순위큐 Python 본문
728x90
    
    
  문제 링크
https://school.programmers.co.kr/learn/courses/30/lessons/42628
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
정답 코드
import heapq
def solution(operations):
    maxHeap = []
    minHeap = []
    for operation in operations:
        oper, number = operation.split()
        number = int(number)
        if oper == "I":
            heapq.heappush(maxHeap, -number)
            heapq.heappush(minHeap, number)
        else:
            if number == 1:
                if maxHeap:
                    maxNumber = heapq.heappop(maxHeap)
                    minHeap.remove(-maxNumber)
            else:
                if minHeap:
                    minNumber = heapq.heappop(minHeap)
                    maxHeap.remove(-minNumber)
    answer = []
    if maxHeap:
        answer = [-maxHeap[0], minHeap[0]]
    else:
        answer = [0, 0]
    return answer풀이 방법
# 힙 불러오기
import heapq
# 힙 만들기
# 최대 힙
maxHeap = []
# 최소 힙
minHeap = []
# 힙 저장하기
number = 777
heapq.heappush(maxHeap, -number)
heapq.heappush(minHeap, number)
# 힙 원소 리턴 (가장 작은 원소를 리턴함)
minNumber = heapq.heappop(minHeap)
maxNumber = -heapq.heappop(maxHeap)
# --- 추가 heapq 함수 ---
# 기존 자료형을 힙 자료형으로 변환
list = [3, 1, 2]
heap = heapify(list)
# 출력하면
# heap = [1, 3, 2]느낀점
- remove 사용하면 시간초과 날 줄 알았는데 꼭 그러지는 않는다.
- 기본적인 heap의 구조: 최소 값을 뿌리 노드에 저장한다.
728x90
    
    
  '알고리즘' 카테고리의 다른 글
| 프로그래머스 Lv. 3 단어 변환 Python (1) | 2023.12.29 | 
|---|---|
| 프로그래머스 Lv. 3 네트워크 Python (1) | 2023.12.29 | 
| Softeer Level 3 안전운전을 도와줄 차세대 지능형 교통시스템 Python (1) | 2023.12.17 | 
| Softeer Level 3 사물인식 최소 면적 산출 프로그램 Python (0) | 2023.12.16 | 
| Softeer Level 3 교차로 Python (0) | 2023.12.14 | 
 
                  