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的更多相关文章

  1. C++之Binary Heap/Max Heap

    #include <iostream> #include <time.h> #include <random> using namespace std; //Bin ...

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

    Google面试题 股市上一个股票的价格从开市开始是不停的变化的,需要开发一个系统,给定一个股票,它能实时显示从开市到当前时间的这个股票的价格的中位数(中值). SOLUTION 1: 1.维持两个h ...

  3. [Algorithm] Median Maintenance algorithm implementation using TypeScript / JavaScript

    The median maintenance problem is a common programming challenge presented in software engineering j ...

  4. Kth Smallest Element in Unsorted Array

    (referrence: GeeksforGeeks, Kth Largest Element in Array) This is a common algorithm problem appeari ...

  5. 面试准备 - 最大堆的Csharp实现

    面试中最常见的问题之一...在N个数中间寻找前K大个元素 最常见的解法就是最大堆 时间复杂度O(N*log(K)) 空间复杂度O(k) 实现了一个最简单的最大堆,每次有元素进来都和堆顶元素比较一下,如 ...

  6. [Algorithm] Heap data structure and heap sort algorithm

    Source, git Heap is a data structure that can fundamentally change the performance of fairly common ...

  7. STL heap usage

    简介 heap有查找时间复杂度O(1),查找.插入.删除时间复杂度为O(logN)的特性,STL中heap相关的操作如下: make_heap() push_heap() pop_heap() sor ...

  8. binary heap

    In computer science, a heap is a specialized tree-based data structure that satisfies the heap prope ...

  9. Heap Sort - recursion

    Heap Sort  Build a max heap using exsiting array, which is called Heapify Swap root with the last el ...

随机推荐

  1. android handler messageQueue,looper

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha 处理器获取 当前线程中的 循环器对象, 循环器 从 消息队列中 取出 消息, 给 处理器 ...

  2. python开发_json_一种轻量级的数据交换格式

    以下是我做的对于python中json模块的demo 运行效果: Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.16 ...

  3. spring---transaction(1)---源代码分析(事务的拦截器TransactionInterceptor)

    写在前面: 先了解一下spring的事务.分为分明式事务管理和注解式事务管理,对于前期的事务,spring会通过扫描拦截对于事务的方法进行增强(以后讲解). 若果目标方法存在事务,spring产出的b ...

  4. org.springframework.orm.hibernate3.LocalSessionFactoryBean

    Spring整合hibernate在配置sessionFactory时, 启动总是报出javax.transaction.TransactionManager找不到. 原因是:缺少jar包,jta-1 ...

  5. Linux终端(Xshell等)的编码设置

    先简单说说一下遇到的情况吧: 在用类似Xshell的工具连接远端Linux时,运行命令ls时,如果有中文,就会显示有乱码: 网上查资料时,也把相应的连接属性修改为utf-8了(见下图),但是还是不行: ...

  6. 【Eclipse提高开发速度-插件篇】安装多语言Propertys编辑工具

    安装的的时候能够通过 Help>Eclipse Marketplace的 搜索方式安装 或者 Help>Install New Software Name:Propertys URL:ht ...

  7. redhat server 5.3内核升极2.6.18 升级到 3.5 装systemtap 原创

    1. 在 LINUX 3.5源代码目录下执行  yum install ncurses-devel     make menuconfig 2  打开内核跟踪事件,用于SYSTEMTAP跟踪 kern ...

  8. Docker 1.3 公布

    Docker 1.3 公布 Docker 1.3 已经正式公布.新的特性包含镜像签名.进程注入.新的创建和执行容器命令.安全选项和 Mac OS 上进行文件夹共享.特别是针对安全方面的改进,成为本地公 ...

  9. MVC中使用AngularJS-01,基本

    Angularjs是一个前端的Javascript MVC 库和框架,使前端得到更好的设计.维护和测试.它的核心特性有:MVC.双向数据绑定.指令和语义化标签.模块化工具.依赖注入.HTML模板,以及 ...

  10. ajax访问遇到Session失效问题

    最近由于一个项目,模块切换为ajax请求数据,当Session失效后,ajax请 求后没有返回值,只有响应的html:<html><script type='text/javascr ...