力扣第二十七天(heap(priority queue) topic)

problem Ⅰ

215. Kth Largest Element in an Array
Given an integer array nums and an integer k, return the kth largest element in the array.

Note that it is the kth largest element in the sorted order, not the kth distinct element.

Example 1:

Input: nums = [3,2,1,5,6,4], k = 2
Output: 5

Example 2:

Input: nums = [3,2,3,1,2,4,5,5,6], k = 4
Output: 4

solution 1

class Solution {
public:
    int findKthLargest(vector<int>& nums, int k) {
        priority_queue<int, vector<int>, greater<int>> pq;
        for(auto it : nums){
            pq.push(it);
            if(pq.size() > k) pq.pop();
        }
        return pq.top();
    }
};

NOTE:
time complexity : O ( n ) ∗ O ( l o g k ) = O ( n l o g k ) O(n)*O(logk) = O(nlogk) O(n)O(logk)=O(nlogk)
space complexity : O ( k ) O(k) O(k)
as we known, the time complexity of the operation like insert and pop in min/max heap is O ( l o g n ) O(logn) O(logn), while the size of the heap is n.,and the space complexity is O ( n ) O(n) O(n).

so it is clear that why the time complexity of this algorithm is O ( n ∗ l o g k ) O(n*logk) O(nlogk), cause we have to insert n elements into the priority queue, and every insert cost O ( l o g k ) O(logk) O(logk), while the priority queue’s max size is k

在这里插入图片描述

problem Ⅱ

347. Top K Frequent Elements
Given an integer array nums and an integer k, return the k most frequent elements. You may return the answer in any order.

Example 1:

Input: nums = [1,1,1,2,2,3], k = 2
Output: [1,2]

Example 2:

Input: nums = [1], k = 1
Output: [1]

solution

class Solution {
public:
    vector<int> topKFrequent(vector<int>& nums, int k) {
        unordered_map<int, int> maps;
        for(auto it : nums)
            maps[it]++;
        priority_queue<pair<int, int>> pq;
        for(auto it : maps)
            pq.push({it.second, it.first});
        vector<int> res;
        while(k--){
            res.push_back(pq.top().second);
            pq.pop();
        }
        return res;
    }
};

在这里插入图片描述
NOTE:
time complexity : O ( n l o g n ) O(nlogn) O(nlogn)

because the unordered_map is implemented using hash table, so the TC of insert operation in unordered_map is O ( 1 ) O(1) O(1) on average and O ( n ) O(n) O(n) worst case, so the TC of constructing a unordered_map is O ( n ) ∗ O ( 1 ) = O ( n ) O(n)*O(1)=O(n) O(n)O(1)=O(n)

the TC of constructing a priority_queue is O ( n ) ∗ O ( l o g n ) = O ( n l o g n ) O(n)*O(logn)=O(nlogn) O(n)O(logn)=O(nlogn)

the TC of geting the result is O ( k l o g n ) O(klogn) O(klogn)

so the total TC is O ( n ) + O ( n l o g n ) + O ( k l o g n ) = O ( n l o g n ) O(n)+O(nlogn)+O(klogn)=O(nlogn) O(n)+O(nlogn)+O(klogn)=O(nlogn)

space complexity : O ( n ) + O ( n ) = O ( n ) O(n)+O(n) = O(n) O(n)+O(n)=O(n)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值