【LeetCode】Heap
[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的更多相关文章
- 【LeetCode】373. Find K Pairs with Smallest Sums 解题报告(Python)
[LeetCode]373. Find K Pairs with Smallest Sums 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/p ...
- 【LeetCode】692. Top K Frequent Words 解题报告(Python)
[LeetCode]692. Top K Frequent Words 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/top ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
- 【刷题】【LeetCode】007-整数反转-easy
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...
- 【刷题】【LeetCode】000-十大经典排序算法
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法
- 【leetcode】893. Groups of Special-Equivalent Strings
Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...
随机推荐
- 力扣——Copy List with Random Pointer(复制带随机指针的链表) python实现
题目描述: 中文: 给定一个链表,每个节点包含一个额外增加的随机指针,该指针可以指向链表中的任何节点或空节点. 要求返回这个链表的深拷贝. 示例: 输入:{"$id":" ...
- js 判断表单是否为空和是否是有效数字
判断是否为空和是否是有效数字 <s:form name='form' onsubmit="return myCheck()" method="post" ...
- MySQL数据类型DECIMAL用法详解
MySQL DECIMAL数据类型用于在数据库中存储精确的数值.我们经常将DECIMAL数据类型用于保留准确精确度的列,例如会计系统中的货币数据. 要定义数据类型为DECIMAL的列,请使用以下语法: ...
- proxy-target-class="false"与proxy-target-class="true"区别
原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11484063.html <aop:aspectj-autoproxy proxy-target- ...
- Windows服务 --- SqlDependency的使用
1 启用当前数据库的 SQL Server Service Broker a 检查Service Broker 是否开启 SELECT is_broker_enabled FROM sys.d ...
- c# 泛型的抗变和协变
namespace test { // 泛型的协变,T 只能作为返回的参数 public interface Class1<out T> { T Get(); int Count { ge ...
- centos下安装java jdk1.8
---恢复内容开始--- mysql密码修改了,发现还没装jdk,那就一起记录下来吧.虽然网上好多,但自己想查更方便了. 查看有没有装jdk #java -version显示下面信息,不是oracle ...
- SCP-bzoj-1085
项目编号:bzoj-1085 项目等级:Safe 项目描述: 戳这里 特殊收容措施: A*(上下界剪枝). 答案上界:15. 答案下界:当前步数+当前状态剩余步数估价. 这里我们简单地设计估价函数为当 ...
- Echarts 甘特图教程
Echarts甘特图教程 echarts官网实例: https://gallery.echartsjs.com/editor.html?c=xEYpsVs30s 效果: 代码: <html& ...
- (转)MAVEN的安装和Myeclipse+maven的配置和使用
转:https://blog.csdn.net/shikongshengzhu/article/details/51779159 MAVEN的安装和Myeclipse+maven的配置和使用 步骤如下 ...