Given an integer array, heapify it into a min-heap array.
For a heap array A, A[0] is the root of heap, and for each A[i], A[i * 2 + 1] is the left child of A[i] and A[i * 2 + 2] is the right child of A[i].
Example
Given [3,2,1,4,5], return [1,2,3,4,5] or any legal heap array. Challenge
O(n) time complexity Clarification
What is heap? Heap is a data structure, which usually have three methods: push, pop and top. where "push" add a new element the heap, "pop" delete the minimum/maximum element in the heap, "top" return the minimum/maximum element. What is heapify?
Convert an unordered integer array into a heap array. If it is min-heap, for each element A[i], we will get A[i * 2 + 1] >= A[i] and A[i * 2 + 2] >= A[i]. What if there is a lot of solutions?
Return any of them.

Heap的介绍1介绍2,要注意complete tree和full tree的区别, Heap是complete tree;Heap里面 i 的 children分别是 i*2+1 和 i*2+2,i 的 parent是      (i-1)/2

Heapify的基本思路就是:Given an array of N values, a heap containing those values can be built by simply “sifting” each internal node down to its proper location:

1. start with the last internal node

2. swap the current internal node with its smaller child, if necessary

3. then follow the swapped node down

4. continue until all internal nodes are done

 public class Solution {
/**
* @param A: Given an integer array
* @return: void
*/
public void heapify(int[] A) {
int start = A.length/2;
for (int i=start;i>=0;i--)
shiftDown(i, A);
} private void shiftDown(int ind, int[] A){
int size = A.length;
int left = ind*2+1;
int right = ind*2+2;
while (left<size || right<size){
int leftVal = (left<size) ? A[left] : Integer.MAX_VALUE;
int rightVal = (right<size) ? A[right] : Integer.MAX_VALUE;
int next = (leftVal<=rightVal) ? left : right;
if (A[ind]<A[next]) break;
else {
swap(A, ind,next);
ind = next;
left = ind*2+1;
right = ind*2+2;
}
}
} private void swap(int[] A, int x, int y){
int temp = A[x];
A[x] = A[y];
A[y] = temp;
}
}

注意第7行,start之所以从A.length/2开始,是因为要从Internal node开始,除开最后一行。其实可以写成start = (A.length - 1 - 1) / 2, 求最后一个index的parent index的基本做法。

17-18行的技巧,不存在就补齐一个很大的数,因为反正最终是求小的,这样省了很多行分情况讨论

下面给出Heap的 Summary, 转来的:implemented a Heap class that can specify min heap or max heap with insert, delete root and build heap functions.

Time Complexity分析:Binary Heap

Java PriorityQueue (Java Doc) time complexity for 1 operation

O(log n) time for the enqueing and dequeing methods (offer, poll, remove() and add). Note that this remove() is inherited, it's not remove(object). This retrieves and removes the head of this queue.

O(n) for the remove(Object) and contains(Object) methods

O(1) for the retrieval methods (peek, element, and size)

The insertion/poll of n elements should be O(n log n)

Build本来应该O(NlogN), 但是如果用巧妙办法:The optimal method starts by arbitrarily putting the elements on a binary tree, respecting the shape property (the tree could be represented by an array, see below). Then starting from the lowest level and moving upwards, shift the root of each subtree downward as in the deletion algorithm until the heap property is restored. 时间复杂度是 O(N)., 参看上面链接里面build a Heap部分证明

These time complexities seem all worst case (wiki), except for .add(). You are right to question the bounds as the Java Doc also states to the extension of this unbound structure:

The details of the growth policy are not specified

As they state in the Doc as well, the PriorityQueue is based on an array with a specific initial capacity. I would assume that the growth will cost O(n) time, which then would also be the worst case time complexity for .add().

To get a guaranteed O(n log n) time for adding n elements you may state the size of your n elements to omit extension of the container: PriorityQueue(int initialCapacity) 

Priority Queue work with Map.Entry

some syntax: everytime you change the Map.Entry, you should take it out and put it into PQ again in order for it to be sorted.

