When should I use a TreeMap over a PriorityQueue and vice versa?

后端 未结 8 2156
南旧
南旧 2020-12-23 11:46

Seems they both let you retrieve the minimum, which is what I need for Prim\'s algorithm, and force me to remove and reinsert a key to update its value. Is there any advanta

8条回答
  •  一生所求
    2020-12-23 12:32

    I may be late to this answer but still.

    They have their own use-cases, in which either one of them is a clear winner.

    For Example:

    1: https://leetcode.com/problems/my-calendar-i TreeMap is the one you are looking at

    2: https://leetcode.com/problems/top-k-frequent-words you don't need the overhead of keys and values.

    So my answer would be, look at the use-case, and see if that could be done without key and value, if yes, go for PQueue else move to TreeMap.

提交回复
热议问题