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. 在终端输入npm run serve时出现npm ERR! code ELIFECYCLE npm ERR! errno 1 npm ERR! test_vue_0613@1.0.0 dev: 错误的解决方法

    在vscode终端使用命令 npm run serve 的时候报错 错误原因在于由于文件 node_modules 太大,在项目上传时有些人会删掉 导致我们下载的项目中缺少这个文件 在尝试把自己项目的 ...

  2. spring源码解析之前置知识点

    本文是作者原创,版权归作者所有.若要转载,请注明出处. 最近在看spring源码,但是spring的体系太庞大了,在这里记录一下阅读源码中遇到知识点 @PostConstruct 被注解的方法,在对象 ...

  3. R的安装以及包安装

        今天看论文,需要用到R语言的库,于是又折腾了半天..     其实并没有什么太大的问题,只是在第三方包的下载方面还有python中使用R方面遇到了问题: 第三方包的导入      其实在网上有 ...

  4. git :error: bad signature fatal: index file corrupt

    删除.git/index文件再执行: git reset 不行的话就执行: git read-tree  --empty https://stackoverflow.com/questions/213 ...

  5. P5410 【模板】扩展 KMP

    P5410 [模板]扩展 KMP #include<bits/stdc++.h> using namespace std; ; int q, nxt[maxn], extend[maxn] ...

  6. flask之response

    import os from flask import Flask,render_template,redirect,jsonify,send_file app=Flask(__name__) #开发 ...

  7. Intel FPGA Clock Region概念以及用法

    目录 Intel FPGA 的Clock Region概念 Intel 不同系列FPGA 的Clock Region 1. Clock Region Assignments in Intel Stra ...

  8. 二、$CSS部分

    1.css sprite是什么,有什么优缺点 概念:将多个小图片拼接到一个图片中.通过background-position和元素尺寸调节需要显示的背景图案. 优点: 减少HTTP请求数,极大地提高页 ...

  9. layui 让弹窗始终居中于屏幕

    前话:今天用 layer.confirm()  弹窗的时候,滚动到页面尾部再弹窗时,发现弹窗还显示在上面,要滚动会上面才能看到. 度娘找了一个获取滚动条位置的方法: function ScollPos ...

  10. 【学习】Python os模块常用方法 记录

    记录一些工作中常用到的用法 os.walk() 模块os中的walk()函数可以遍历文件夹下所有的文件. os.walk(top, topdown=Ture, onerror=None, follow ...