[215] Kth Largest Element in an Array [Medium]

给个无序数组,返回第K大的数字。

方法1. 直接使用优先队列 priority_queue

 class Solution {
public:
int findKthLargest(vector<int>& nums, int k) {
priority_queue<int> pq; // priority queue 实现一个大根堆
for (int i = ; i < nums.size(); ++i) {
pq.emplace(nums[i]);
}
int i = ;
while(i++ < k) {
pq.pop();
}
return pq.top();
}
};

方法2. 自己实现堆排序 (周末补上)

[373] Find K Pairs with Smallest Sums [Medium]

给两个有序数组,返回前k个和最小的组合对。

思路:用一个大根堆,放k个组合对,如果新来的小于当前的最大值,就把最大的踢出去。

 /* author: wyzhang
* 2017/03/25
* 最大堆
*/
class Solution {
public:
struct cmp {
bool operator()(pair<int, int> a, pair<int, int> b) {
return a.first + a.second < b.first + b.second;
}
};
vector<pair<int, int>> kSmallestPairs(vector<int>& nums1, vector<int>& nums2, int k) {
priority_queue<pair<int, int>, vector<pair<int, int>>, cmp> pq;
for (size_t i = ; i < nums1.size() && i < k; ++i) {
for (size_t j = ; j < nums2.size() && j < k; ++j) {
if (pq.size() < k) {
pq.push(make_pair(nums1[i], nums2[j]));
} else {
if (nums1[i] + nums2[j] < pq.top().first + pq.top().second) {
pq.pop();
pq.push(make_pair(nums1[i], nums2[j]));
}
}
}
}
int i = min((size_t)k, pq.size());
vector<pair<int, int>> ans(i);
while(!pq.empty()) {
ans[i-] = pq.top();
pq.pop();
i--;
}
return ans;
}
};

【LeetCode】Heap的更多相关文章

  1. 【LeetCode】373. Find K Pairs with Smallest Sums 解题报告(Python)

    [LeetCode]373. Find K Pairs with Smallest Sums 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/p ...

  2. 【LeetCode】692. Top K Frequent Words 解题报告(Python)

    [LeetCode]692. Top K Frequent Words 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/top ...

  3. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  4. 【Leetcode】Pascal&#39;s Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  5. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

  6. 27. Remove Element【leetcode】

    27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...

  7. 【刷题】【LeetCode】007-整数反转-easy

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...

  8. 【刷题】【LeetCode】000-十大经典排序算法

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法

  9. 【leetcode】893. Groups of Special-Equivalent Strings

    Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...

随机推荐

  1. Dev常用控件

    GridControl TreeView DEV GridControl小结.. https://blog.csdn.net/happy09li/article/details/7186829 Dev ...

  2. 【串线篇】SpringMvc视图解析

    一. 请求处理方法执行完成后,最终返回一个 ModelAndView 对象.对于那些返回 String,View 或 ModeMap 等类型的处理方法,Spring MVC 也会在内部将它们装配成一个 ...

  3. 那些长短不一的PCI-E插槽都有什么不一样?

    https://www.ednchina.com/news/20171121-PCI-E.html 时间:2017-11-21   目前PCI-E插槽已经成为了主板上的主力扩展插槽,除了显卡会用到P ...

  4. vue基础九

    1.使用组件 1.1注册 要注册一个全局组件,你可以使用 Vue.component(tagName, options). 例如: Vue.component('my-component', { // ...

  5. BN和正则化一起使用的后果

    就是因为 batch norm 过后, weight 影响没那么重了,所以 l2 weight decay 的效果就不明显了. 证明了L2正则化与归一化相结合时没有正则化效应.相反,正则化会影响权重的 ...

  6. es6 常用的语法

    1.es6 模块化 你import 和 export export default 为默认到处,而export能导出多个方法或变量. 2. es6——class与普通构造函数的区别 class的继承方 ...

  7. percona-toolkit工具包的安装和初步使用

    percona-toolkit工具包的安装和初步使用 原文地址:http://blog.csdn.net/yumushui/article/details/42919601 一.percona-too ...

  8. [CSP-S模拟测试]:简单的区间(分治)

    题目描述 给定一个长度为$n$的序列$a$以及常数$k$,序列从$1$开始编号.记$$f(l,t)=\sum \limits_{i=l}^ra_i-\max \limits_{i=l}^r\{a_i\ ...

  9. [NOIP模拟25]题解

    A.字符串 Catalan数不能再裸了 #include<cstdio> #include<iostream> #include<cstring> using na ...

  10. 【Windows、SVN】在Windows服务器下安装SVN,并在客户端能维护代码版本

    1.分别在客户端和服务器端安装软件 在网上搜索一下安装包的下载地址(这里暂不介绍) 得到2个安装文件 Server是装在服务器端的,另外一个装在客户端 2.安装SVN服务器端 基本一致下一步即可 特殊 ...