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. Kubernetes学习之路(十五)之Ingress和Ingress Controller

    目录 一.什么是Ingress? 1.Pod 漂移问题 2.端口管理问题 3.域名分配及动态更新问题 二.如何创建Ingress资源 三.Ingress资源类型 1.单Service资源型Ingres ...

  2. ssm 配置事务回滚

    参考:https://blog.csdn.net/Mint6/article/details/78363761 在 applicationContext.xml 中配置好了事务和数据源等必须要用到的配 ...

  3. JavaScript 数组——filter()、map()、some()、every()、forEach()、lastIndexOf()、indexOf()

    filter():   语法: var filteredArray = array.filter(callback[, thisObject]); 参数说明: callback: 要对每个数组元素执行 ...

  4. 解决Eclipse Install New Software太慢的问题

    Eclipse -> Help -> Install New Software... 在出现的窗口点击Manage管理Available Software Sites 将所有URL中的&q ...

  5. Python的with语句(文件打开方式)

    Python文件打开方式(with语句) python编程中对于文件的打开方式主要有以下两种: 1.利用直接性的open("","")函数:(举例说明) try ...

  6. idea_debug

    条件断点 快捷键 cmd + shift +f8 demo 表达式求值 注意,调试的时候,选中相应变量 alt + f8 demo set value (感觉会非常有用) 调试时直接改变变量的值,快捷 ...

  7. P4562 [JXOI2018]游戏

    题面 题目描述 她长大以后创业了,开了一个公司. 但是管理公司是一个很累人的活,员工们经常背着可怜偷懒,可怜需要时不时对办公室进行检查. 可怜公司有 \(n\) 个办公室,办公室编号是 \(l\) 到 ...

  8. React Native移动开发实战-3-实现页面间的数据传递

    React Native使用props来实现页面间数据传递和通信.在React Native中,有两种方式可以存储和传递数据:props(属性)以及state(状态),其中: props通常是在父组件 ...

  9. Navicat新建查询,系统找不到指定路径 独家解决办法

    Navicat新建查询系统找不到指定路径,很多人用了网上流行的那些解决办法,还是无法解决.比如: https://jingyan.baidu.com/article/86112f1387a713273 ...

  10. Django_QueryDict

    介绍 class QueryDict(MultiValueDict): """ A specialized MultiValueDict which represents ...