Skip to content

Commit 5938cf5

Browse files
committed
1 parent 50de6cd commit 5938cf5

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
"""
2+
- 우선순위 큐 함수 조건
3+
- l 숫자 : 큐에 해당 숫자 삽입
4+
- D -1 : 큐에서 최소값 삭제(빈 큐면 무시 )
5+
- D 1 : 큐에서 최대값 삭제
6+
- 출력 : 연산 종료 후 큐가 비어 있음 [0,0] / 비어 있지 않음 [최대값, 최솟값]
7+
-
8+
"""
9+
import heapq
10+
def solution(operations):
11+
answer = []
12+
heap = []
13+
14+
def prior_queue(operate , heap) : # 입력 명령문 종류에 따라 3가지 동작 중 택 1을 적용한다.
15+
16+
action , num = operate.split()
17+
# [0] (예외처리)빈 heap에 삭제 명령이면, 무시한다.
18+
if not heap and action == "D":
19+
return heap
20+
# [1] 큐에 삽입하기
21+
if action == "I" :
22+
heapq.heappush(heap , int(num))
23+
# [2] 최소값 삭제
24+
elif action == "D" and num == "-1":
25+
heapq.heapify(heap)
26+
heapq.heappop(heap)
27+
#[3] 최대값 삭제
28+
elif action == 'D' and num == "1" :
29+
heap = list(map(lambda x : -1*x , heap))
30+
heapq.heapify(heap)
31+
heapq.heappop(heap)
32+
heap = list(map(lambda x : -1*x , heap))
33+
34+
return heap
35+
36+
#2. 입력 명령문에 대해 반복 동작하기
37+
for o in operations :
38+
heap=prior_queue(o , heap )
39+
40+
#3. 출력 형태 맞춰 변형하기
41+
if len(heap) >=2 : # (1)안 비어 있으면 -> 최대값 & 최소값 출력
42+
heap.sort()
43+
return [heap[-1] , heap[0]]
44+
elif len(heap) < 1 : # (2) 비어 있음 -> [0,0]
45+
return [0,0]
46+
else : # (3) heap 이 1개 남아 있을떄 -> 최대값 = 최소값 *[테케 8,9,10]
47+
return [heap[0] , heap[0]]
48+
49+
return answer

0 commit comments

Comments
 (0)