[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 ...
随机推荐
- BZOJ 2843: 极地旅行社 lct splay
http://www.lydsy.com/JudgeOnline/problem.php?id=2843 https://blog.csdn.net/clove_unique/article/deta ...
- poj 3463 次短路
题意:给定一个有向图,问从起点到终点,最短路+比最短路距离长1的路的个数. 当年数据结构课程设计用A*做过,现在忘光了,2333 #include<stdio.h> #include< ...
- js的继承实现方式
1. 使用call或者apply来实现js对象继承 function Animal(age){ this.age = age; this.say = function(){ console.log(' ...
- 【对比分析二】Web Storage和cookie的区别
1) 存储空间不同. a) Web Storage能提供5MB的存储空间(不同浏览器的提供的空间不同).Cookie仅4KB. b) Web Storage每个域(包括子域)有独立的存储空间,各 ...
- Codeforces Round #350 (Div. 2) C. Cinema 水题
C. Cinema 题目连接: http://www.codeforces.com/contest/670/problem/C Description Moscow is hosting a majo ...
- Window 下安装
Window 下安装 下载地址:https://github.com/MSOpenTech/redis/releases Redis 支持 32 位和 64 位.这个需要根据你系统平台的实际情况选择, ...
- IO流-批量修改文件名称案例
/* * 源文件名: 桌面-我们今天学习IO流了哈哈哈哈-001.jpg * 修改后文件名: 桌面-000x.jpg */public class File_listFiles_upda ...
- BZOJ 1269: [AHOI2006]文本编辑器editor (splay tree)
1269: [AHOI2006]文本编辑器editor Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1213 Solved: 454[Submit ...
- Java的线程和多线程教程
Java线程(Java Thread)是执行某些任务的一种轻量级进程.Java中的Thread类提供了多线程(multi-threading)功能,应用程序能够创建多个线程并同一时候执行. 在一个应用 ...
- Easy WordPress Updates: Store FTP Info in wp-config.php
Saw an interesting blog post on Twitter today about storing WordPress FTP information in wp-config.p ...