[Algorithm] How to use Max Heap to maintain K smallest items

Let‘s say we are given an array:

[4,1,5,2,3,0,10]

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)

原文地址:https://www.cnblogs.com/Answer1215/p/10480327.html

时间: 2024-10-10 04:50:55

[Algorithm] How to use Max Heap to maintain K smallest items的相关文章

C++之Binary Heap/Max Heap

1 #include <iostream> 2 #include <time.h> 3 #include <random> 4 5 using namespace std; 6 7 //Binary Heap; Max Heap; 8 9 class BinaryHeap 10 { 11 public: 12 BinaryHeap(); 13 BinaryHeap(int capacity); 14 ~BinaryHeap(); 15 16 int insert(int

Google 面试题:Java实现用最大堆和最小堆查找中位数 Find median with min heap and max heap in Java

Google面试题 股市上一个股票的价格从开市开始是不停的变化的,需要开发一个系统,给定一个股票,它能实时显示从开市到当前时间的这个股票的价格的中位数(中值). SOLUTION 1: 1.维持两个heap,一个是最小堆,一个是最大堆. 2.一直使maxHeap的size大于minHeap. 3. 当两边size相同时,比较新插入的value,如果它大于minHeap的最大值,把它插入到minHeap.并且把minHeap的最小值移动到maxHeap. ...具体看代码 1 /*********

[Algorithm] Median Maintenance algorithm implementation using TypeScript / JavaScript

The median maintenance problem is a common programming challenge presented in software engineering job interviews. In this lesson we cover an example of how this problem might be presented and what your chain of thought should be to tackle this probl

Kth Smallest Element in Unsorted Array

(referrence: GeeksforGeeks, Kth Largest Element in Array) This is a common algorithm problem appearing in interviews. There are four basic solutions. Solution 1 -- Sort First A Simple Solution is to sort the given array using a O(n log n) sorting alg

973. K Closest Points to Origin - Medium

We have a list of points on the plane.  Find the K closest points to the origin (0, 0). (Here, the distance between two points on a plane is the Euclidean distance.) You may return the answer in any order.  The answer is guaranteed to be unique (exce

STL heap usage

简介 heap有查找时间复杂度O(1),查找.插入.删除时间复杂度为O(logN)的特性,STL中heap相关的操作如下: make_heap() push_heap() pop_heap() sort_heap() reverse() 本次着重介绍make_heap() ,根据其创出的堆有大小堆之分. 其函数原型如下: default (1) template <class RandomAccessIterator> void make_heap (RandomAccessIterator

左偏树(Leftist Heap/Tree)简介及代码

左偏树是一种常用的优先队列(堆)结构.与二叉堆相比,左偏树可以高效的实现两个堆的合并操作. 左偏树实现方便,编程复杂度低,而且有着不俗的效率表现. 它的一个常见应用就是与并查集结合使用.利用并查集确定两个元素是否在同一集合,利用左偏树确定某个集合中优先级最高的元素. 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 5 template <class T> 6 struct H

C++11新特性应用--介绍几个新增的便利算法(stl中的heap使用,最大堆)

有的时候为了维护数据,我们使用stl的堆去维护一序列. 首先您要弄清楚堆和栈的区别,即heap和stack stl中的堆默认是最大堆. 先介绍 push_heap,pop_heap,make_heap,sort_heap这四个算法,这四个不是C++11新增加的内容. 首先是如何产生一个最大推: make_heap 原型: template <class RandomAccessIterator> void make_heap (RandomAccessIterator first, Rando

c++中STL之heap, priority_queue使用

一.heap heap并不属于STL容器组件,它分为 max heap 和min heap,在缺省情况下,max-heap是优先队列(priority queue)的底层实现机制.而这个实现机制中的max-heap实际上是以一个vector表现的完全二叉树(complete binary tree).STL在<algorithm.h>中实现了对存储在vector/deque 中的元素进行堆操作的函数,包括make_heap, pop_heap, push_heap, sort_heap,对不愿