堆積結構
背景知識
最大最小堆積
python
理解問題
建構堆積取得最大最小值。
Java
Heap is use for PriorityQueue
TC:
Offer(), add(): O(logn) // because heap is tree structure
Poll(), remove(): O(logn)
peek(): O(1)
contains: O(n)
Nlargest/Nsmallest: O(n)
SC:
O(n)
1 | import java.util.*; |
Python
TC: Push/Pop: O(logn), Nlargest/Nsmallest: [Python]O(nlogn)
SC: O(n)
1 | import heapq, copy |
如果你覺得這篇文章很棒,請你不吝點讚 (゚∀゚)