這個東西 "variable 紀錄 current price 在 list 裡面第一個比它大的 alert price index" 你應該是沒有辦法事前知道 (pre-calculate) 你的方法比較像是用binary search的思路 力求在 O(log N) 的時間內找出這個 index 但是如面試中討論的 會遇到一個問題是 你要怎麼 "維護這個sorted list"? 在用戶 insert alert 的時候 你要 insert into this alert list and make sure it's still sorted 這個東西的複雜度 worst case = O(n), considering insert at the beginning then shift the rest array by 1 find insert point: O(log N) using binary search, insert O(N), so overall O(N)
To improve performance, priority queues are typically based on a heap, giving O(log n) performance for inserts and removals, and O(n) to build the heap initially from a set of n elements.
@a33939623 сағат бұрын
為什麼知道紅黑樹會不知道 priority queue
@WTF_howareyou2 сағат бұрын
priority queue是通常是使用heap實作吧
@a3393962Сағат бұрын
@@cia1099 也沒人規定pq不能是balanced search tree pq 是 abstraction 你要用 array, linked list, tree 實作都可以