【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 ...
随机推荐
- Mysql 事务相关
MySQL介绍 什么是MySQL? MySQL 是一种关系型数据库,在Java企业级开发中非常常用,因为 MySQL 是开源免费的,并且方便扩展.阿里巴巴数据库系统也大量用到了 MySQL,因此它 ...
- Tornado中异步框架的使用
tornado的同步框架与其他web框架相同都是处理先来的请求,如果先来的请求阻塞,那么后面的请求也会处理不了.一直处于等待过程中.但是请求一旦得到响应,那么: 请求发送过来后,将需要的本站资源直接返 ...
- testNG之参数化测试
@Parameters 测试的时候,测试的数据可以放在testng.xml文件中,被测试的方法通过@Parameters注解传递到测试方法中 parameterizedTest.java import ...
- kubernetes使用kubeadm升级集群
升级前准本 官网: https://kubernetes.io/docs/reference/setup-tools/kubeadm/kubeadm-upgrade/查看可升级的组件 [root@h ...
- vue 学习中遇到的一些问题
Vue中data有return返回函数问题: 在简单的vue实例中会看到Vue实例中data属性展示方式如下: let app= newVue({ el:"#app", data: ...
- Android学习--写一个发送短信的apk,注意布局文件的处理过程!!!
刚开始写Android程序如图发现使用了findViewById方法之后输出的话居然是null(空指针错误),也就是说这个方法没有成功.网上说这样写是在activity_main .xml去找这个ID ...
- LR之-参数化
1.改变参数化主要在于select next now和update value on这个二个选项 sequential:顺序取值 random:随机取值 unique:唯一取值 same line a ...
- windows系统查看端口占用
netstat -ano #列出所用端口使用情况 netstat -aon|findstr "端口号" #查询指定端口 tasklist|findstr "PID&qu ...
- java BufferSegment
package org.rx.util; import java.util.function.Consumer; import static org.rx.core.Contract.require; ...
- HTML5: HTML(5) 代码规范
ylbtech-HTML5: HTML(5) 代码规范 1.返回顶部 1. HTML(5) 代码规范 HTML 代码约定 很多 Web 开发人员对 HTML 的代码规范知之甚少. 在2000年至201 ...