1、最后一块石头的重量
1046. 最后一块石头的重量 - 力扣(LeetCode)
class Solution
{
public:
int lastStoneWeight(vector<int>& stones)
{
priority_queue<int> pq(stones.begin(),stones.end());
while(pq.size() > 1)
{
int y = pq.top(); pq.pop();
int x = pq.top(); pq.pop();
if(x == y){}
else
{
y = y-x;
pq.push(y);
}
}
if(pq.size() == 0) return 0;
return pq.top();
}
};
2、数据流中的第k大元素
703. 数据流中的第 K 大元素 - 力扣(LeetCode)
Top K问题
class KthLargest
{
priority_queue<int,vector<int>,greater<int>> pq;//小根堆
int _k;
public:
KthLargest(int k, vector<int>& nums)
{
_k = k;
for(auto x : nums)
{
pq.push(x);
if(pq.size() > _k) pq.pop();
}
}
int add(int val)
{
pq.push(val);
if(pq.size() > _k) pq.pop();
return pq.top();
}
};
/**
* Your KthLargest object will be instantiated and called as such:
* KthLargest* obj = new KthLargest(k, nums);
* int param_1 = obj->add(val);
*/
3、前K个高频单词
class Solution
{
typedef pair<string,int> PSI;
struct cmp
{
bool operator()(const PSI& a,const PSI& b)
{
//频次相同,按照大根堆排列
if(a.second == b.second)
return a.first < b.first;
return a.second > b.second;//按频次,小根堆排列
}
};
public:
vector<string> topKFrequent(vector<string>& words, int k)
{
//1、统计每个单词出现的频次
unordered_map<string,int> hash;
for(auto s : words)
hash[s]++;
//2、创建大小为k的堆
priority_queue<PSI,vector<PSI>,cmp> heap;
//3、TopK的主逻辑
for(auto& psi : hash)
{
heap.push(psi);
if(heap.size() > k) heap.pop();
}
//4、提取结果
vector<string> res(k);
for(int i = k-1;i>=0;i--)
{
res[i] = heap.top().first;
heap.pop();
}
return res;
}
};
4、数据流的中位数
解法一:直接sort,然后求中位数(时间复杂度太高,每次求中位数都要sort,会超时)
class MedianFinder
{
vector<int> nums;
public:
MedianFinder()
{
}
void addNum(int num)
{
nums.push_back(num);
}
double findMedian()
{
sort(nums.begin(),nums.end());
int n = nums.size();
if(n % 2 == 1)
return nums[n/2];
return ((double)nums[n/2] + (double)nums[n/2 -1])/2;
}
};
/**
* Your MedianFinder object will be instantiated and called as such:
* MedianFinder* obj = new MedianFinder();
* obj->addNum(num);
* double param_2 = obj->findMedian();
*/
解法二:插入排序的思想
如果add调用很多次的话,时间复杂度也很高,但这题能过。
class MedianFinder
{
vector<int> nums;
public:
MedianFinder()
{
}
void addNum(int num)
{
if(nums.size() == 0) nums.push_back(num);
else
{
auto it = nums.begin();
while(it != nums.end() && *it < num)
it++;
nums.insert(it,num);
}
}
double findMedian()
{
int n = nums.size();
if(n % 2 == 1)
return nums[n/2];
return ((double)nums[n/2] + (double)nums[n/2 -1])/2;
}
};
/**
* Your MedianFinder object will be instantiated and called as such:
* MedianFinder* obj = new MedianFinder();
* obj->addNum(num);
* double param_2 = obj->findMedian();
*/
解法三:大小堆来维护数据流的中位数
大根堆:记录前一半的最小元素。
小根堆:记录后一半的最大元素。
中位数 = (小根堆top + 大根堆top)/2;
class MedianFinder
{
priority_queue<int> left_heap;//前k个最小元素,大堆,left.top就是左边小的数里最大的
priority_queue<int,vector<int>,greater<int>> right_heap;//前k个最大元素,小堆,right.top就是右边大的数里最小的
int left_n = 0, right_n = 0;
//规定,如果一共有奇数个数,那规定left_n == right_n + 1;
//如果是偶数个,则left_n == right_n
//添加数的时候,就要保证这个特性--”两边数据个数均分“
//添加数的细节:
//1、如果left_n == right_n
//(1)left_top >= num(左堆的堆顶元素>=num) || left_n == 0,直接进入左堆,left_n++
//(2)left_top < num(左堆的堆顶元素<num),num进入右堆,此时left_n == right_n-1,此时要将right.top放进左堆,然后,left_n++,left_n == right_n + 1
//2、如果left_n == right_n + 1
//(1)left_top >= num,直接放入左堆,left_n == right_n +2,此时将left.top放入右堆,然后right_n++,left_n == right_n
//(2)left_top <num,直接放入右堆,right_n++
public:
MedianFinder()
{
}
void addNum(int num)
{
if(left_n == right_n)
{
if(left_n == 0 || left_heap.top() >= num)
{
left_heap.push(num);
left_n++;
}
else if(left_heap.top() < num)
{
right_heap.push(num);
int top = right_heap.top(); right_heap.pop();
left_heap.push(top);
left_n++;
}
}
else if(left_n == right_n + 1)
{
if(left_heap.top() >= num)
{
left_heap.push(num);
int top = left_heap.top(); left_heap.pop();
right_heap.push(top);
right_n++;
}
else if(left_heap.top() < num)
{
right_heap.push(num);
right_n++;
}
}
}
double findMedian()
{
if(left_n == right_n)
return (left_heap.top() + right_heap.top()) / 2.0;
return left_heap.top();
}
};
/**
* Your MedianFinder object will be instantiated and called as such:
* MedianFinder* obj = new MedianFinder();
* obj->addNum(num);
* double param_2 = obj->findMedian();
*/