[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 ...
随机推荐
- Java重写、重载与覆盖
Java继承.重载与重写 一.继承(单继承) 1.利用extends关键字一个方法继承另一个方法,而且只能直接继承一个类. 2.当Sub类和Base类在同一个包时,Sub类继承Base类中的publi ...
- BZOJ 1449: [JSOI2009]球队收益 最小费用最大流 网络流
https://www.lydsy.com/JudgeOnline/problem.php?id=1449 给每条路加上一个权值,每条路的费用是这条路的流量*权值,求最大流的最小费用. 每次spfa记 ...
- BZOJ 4003: [JLOI2015]城池攻占 左偏树 可并堆
https://www.lydsy.com/JudgeOnline/problem.php?id=4003 感觉就是……普通的堆啊(暴论),因为这个堆是通过递归往右堆里加一个新堆或者新节点的,所以要始 ...
- Codedforces 1076G Array Game 线段树
题意 现在cf上看题意真nm麻烦,有道网页翻译和谷歌翻译鬼畜的一匹 两个人在玩一个游戏. 有一个有\(n\)个数序列\(B\),一开始有一个棋子在\(B\)的第一个位置. 双方轮流操作,第一次操作前将 ...
- python配置文件操作——configparser模块
# -*- coding: utf-8 -*- ''' Version : Python27 Author : Spring God Date : 2012-4-26 Info : 配置文件ini所在 ...
- Python编程练习题学习汇总
实例一:数学计算 简述:这里有四个数字,分别是:1.2.3.4提问:能组成多少个互不相同且无重复数字的三位数?各是多少? Python解题思路分析:可填在百位.十位.个位的数字都是1.2.3.4.组成 ...
- 读书笔记_Effective_C++_条款三十九:明智而审慎地使用private继承
private继承的意义在于“be implemented in turns of”,这个与上一条款中说的复合模型的第二层含义是相同的,这也意味着通常我们可以在这两种设计方法之间转换,但书上还是更提倡 ...
- oracle的dump理解
http://blog.csdn.net/notbaron/article/details/51228444 http://blog.csdn.net/lqx0405/article/details/ ...
- BitmapFactory.Options.inSampleSize 的使用方法
BitmapFactory.decodeFile(imageFile); 用BitmapFactory解码一张图片时.有时会遇到该错误. 这往往是因为图片过大造成的. 要想正常使用,则须要分配更少的内 ...
- Spring 反射注入+全注解注入
Spring IoC容器会先把所有的Bean都进行实例化,不管是要用到的火鼠用不到的,如果你想暂时不进行Bean的实例化,要用到属性lazy-init="true". Spring ...