Priority queue with dynamic item priorities

前端 未结 5 495
抹茶落季
抹茶落季 2020-12-10 02:21

I need to implement a priority queue where the priority of an item in the queue can change and the queue adjusts itself so that items are always removed in the correct order

5条回答
  •  一个人的身影
    2020-12-10 02:53

    I am looking for just exactly the same thing!

    And here is some of my idea:

    1. Since a priority of an item keeps changing, it's meaningless to sort the queue before retrieving an item.
    2. So, we should forget using a priority queue. And "partially" sort the container while retrieving an item.

    And choose from the following STL sort algorithms: a. partition b. stable_partition c. nth_element d. partial_sort e. partial_sort_copy f. sort g. stable_sort

    partition, stable_partition and nth_element are linear-time sort algorithms, which should be our 1st choices.

    BUT, it seems that there is no those algorithms provided in the official Java library. As a result, I will suggest you to use java.util.Collections.max/min to do what you want.

提交回复
热议问题