If you just change the value of the undelying Map.Entry, PQ won't sort by itself. Example: https://www.cnblogs.com/EdwardLiu/p/11738048.html

 class Heap{
private int[] nodes;
private int size;
private boolean isMaxHeap; public Heap(int capa, boolean isMax){
nodes = new int[capa];
size = 0;
isMaxHeap = isMax;
} //Build heap from given array.
public Heap(int[] A, boolean isMax){
nodes = new int[A.length];
size = A.length;
isMaxHeap = isMax;
for (int i=0;i<A.length;i++) nodes[i] = A[i];
int start = A.length/2;
for (int i=start;i>=0;i--)
shiftDown(i);
} //Assume A and nodes have the same length.
public void getNodesValue(int[] A){
for (int i=0;i<nodes.length;i++) A[i] = nodes[i];
} public boolean isEmpty(){
if (size==0) return true;
else return false;
} public int getHeapRootValue(){
//should throw exception when size==0;
return nodes[0];
} private void swap(int x, int y){
int temp = nodes[x];
nodes[x] = nodes[y];
nodes[y] = temp;
} public boolean insert(int val){
if (size==nodes.length) return false;
size++;
nodes[size-1]=val;
//check its father iteratively.
int cur = size-1;
int father = (cur-1)/2;
while (father>=0 && ((isMaxHeap && nodes[cur]>nodes[father]) || (!isMaxHeap && nodes[cur]<nodes[father]))){
swap(cur,father);
cur = father;
father = (cur-1)/2;
}
return true;
} private void shiftDown(int ind){
int left = (ind+1)*2-1;
int right = (ind+1)*2;
while (left<size || right<size){
if (isMaxHeap){
int leftVal = (left<size) ? nodes[left] : Integer.MIN_VALUE;
int rightVal = (right<size) ? nodes[right] : Integer.MIN_VALUE;
int next = (leftVal>=rightVal) ? left : right;
if (nodes[ind]>nodes[next]) break;
else {
swap(ind,next);
ind = next;
left = (ind+1)*2-1;
right = (ind+1)*2;
}
} else {
int leftVal = (left<size) ? nodes[left] : Integer.MAX_VALUE;
int rightVal = (right<size) ? nodes[right] : Integer.MAX_VALUE;
int next = (leftVal<=rightVal) ? left : right;
if (nodes[ind]<nodes[next]) break;
else {
swap(ind,next);
ind = next;
left = (ind+1)*2-1;
right = (ind+1)*2;
}
}
}
} public int popHeapRoot(){
//should throw exception, when heap is empty. int rootVal = nodes[0];
swap(0,size-1);
size--;
if (size>0) shiftDown(0);
return rootVal;
}
} public class Solution {
/**
* @param A: Given an integer array
* @return: void
*/
public void heapify(int[] A) {
if (A.length==0) return; Heap minHeap = new Heap(A,false);
minHeap.getNodesValue(A);
}
}

经常有关Heap的问题比如:

k largest(or smallest) elements in an array
Write an efficient program for printing k largest elements in an array. Elements in array can be in any order.

常用的方法肯定有QuickSelect, 用Heap也有两种方法可解:

Method Use Max Heap
1) Build a Max Heap tree in O(n)
2) Use Extract Max k times to get k maximum elements from the Max Heap O(klogn)

这个Max Heap的size是O(N)

Time complexity: O(n + klogn)

推荐方法:

Method Use Min Heap

1) Build a Min Heap MH of the first k elements (arr[0] to arr[k-1]) of the given array. O(k)

2) For each element, after the kth element (arr[k] to arr[n-1]), compare it with root of MH.
  a) If the element is greater than the root then make it root and call heapifyfor MH
  b) Else ignore it.
// The step 2 is O((n-k)*logk)

3) Finally, MH has k largest elements and root of the MH is the kth largest element.

这个Min Heap的size是O(k)

Time Complexity: O(k + (n-k)Logk) without sorted output.

