[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. numpy 数组中添加新元素

    import numpy as npnew_array = np.empty(shape=[0, 3]) # 3列n行for i in range(10): x = i+1 y = i+2 z = i ...

  2. @HttpEntity参数(怪异)

    1).在Controller中写 与@RequestBody请求体对应 @HttpEntity更强大,不光有请求体,还能获取请求头 @RequestMapping("/test02" ...

  3. jcrop+java 后台

    //jcrop 用法 lpf //页面引入 //<link rel="stylesheet" href="${basePath}/scripts/jcrop/jqu ...

  4. python3 实现简单ftp服务功能(服务端 For Linux)

    转载请注明出处! 功能介绍: 可执行的命令: lspwdcd put rm get mkdir 1.用户加密认证 2.允许多用户同时登陆 3.每个用户有自己的家目录,且只可以访问自己的家目录 4.运行 ...

  5. OC学习篇之---类目的概念和使用

    上一篇文章介绍了OC中的@class关键字的使用http://blog.csdn.net/jiangwei0910410003/article/details/41774747,这一篇我们介绍一下,O ...

  6. c#发送邮件功能

    protected void Page_Load(object sender, EventArgs e)    {        //先到qq邮箱设置中启用smtp服务        Random r ...

  7. 【Tomcat】使用Tomcat部署Spring Boot项目生成的jar包

    介绍 简单来说,Tomcat是一个免费的,用于Java Web应用以及其它Web应用的一个Web服务器.(简单地概括一下,可能有误) 下载与安装 本文章目标是把Spring Boot Web项目生成的 ...

  8. ajax图片上传,单个按钮实现选择图片和上传

    //图片原件上传功能 function gosubmit(file, key) { var formData = new FormData($("#inputForm")[0]); ...

  9. 100、TensorFlow实现FFM Field-awared FM模型

    ''' Created on 2017年11月15日 @author: weizhen ''' import tensorflow as tf import pandas as pd import n ...

  10. Nginx网络架构实战学习笔记(三):nginx gzip压缩提升网站速度、expires缓存提升网站负载、反向代理实现nginx+apache动静分离、nginx实现负载均衡

    文章目录 nginx gzip压缩提升网站速度 expires缓存提升网站负载 反向代理实现nginx+apache动静分离 nginx实现负载均衡 nginx gzip压缩提升网站速度 网页内容的压 ...