https://programmers.co.kr/learn/courses/30/lessons/42628
์ฝ๋ฉํ ์คํธ ์ฐ์ต - ์ด์ค์ฐ์ ์์ํ
programmers.co.kr
import heapq
class Double_heap:
def __init__(self):
self.maxheap = list()
self.minheap = list()
def push(self, number):
heapq.heappush(self.maxheap, -number)
heapq.heappush(self.minheap, number)
def pop_max(self):
if self.maxheap and self.minheap :
max_out = -heapq.heappop(self.maxheap)
self.minheap.remove(max_out)
heapq.heapify(self.minheap)
def pop_min(self):
if self.maxheap and self.minheap:
min_out = heapq.heappop(self.minheap)
self.maxheap.remove(-min_out)
heapq.heapify(self.maxheap)
def return_top(self):
if self.minheap:
top_min = heapq.heappop(self.minheap)
top_max = -heapq.heappop(self.maxheap)
return top_max, top_min
else :
return [0,0]
def solution(operations):
doubleHeap = Double_heap()
for operation in operations :
if operation[0] == "I" :
doubleHeap.push(int(operation[1:]))
elif operation == "D -1" :
doubleHeap.pop_min()
elif operation == "D 1" :
doubleHeap.pop_max()
else :
print("Error")
return
answer = doubleHeap.return_top()
return answer
์ด์ค ์ฐ์ ์์ ํ๋ฅผ ๊ทธ๋ฅ ํ ๋๊ฐ๋ฅผ ์ฌ์ฉํด์ ๊ตฌํํ๋ค.
๋น ํ์ ๋ฐ์ดํฐ๋ฅผ ์ญ์ ํ๋ผ๋ ์ฐ์ฐ -> ๋ฌด์
" ์ด์ค ์ฐ์ ์์ ํ๊ฐ ํ ์ฐ์ฐ operations๊ฐ ๋งค๊ฐ๋ณ์๋ก ์ฃผ์ด์ง ๋, ๋ชจ๋ ์ฐ์ฐ์ ์ฒ๋ฆฌํ ํ ํ๊ฐ ๋น์ด์์ผ๋ฉด [0,0] ๋น์ด์์ง ์์ผ๋ฉด [์ต๋๊ฐ, ์ต์๊ฐ]์ return ํ๋๋ก solution ํจ์๋ฅผ ๊ตฌํํด์ฃผ์ธ์ " -> ๋ง๋ง ์ ์ง์ผ์ ํ์
๋ ๋ฒจ 3์ด์ง๋ง ์ฌ์ด ํธ์ธ๊ฑฐ ๊ฐ๋ค.
https://programmers.co.kr/learn/courses/30/lessons/42748
์ฝ๋ฉํ ์คํธ ์ฐ์ต - K๋ฒ์งธ์
[1, 5, 2, 6, 3, 7, 4] [[2, 5, 3], [4, 4, 1], [1, 7, 3]] [5, 6, 3]
programmers.co.kr
๋ด ์ฝ๋๋ฅผ ์์ด ๋ฒ๋ ธ๋ค, ํ์ง๋ง ์ฝ๋ค
'๐ป CS' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
ํํ ๊ฐ๋ฅํ ์ด์งํธ๋ฆฌ (0) | 2023.01.31 |
---|---|
์ธ์ฌ๊ณ ๊ณผ (0) | 2023.01.30 |
ํ๋ก๊ทธ๋๋จธ์ค ๋ ๋ฒจ 1 ๋ค ํ๊ณ (0) | 2022.08.11 |
2022-08-08 ํ๋ก๊ทธ๋๋จธ์ค ์ธ์ฆ 100๋ฌธ์ ๋ฌ์ฑ (0) | 2022.08.08 |
ํ๋ก๊ทธ๋๋จธ์ค - ๋ค๋ฆฌ๋ฅผ ์ง๋๋ ํธ๋ญ, ์ฃผ์ ๊ฐ๊ฒฉ, ๋ ๋งต๊ฒ (0) | 2022.01.19 |