[Algorithm] How to use Max Heap to maintain K smallest items
Let's say we are given an array:
[,,,,,,]
We want to get K = 3 smallest items from the array and using Max heap data structure.
So this is how to think about it.
1. We take first K items put it into Max Heap:
5
/ \
4 1
2. Then we move forward to next element '2' in the array, we check
- Whether 2 is smaller than current max item in heap, which is '5', if yes, then replace 5 with 2
- Then re-arrange the heap
4
/ \
2 1
3. Repeat the process for items [3,0]
3 2
/ \ / \
2 1 0 1
4. When the item is '10' which is larger than current max '2', we just ingore it.
5. In the end, we got K smallest items, we just need to print it out. (K smallest items are not ncessary in order)
[Algorithm] How to use Max Heap to maintain K smallest items的更多相关文章
- C++之Binary Heap/Max Heap
#include <iostream> #include <time.h> #include <random> using namespace std; //Bin ...
- Google 面试题:Java实现用最大堆和最小堆查找中位数 Find median with min heap and max heap in Java
Google面试题 股市上一个股票的价格从开市开始是不停的变化的,需要开发一个系统,给定一个股票,它能实时显示从开市到当前时间的这个股票的价格的中位数(中值). SOLUTION 1: 1.维持两个h ...
- [Algorithm] Median Maintenance algorithm implementation using TypeScript / JavaScript
The median maintenance problem is a common programming challenge presented in software engineering j ...
- Kth Smallest Element in Unsorted Array
(referrence: GeeksforGeeks, Kth Largest Element in Array) This is a common algorithm problem appeari ...
- 面试准备 - 最大堆的Csharp实现
面试中最常见的问题之一...在N个数中间寻找前K大个元素 最常见的解法就是最大堆 时间复杂度O(N*log(K)) 空间复杂度O(k) 实现了一个最简单的最大堆,每次有元素进来都和堆顶元素比较一下,如 ...
- [Algorithm] Heap data structure and heap sort algorithm
Source, git Heap is a data structure that can fundamentally change the performance of fairly common ...
- STL heap usage
简介 heap有查找时间复杂度O(1),查找.插入.删除时间复杂度为O(logN)的特性,STL中heap相关的操作如下: make_heap() push_heap() pop_heap() sor ...
- binary heap
In computer science, a heap is a specialized tree-based data structure that satisfies the heap prope ...
- Heap Sort - recursion
Heap Sort Build a max heap using exsiting array, which is called Heapify Swap root with the last el ...
随机推荐
- uoj407 【IOI2018】狼人
link 题意: 给一张n个点m条边的无向图,有q个询问,每次询问给出s,t,l,r,问你能否从s走到t,并且初始为人形,结束时必须为狼形,你是人形的时候必须避开$[1,l)$的节点,狼形的时候必须避 ...
- hdu 5316 Magician(2015多校第三场第1题)线段树单点更新+区间合并
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5316 题意:给你n个点,m个操作,每次操作有3个整数t,a,b,t表示操作类型,当t=1时讲a点的值改 ...
- hdu 4745 区间dp
题意:求一个环的最长回文序列,是序列不是串 链接:点我 起点是可以任意的, 所以只要求出每个区间的最长回文序列之后取max(dp[1][i]+dp[i+1][n]),即可得最终答案 本来是想扩展两倍的 ...
- 使用Layui和Vue实现分页
原理就是利用Layui的分页组件和Vue组件的模板渲染功能. 我下面直接贴代码,比较直观. index.html <!DOCTYPE html> <html> <head ...
- IIS Express并发数设置
今天将之前的一个瓦片图的服务迁移到了asp.net core试了一下,使用的时候感觉客户端刷新时有些慢,估计是并发连接数限制的原因. 由于这是一个开发中的版本,是用IIS Express部署的,IIS ...
- Content portal for Pocketables Tasker articles
http://www.pocketables.com/2013/03/overview-of-pocketables-tasker-articles.html I write a lot about ...
- Clever Little Box 电缆组件 USB A 插座 至 USB B 插头
http://china.rs-online.com/web/p/usb-cable-assemblies/7244156/ 产品详细信息 USB3.0适配器 superspeed USB将提供10x ...
- 支付宝支付-常用支付API详解(查询、退款、提现等)-转
所有的接口支持沙盒环境的测试 1.前言 前面几篇文件详细介绍了 支付宝提现.扫码支付.条码支付.Wap支付.App支付 支付宝支付-提现到个人支付宝 支付宝支付-扫码支付 支付宝支付-刷卡支付(条码支 ...
- mysql 审计插件编写
http://bbs.chinaunix.net/forum.php?mod=viewthread&tid=1864367&page=1#pid13527550 http://blog ...
- 在Windows Server 2008 R2中使用web方式修改域用户账户密码
在Windows的domain环境下,加域的客户端修改账户密码是一件很easy的事情:即使没有加域的客户端如果组织中,使用Exchange邮件系统,借助Exchange的owa也可以轻松修改账户密码. ...