Given scores of N athletes, find their relative ranks and the people with the top three highest scores, who will be awarded medals: “Gold Medal”, “Silver Medal” and “Bronze Medal”.
Example 1:
Note:
N is a positive integer and won’t exceed 10,000.
All the scores of athletes are guaranteed to be unique.
解法1:Priority Queue
这里感觉是要用一个带下标的排序,那么我们可以自己实现一个带下标的排序,或者可以使用已有的可排序的数据结构。对于可排序的比较熟悉的就是PriorityQueue了。
每一个放入的元素都会自动排序,那么我们只需要全部放入然后一个个读出同时判断是第几个元素就可以解决medal的归属问题。
用一个辅助class记录data和index,同时实现一个comparator,来比较这两个pair。
Java