Lintcode: Heapify && Summary: Heap的更多相关文章

  1. LintCode "Heapify"

    My first try was, using partial sort to figure out numbers layer by layer in the heap.. it only fail ...

  2. Lintcode: Singleton && Summary: Synchronization and OOD

    Singleton is a most widely used design pattern. If a class has and only has one instance at every mo ...

  3. 算法 Heap sort

    // ------------------------------------------------------------------------------------------------- ...

  4. Python常用数据结构之heapq模块

    Python数据结构常用模块:collections.heapq.operator.itertools heapq 堆是一种特殊的树形结构,通常我们所说的堆的数据结构指的是完全二叉树,并且根节点的值小 ...

  5. linux调试工具glibc的演示分析-core dump double free【转】

    转自:http://www.cnblogs.com/jiayy/p/3475544.html 偶然中发现,下面的两端代码表现不一样 void main(){ void* p1 = malloc(32) ...

  6. linux调试工具glibc的演示分析

    偶然中发现,下面的两端代码表现不一样 void main(){ void* p1 = malloc(32);       free(p1); free(p1); // 这里会报double free ...

  7. Windbg基本命令应用总结

    .cordll -ve -u -l //reload core dlls ------加载下载系统文件符号的URL---------- .sympath SRV*C:\Symbols*http://m ...

  8. centos安装hadoop(伪分布式)

    在本机上装的CentOS 5.5 虚拟机, 软件准备:jdk 1.6 U26 hadoop:hadoop-0.20.203.tar.gz ssh检查配置 [root@localhost ~]# ssh ...

  9. [算法]打印N个数组的整体最大Top K

    题目: 有N个长度不一的数组,所有的数组都是有序的,请从大到小打印这N个数组整体最大的前K个数. 例如: 输入含有N行元素的二维数组代表N个一维数组. 219,405,538,845,971 148, ...

随机推荐

  1. DOS 如何取当前时间做为文件名?

    如果要取得以日期为文件名的文件,假设在命令行下键入date返回形式为:当前日期: 2005-06-02 星期四echo > %date:~0,4%%date:~5,2%%date:~8,2%~表 ...

  2. 电子邮件 -- 图解TCP_IP_第5版

    图解TCP_IP_第5版 作者: [日]竹下隆史 / [日]村山公保 / [日]荒井透 / [日]苅田幸雄 出版社: 人民邮电出版社原作名: マスタリングTCP/IP 入門編 第5版译者: 乌尼日其其 ...

  3. LeetCode 36 Valid Sudoku(合法的数独)

    题目链接: https://leetcode.com/problems/valid-sudoku/?tab=Description   给出一个二维数组,数组大小为数独的大小,即9*9  其中,未填入 ...

  4. Centos 升级 python

    昨天把redmine的测试环境给搞Over了,想了下,干脆直接把环境给整成docker化的,配置环境的时候,安装docker-compose需要python2.7支持. CentOS 6 系统默认 P ...

  5. Maven:版本管理 【SNAPSHOT】【Release】【maven-release-plugin】【nexus】

    什么是版本管理 首先,这里说的版本管理(version management)不是指版本控制(version control),但是本文假设你拥有基本的版本控制的知识,了解subversion的基本用 ...

  6. 23种设计模式之外观模式(Facade)

    外观模式是对象的结构模式,要求外部与一个子系统的通信必须通过一个统一的外观对象进行,为子系统中的一组接口提供一个一致的界面,外观模式定义了一个高层接口,这个接口使得这一子系统更加容易使用. 优点: 1 ...

  7. Unity3D Mecanim :Body Mask的使用、 角色Retargeting原理分析、Apply RootMotion

    一.Body Mask的使用 1.1.配置好骨骼后通过Muscles来微调角色骨骼中的运动范围,以避免角色在动画中的不正确的叠加或失真等现象. 1.2.身体遮罩BodyMask更形象的描述就是身体的开 ...

  8. Sciter TIScript KeyEvent

    function movable() // install movable window handler{ function onKeyDown(evt) { if(evt.keyCode == Ev ...

  9. 2018C语言第三次作业

    要求一 2.struct sk{int a; char *str)}*p;   p->str++ 中的++ 加向? ++加向srt的地址. 要求二 题目1-计算平均成绩 1.设计思路 (1)主要 ...

  10. MySQL复制原理

    mysql从3.23开始提供复制功能,复制指将主库的ddl和dml操作通过binlog文件传送到从库上执行,从而保持主库和从库数据同步.mysql支持一台主库同时向多台从库复制,从库同时也可以作为其他 ...