vector<int>  getLeastNumber(vector<int>&  arr,int k){
vector<int> vec(k,);
if(==k)
return vec;
priority_queue<int> q;
for(int i = ;i < k;i++){
q.push(arr[i]);
}
for(int i = k;i <(int)arr.size();i++){
if(q.top()>arr[i]){
q.pop();
q.push(arr[i]);
}
}
for(int i = ;i < k;i++){
vec[i] = q.top();
q.pop();
}
return vec;
}

我们用一个大根堆实时维护数组的前 kk 小值。首先将前 kk 个数插入大根堆中,随后从第 k+1k+1 个数开始遍历,如果当前遍历到的数比大根堆的堆顶的数要小,就把堆顶的数弹出,再插入当前遍历到的数。最后将大根堆里的数存入数组返回即可。在下面的代码中,由于 C++ 语言中的堆(即优先队列)为大根堆,我们可以这么做。

作者:LeetCode-Solution
链接:https://leetcode-cn.com/problems/zui-xiao-de-kge-shu-lcof/solution/zui-xiao-de-kge-shu-by-leetcode-solution/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

//定义方式

priority-queue<int> p;//最大值优先,大顶堆的简写方式
priority-queue<int,vector<int>,greater<int> > q1;//最小值优先,小顶堆
priority-queue< int,vector<int>,less<int> > q2;//最大值优先,大顶堆

//第一个参数--数据类型,第二个参数--容器类型,第三个参数--比较函数
}

{
//结构体的优先级比较方式

struct node{
string name;
int price;
friend bool operator < (node a,node b)
{
return a.price < b.price;
}
//相当于less,这是大顶堆,反之是小顶堆,最大值优先
}stu;

priority-queue<node> q;

//常用操作
q.push(x);
q.pop();
q.top();
q.size();
q.empty();
}

{
//举例
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#include <vector>

using namespace std;

struct node
{
friend bool operator< (node n1,node n2)
{
return (n1.priority < n2.priority);
}

int priority;
int value;
};

int main()
{
const int len = 5;
int i;
int a[len] = {3,5,9,6,2};

//从大到小输出
priority-queue<int> q1;
for (int i = 0;i < len;i++)
q1.push[a[i]);

for (int i = 0;i < len;i++)
{
cout << q1.top() << " ";
q1.pop();
}
cout << endl;

//从小到大输出
priority-queue<int,vector<int>,greater<int> > q2;
for (int i = 0;i < len;i++)
q2.push(a[i]);

for (int i = 0;i < len;i++)
{
cout << q2.top() << " ";
q2.pop();
}
cout << endl;

//按优先级输出
priority-queue<node> qn;
node b[len];
b[0].priority = 6;b[0].value = 1;
b[1].priority = 9;b[1].value = 5;
b[2].priority = 2;b[2].value = 3;
b[3].priority = 8;b[3].value = 2;
b[4].priority = 1;b[4].value = 4;

for (int i = 0;i < len;i++)
qn.push(b[i]);

cout << "优先级" << '\t' << "值" << endl;
for (int i = 0;i < len;i++)
{
cout << qn.top().priority << '\t' << qn.top().value << endl;
qn.pop();
}
cout << endl;

}
}

