数据结构---堆---简单

https://leetcode-cn.com/tag/heap-priority-queue/problemset/

506. 相对名次

给你一个长度为 n 的整数数组 score ,其中 score[i] 是第 i 位运动员在比赛中的得分。所有得分都 互不相同 。

运动员将根据得分 决定名次 ,其中名次第 1 的运动员得分最高,名次第 2 的运动员得分第 2 高,依此类推。运动员的名次决定了他们的获奖情况:

名次第 1 的运动员获金牌 "Gold Medal" 。
名次第 2 的运动员获银牌 "Silver Medal" 。
名次第 3 的运动员获铜牌 "Bronze Medal" 。
从名次第 4 到第 n 的运动员,只能获得他们的名次编号(即,名次第 x 的运动员获得编号 "x")。

使用长度为 n 的数组 answer 返回获奖,其中 answer[i] 是第 i 位运动员的获奖情况。

示例 1:

输入:score = [5,4,3,2,1]
输出:[“Gold Medal”,“Silver Medal”,“Bronze Medal”,“4”,“5”]
解释:名次为 [1st, 2nd, 3rd, 4th, 5th] 。

示例 2:

输入:score = [10,3,8,9,4]
输出:[“Gold Medal”,“5”,“Bronze Medal”,“Silver Medal”,“4”]
解释:名次为 [1st, 5th, 3rd, 2nd, 4th] 。

提示:

n == score.length
1 <= n <= 104
0 <= score[i] <= 106
score 中的所有值 互不相同
// class Solution {
// public:
//     vector<string> findRelativeRanks(vector<int>& score) { // 参数是输入
//         priority_queue<pair<int,int>>pq; // 创建队列
//         for(int i=0; i<score.size(); i++)
//         {
//             pq.push({score[i],i}); // 实例化队列
//         }
//         vector<string> ans(score.size());
//         string name[3]={"Gold Medal", "Silver Medal", "Bronze Medal"};
//         int i = 0;
//         while(!pq.empty()){
//             auto t = pq.top();
//             if(i < 3){
//                 ans[t.second] = name[i];
//             }
//             else{
//                 ans[t.second]= to_string(i+1);
//             }
//             pq.pop();
//             i++;
//         }
//     return ans;
//     }
// };
class Solution {
public:
    vector<string> findRelativeRanks(vector<int>& score) { // 参数是输入
        priority_queue<pair<int,int>>pq; // 创建队列
        int n = score.size();
        for(int i=0; i<n; i++)
        {
            pq.push({score[i],i}); // 实例化队列
        }
        vector<string> ans(n);
        string name[3]={"Gold Medal", "Silver Medal", "Bronze Medal"};
        for(int i =0; i< n; i++){
            auto t = pq.top(); 
            pq.pop();//必须这里就弹出来,放在后面不对
            if(i==0){ans[t.second]=name[i];continue;}
            if(i==1){ans[t.second]=name[i];continue;}
            if(i==2){ans[t.second]=name[i];continue;}
            ans[t.second]=to_string(i+1);
           
        }
    return ans;
    }
};

703. 数据流中的第 K 大元素

设计一个找到数据流中第 k 大元素的类(class)。注意是排序后的第 k 大元素,不是第 k 个不同的元素。

请实现 KthLargest 类:

KthLargest(int k, int[] nums) 使用整数 k 和整数流 nums 初始化对象。
int add(int val) 将 val 插入数据流 nums 后,返回当前数据流中第 k 大的元素。

示例:

输入:
[“KthLargest”, “add”, “add”, “add”, “add”, “add”]
[[3, [4, 5, 8, 2]], [3], [5], [10], [9], [4]]
输出:
[null, 4, 5, 5, 8, 8]

解释:
KthLargest kthLargest = new KthLargest(3, [4, 5, 8, 2]);
kthLargest.add(3); // return 4
kthLargest.add(5); // return 5
kthLargest.add(10); // return 5
kthLargest.add(9); // return 8
kthLargest.add(4); // return 8

提示:

1 <= k <= 104
0 <= nums.length <= 104
-104 <= nums[i] <= 104
-104 <= val <= 104
最多调用 add 方法 104 次
题目数据保证,在查找第 k 大元素时,数组中至少有 k 个元素
class KthLargest {
public:
    KthLargest(int k, vector<int>& nums) {
        myk=k;
        int n = nums.size();
 
        for(auto i : nums){
            mynums.push(i);
            if(mynums.size() > myk){
                mynums.pop(); // 一直维护大小为k的小根堆。里面是前k 个最大的数,小的在最前面,如果尺寸大于k就弹出去。
            }
        }

    }
    int add(int val) {
        int res;
        mynums.push(val);
        if(mynums.size() > myk){
            mynums.pop();
        }
        res = mynums.top();
        return res;
    }
private:
    priority_queue<int, vector<int>, greater<int>> mynums; // 小根堆
    int myk;
};
/*
维护大根堆不太好,需要取出第k个数据,前k个弹出的还不能删除不管,还需要继续维护,但是这样会超时
*/

//  class KthLargest {
// private:
//     int k;
//     priority_queue<int, vector<int>, greater<int>> small_que; 
// public:
//     KthLargest(int k, vector<int>& nums) 
//     {
//         this->k = k;
//         for (auto i : nums)
//         {
//             small_que.push(i);
//         }
            
//     }
    
//     int add(int val) 
//     {
//         small_que.push(val);        // 新来的元素插入堆中
//         for(int i = 0; i < this->k;i++){
//             auto t = small_que.top();
//             small_que.pop();
//         }
//         return small_que.top();     // 堆顶元素就是最大的k个元素中最小的
//     }
// };

/**
 * Your KthLargest object will be instantiated and called as such:
 * KthLargest* obj = new KthLargest(k, nums);
 * int param_1 = obj->add(val);
 */

//  class KthLargest {
// private:
//     int k;
//     priority_queue<int, vector<int>, greater<int>> small_que; 
// public:
//     KthLargest(int k, vector<int>& nums) 
//     {
//         this->k = k;
//         for (auto i : nums)
//         {
//             small_que.push(i);
//             // 维护一个大小为k的小根堆(里面是nums中最大的k个元素)
//             if (small_que.size() > k)
//                 small_que.pop();
//         }
            
//     }
    
//     int add(int val) 
//     {
//         small_que.push(val);        // 新来的元素插入堆中
//         if (small_que.size() > k)   // 维护堆的大小为k
//             small_que.pop();
//         return small_que.top();     // 堆顶元素就是最大的k个元素中最小的
//     }
// };
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值