๐ŸŒCoding/๐ŸŒŸPython3

[Python] heapq

๋ฟŒ์•ผ._. 2021. 8. 20. 20:53

์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋ฉด์„œ ์ฒ˜์Œ์œผ๋กœ heapq๋ฅผ ์ ‘ํ–ˆ๋‹ค.

 

๐Ÿ˜‰ heapq ๋ž€?

 

์šฐ์„ ์ˆœ์œ„ ํ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋ผ๊ณ ๋„ ํ•˜๋Š” ํž™ ํ ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ๊ตฌํ˜„์„ ์ œ๊ณตํ•œ๋‹ค.
ํž™์€ ๋ชจ๋“  ๋ถ€๋ชจ ๋…ธ๋“œ๊ฐ€ ์ž์‹๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ๊ฐ’์„ ๊ฐ–๋Š” ์ด์ง„ํŠธ๋ฆฌ์ด๋‹ค.

 

๐Ÿ˜‰ heapq import

import heapq

 

๐Ÿ˜‰ heapq ํ•จ์ˆ˜

heap=[]

 

  • ์›์†Œ ์ถ”๊ฐ€
    #์›์†Œ ์ถ”๊ฐ€
    heapq.heappush(heap, ์›์†Œ ๊ฐ’)
    heapq.heappush(heap,(-์›์†Œ ๊ฐ’, ์›์†Œ ๊ฐ’)) #์ตœ๋Œ€ ํž™ ๊ตฌํ˜„
  • ์›์†Œ ์ œ๊ฑฐ
    #์›์†Œ ์ œ๊ฑฐ
    heapq.heappop(heap) #heap์—์„œ ๊ฐ€์žฅ ์ž‘์€ ํ•ญ๋ชฉ ํŒ

'๐ŸŒCoding > ๐ŸŒŸPython3' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[Python] ์†Œ์ˆ˜์  ์ž๋ฆฌ ์ˆ˜ ์ถœ๋ ฅ  (0) 2021.10.04
[Python] list, tuple -> String  (0) 2021.09.01
[Python] lambda  (0) 2021.07.29
[Python] ์ˆœ์—ด, ์กฐํ•ฉ  (0) 2021.05.16
[Python] ๋”•์…”๋„ˆ๋ฆฌ  (0) 2021.05.02