【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 ...
随机推荐
- shell内置命令getopts
- USB hub串口绑定
方式一 1.查看串口信息 udevadm info /dev/ttyUSB0 2.创建配置文件 sudo vi /etc/udev/rules.d/com_port.rules ACTION==&qu ...
- 【LeetCode】Array
[11] Container With Most Water [Medium] O(n^2)的暴力解法直接TLE. 正确的解法是Two Pointers. O(n)的复杂度.保持两个指针i,j:分别指 ...
- 错误描述:fatal error C1010: 在查找预编译头时遇到意外的文件结尾。是否忘记了向源中添加“#include "stdafx.h"”?(转)
错误分析: 此错误发生的原因是编译器在寻找预编译指示头文件(默认#include "stdafx.h")时,文件未预期结束.没有找到预编译指示信息的头文件"stdafx. ...
- 03.线程的通知notify与等待wait
wait().notify.notifyAll()方法 wait().notify().notifyAll()是三个定义在Object类里的方法,可以用来控制线程的状态. 这三个方法最终调用的都是jv ...
- redis相关笔记(二.集群配置及使用)
redis笔记一 redis笔记二 redis笔记三 1.配置:在原redis-sentinel文件夹中添加{8337,8338,8339,8340}文件夹,且复制原8333中的配置 在上述8333配 ...
- UVa 839 Not so Mobile (递归思想处理树)
Before being an ubiquous communications gadget, a mobilewas just a structure made of strings and wir ...
- hdu1166 敌兵布阵 (线段树单点更新)
Description C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了.A国在海岸线沿直线布置了N个工兵营 地,Derek和Tidy的任务就是要监视这 ...
- Jetson Nano 系列教程2:串口调试接口登录Jetson Nano
连接Jetson Nano可以有多种方法,这里我们一一介绍一下.开始本章节前,请先参考上一章,烧写好镜像 直接连接 所谓直接连接,就是将Jetson Nano当做主机,连接HDMI屏幕,连接键盘和鼠标 ...
- LeetCode题:旋转链表
原题: 给定一个链表,旋转链表,将链表每个节点向右移动 k 个位置,其中 k 是非负数. 示例 1: 输入: 1->2->3->4->5->NULL, k = 2输出: ...