leetcode 506. Relative Ranks | HEAP or priority_queue

Description

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:
Input: [5, 4, 3, 2, 1]
Output: ["Gold Medal", "Silver Medal", "Bronze Medal", "4", "5"]
Explanation: The first three athletes got the top three highest scores, so they got "Gold Medal", "Silver Medal" and "Bronze Medal". 
For the left two athletes, you just need to output their relative ranks according to their scores.
Note:
N is a positive integer and won't exceed 10,000.
All the scores of athletes are guaranteed to be unique.

Discuss

主要在于怎么用c++实现成对元素的排序.

priority_queue method

class Solution {
public:
    vector findRelativeRanks(vector &nums) {
        int numSize = nums.size();
        vector res(numSize);
        priority_queue> pq;
        for (int i = 0; i < nums.size(); ++i) pq.push(make_pair(nums[i], i));
        for (int i = 0; i < nums.size(); ++i) {
            auto p = pq.top();
            pq.pop();
            if (i == 0) res[p.second] = "Gold Medal";
            else if (i == 1) res[p.second] = "Silver Medal";
            else if (i == 2) res[p.second] = "Bronze Medal";
            else res[p.second] = to_string(i + 1);
        }
        return res;
    }
};

利用pq把pair排序, index是element.second, 每次top都是max元素, 整体上实际上是堆排序!

Simple Sorting O(n log n) solution

class Solution {
public:
    vector findRelativeRanks(vector& nums) {
        vector rank;
        for(int i=0; i nums[b];});
        vector ranks(nums.size());
        for(int i=3; i 0) ranks[rank[0]] = "Gold Medal";
        if(nums.size() > 1) ranks[rank[1]] = "Silver Medal";
        if(nums.size() > 2) ranks[rank[2]] = "Bronze Medal";
        return ranks;
    }
};

注意sort(rank.begin(), rank.end(), [&](int a, int b){return nums[a] > nums[b];});是C++11的lambda表达式, 也可以写为经典的compare函数.

Reference

  • leetcode 506. Relative Ranks
  • Lambda Expressions in C++
  • C++sort函数的用法

Heap | priority_queue

C++ STL中大根堆和小根堆的实现是priority_queue

需要使用priority_queue,定义在中:
#include
默认下,priority_queue是大顶堆,比如这样声明:
priority_queue max_heap;
这个等效于:
priority_queue, less> max_heap;
小顶堆的话,需要使用std:greater:
priority_queue, greater> min_heap;

debug下面代码观察pq元素, 理解堆insert和delete element的过程.

class Solution {
public:
    vector findRelativeRanks(vector &nums) {
        vector res;
        priority_queue> pq;
        for (int i = 0; i < nums.size(); ++i) {
            pq.push(make_pair(nums[i], i));
        }
        for (int i = 0; i < nums.size(); ++i) {
            auto p = pq.top();
            pq.pop();
            cout << p.first << "-";
            cout << p.second << " ";
        }
        return res;
    }
};

int main() {
    vector nums{1, 3, 2, 5, 4};
    Solution s;
    s.findRelativeRanks(nums);
    return 0;
}

pq的push(insert)以及pop(delete)中间结果图解如下:

关于大根堆的插入和删除代码, 参考<数据结构,算法与应用> 机械工业出版社, 第303页.

你可能感兴趣的:(leetcode,priority-queue,heap,sort,c++)