Maintain a min-heap with size = k, to collect the result.

 //Find K minimum values from an unsorted array
//Implement Min-Heap public int[] findKMax(int[] arr, int k){
if(arr == null || arr.length == 0) return null;
int[] result = new int[k];
for(int i = 0; i < k; i ++)
result[i] = arr[i];
buildMinHeap(result);
for(int i = k; i < arr.length; i ++){
if(arr[i] > result[0]){
result[0] = arr[i];
minHeapify(result, 0);
}
}
return result;
} public void buildMinHeap(int[] arr){
for(int i = arr.length / 2 - 1; i > -1; i --){//bottom-up build min heap
minHeapify(arr, i);
}
} public void minHeapify(int[] arr, int curIndex){
int left = curIndex * 2 + 1;
int right = curIndex * 2 + 2;
int smallest = curIndex;
if(left < arr.length && arr[left] < arr[smallest])
smallest = left;
if(right < arr.length && arr[right] < arr[smallest])
smallest = right;
if(smallest != curIndex){
swap(arr, smallest, curIndex);
minHeapify(arr,smallest);
}
} public void swap(int[] arr, int aa, int bb){
int tmp = arr[aa];
arr[aa] = arr[bb];
arr[bb] = tmp;
}

find K maximum value from an unsorted array(implement min heap)的更多相关文章

  1. Why is processing a sorted array faster than an unsorted array?

    这是我在逛 Stack Overflow 时遇见的一个高分问题:Why is processing a sorted array faster than an unsorted array?,我觉得这 ...

  2. Kth Smallest Element in Unsorted Array

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

  3. [Swift]LeetCode325. 最大子数组之和为k $ Maximum Size Subarray Sum Equals k

    Given an array nums and a target value k, find the maximum length of a subarray that sums to k. If t ...

  4. Why is processing a sorted array faster than an unsorted array(Stackoverflow)

    What is Branch Prediction? Consider a railroad junction: Image by Mecanismo, via Wikimedia Commons. ...

  5. 77 找出最大连续自然数个数[Longest Consecutive Sequence in an Unsorted Array]

    [本文链接] http://www.cnblogs.com/hellogiser/p/Longest-Consecutive-Sequence-in-an-Unsorted-Array.html [题 ...

  6. Data Structure Array: Find the two numbers with odd occurrences in an unsorted array

    http://www.geeksforgeeks.org/find-the-two-numbers-with-odd-occurences-in-an-unsorted-array/ #include ...

  7. 【leetcode】Maximum Gap

    Maximum Gap Given an unsorted array, find the maximum difference between the successive elements in ...

  8. 【LeetCode】164. Maximum Gap (2 solutions)

    Maximum Gap Given an unsorted array, find the maximum difference between the successive elements in ...

  9. 【刷题-LeetCode】164 Maximum Gap

    Maximum Gap Given an unsorted array, find the maximum difference between the successive elements in ...

随机推荐

  1. 牛客国庆集训派对Day1 B. Attack on Titan

    B. Attack on Titan 链接 #include<cstdio> #include<algorithm> #include<cstring> #incl ...

  2. $watch和$observe的使用

    $observe 是Attribute对象的一个方法,用来监听DOM中属性值的变化.比如 attr1="{{name}}" Attribute定义在directive中的link函 ...

  3. SpringCloud-微服务网关ZUUL(六)

    前言:前面说过,由于微服务过多,可能某一个小业务就需要调各种微服务的接口,不可避免的就会需要负载均衡和反向代理了,以确保ui不直接与所有的微服务接口接触,所以我们需要使用一个组件来做分发,跨域等各种请 ...

  4. C语言编译过程以及gcc编译参数

    1.1       C语言编译过程,gcc参数简介 1.1.1          C语言编译过程 一.gcc - o a a.c -o:指定文件输出名字 二.C语言编译的过程: 1.1.1       ...

  5. UWP 自然灾害App在刷新数据后卡死的解决方案

    一直以为都在纳闷,为啥我的其他app崩溃次数几乎为0,而单单这个App的崩溃次数简直逆天了,我都不敢相信. 每天都有至少上千次crash...我也是服的 不甘心,趁着这次重构的机会,把代码好好捋了1下 ...

  6. How to: Display a Non-Persistent Object's List View from the Navigation

    This example demonstrates how to display a non-persistent object's List View when a navigation item ...

  7. CSS3新增特性详解(一)

    注:由于CSS3的新特性较多,所以分两篇博文来说明.第一篇主要包括新的选择器.文字及块阴影.多背景图.颜色渐变.圆角等.第二篇主要细说CSS3的各种动画效果,如:旋转.移动.缩放等,还包括图标字体的应 ...

  8. hdu2844Coins(多重背包模板)

    Coins Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  9. 如何让mysql按照两个或多个字段排序

    我准备设计一个供求信息的表格,里边包含序号(id)(自动增量),发布日期(time),上次更新(last_time).因为考虑到避免有人不停的重复发布信息来占据前列位置所以设置了last_time这个 ...

  10. 解决Unity烘焙阴影锯齿精度不足的问题

    烘焙阴影锯齿问题  烘焙后阴影锯齿明显,如下图: 烘焙的光照贴图质量主要受LightmapParameters 的Blur Radius和抗锯齿级别影响, 默认最高级别如下: 如果最高级别不能达到好的 ...