大顶堆与小顶堆应用---寻找前k小数的更多相关文章

  1. 堆排序(大顶堆、小顶堆)----C语言

    堆排序 之前的随笔写了栈(顺序栈.链式栈).队列(循环队列.链式队列).链表.二叉树,这次随笔来写堆 1.什么是堆? 堆是一种非线性结构,(本篇随笔主要分析堆的数组实现)可以把堆看作一个数组,也可以被 ...

  2. heap c++ 操作 大顶堆、小顶堆

    在C++中,虽然堆不像 vector, set 之类的有已经实现的数据结构,但是在 algorithm.h 中实现了一些相关的模板函数.下面是一些示例应用 http://www.cplusplus.c ...

  3. 《排序算法》——堆排序(大顶堆,小顶堆,Java)

    十大算法之堆排序: 堆的定义例如以下: n个元素的序列{k0,k1,...,ki,-,k(n-1)}当且仅当满足下关系时,称之为堆. " ki<=k2i,ki<=k2i+1;或k ...

  4. 378. Kth Smallest Element in a Sorted Matrix(大顶堆、小顶堆)

    Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth ...

  5. wikioi 2573 大顶堆与小顶堆并用

    题目描写叙述 Description 我们使用黑匣子的一个简单模型.它能存放一个整数序列和一个特别的变量i.在初始时刻.黑匣子为空且i等于0. 这个黑匣子能运行一系列的命令.有两类命令: ADD(x) ...

  6. 数据结构:堆排序 (python版) 小顶堆实现从大到小排序 | 大顶堆实现从小到大排序

    #!/usr/bin/env python # -*- coding:utf-8 -*- ''' Author: Minion-Xu 小堆序实现从大到小排序,大堆序实现从小到大排序 重点的地方:小堆序 ...

  7. Python使用heapq实现小顶堆(TopK大)、大顶堆(BtmK小)

    Python使用heapq实现小顶堆(TopK大).大顶堆(BtmK小) | 四号程序员 Python使用heapq实现小顶堆(TopK大).大顶堆(BtmK小) 4 Replies 需1求:给出N长 ...

  8. 剑指offer:数据流中的中位数(小顶堆+大顶堆)

    1. 题目描述 /** 如何得到一个数据流中的中位数? 如果从数据流中读出奇数个数值,那么中位数就是所有数值排序之后位于中间的数值. 如果从数据流中读出偶数个数值,那么中位数就是所有数值排序之后中间两 ...

  9. HDU 4006The kth great number(K大数 +小顶堆)

    The kth great number Time Limit:1000MS     Memory Limit:65768KB     64bit IO Format:%I64d & %I64 ...

随机推荐

  1. angular foreach的使用

    var myAppModule = angular.module('myApp', []); var values = { name : 'misko', gender : 'male', " ...

  2. 仅需60秒,使用k3sup快速部署高可用K3s集群

    作者简介 Dmitriy Akulov,连续创业者,16岁时搭建了开源CDN公共服务jsDelivr的v1版本.目前是边缘托管平台appfleet创始人. 原文链接: https://ma.ttias ...

  3. 深入理解JS中的对象(三):class 的工作原理

    目录 序言 class 是一个特殊的函数 class 的工作原理 class 继承的原型链关系 参考 1.序言 ECMAScript 2015(ES6) 中引入的 JavaScript 类实质上是 J ...

  4. Arthas 使用(二) —— 应用场景

    1. ognl获取bean SpringContextUtil,通常代码中会有类似这样的工具类用来获取 bean 实例 @Component public class SpringContextUti ...

  5. npm WARN enoent ENOENT: no such file or directory

    https://github.com/visionmedia/debug/issues/261

  6. NPOI导入excel为datatable (xls xlsx xlsm)

    使用NPOI导入导出Excel(xls/xlsx)数据到DataTable中 http://www.cnblogs.com/songrun/p/3547738.html NPOI 2.0教程 – 自动 ...

  7. SpringBoot代码生成器,从此不用手撸代码

    前言 通常在开始开发项目的时候,首先会建立好数据库相关表,然后根据表结构生成 Controller.Service.DAO.Model以及一些前端页面. 如果开发前没有强制的约束,而每个程序员都有自己 ...

  8. 树点分治入门题poj1741

    Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 24253   Accepted: 8060 Description ...

  9. [PHP]用PHP自己写一个基于zoomeye的api(偷懒必备quq)

    0x01 起因 因为手速慢,漏洞刷不过别人,一个个手补确实慢,所以想自己写一个api,一键抓取zoomeye的20页,然后就可以打批量了 ovo(真是太妙了!) 0x02 动工       1.抓包做 ...

  10. Docker安装常见的应用与将本地镜像推送到阿里云

    一.Docker安装常用的应用 1,docker安装mysql #拉取镜像mysql5.7 docker pull mysql:5.7 #启动容器(绑定对应的配置文件和日志,默认密码为123456) ...