find K maximum value from an unsorted array(implement min heap)
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)的更多相关文章
- Why is processing a sorted array faster than an unsorted array?
这是我在逛 Stack Overflow 时遇见的一个高分问题:Why is processing a sorted array faster than an unsorted array?,我觉得这 ...
- Kth Smallest Element in Unsorted Array
(referrence: GeeksforGeeks, Kth Largest Element in Array) This is a common algorithm problem appeari ...
- [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 ...
- 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. ...
- 77 找出最大连续自然数个数[Longest Consecutive Sequence in an Unsorted Array]
[本文链接] http://www.cnblogs.com/hellogiser/p/Longest-Consecutive-Sequence-in-an-Unsorted-Array.html [题 ...
- 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 ...
- 【leetcode】Maximum Gap
Maximum Gap Given an unsorted array, find the maximum difference between the successive elements in ...
- 【LeetCode】164. Maximum Gap (2 solutions)
Maximum Gap Given an unsorted array, find the maximum difference between the successive elements in ...
- 【刷题-LeetCode】164 Maximum Gap
Maximum Gap Given an unsorted array, find the maximum difference between the successive elements in ...
随机推荐
- 2-4 list练习题
参考答案 >>> names = [] >>> names.append('old_driver') >>> names.append('rain ...
- vscode 运行起来
1;需要安装yarn等一些列插件 2:运行yarn 和yarn serve -- open
- iOS开发-通过正则表达式进行各种判断银行卡,车牌号,邮箱地址,QQ,身份证,全字母,仅输入字母或数字同时包含大小写字母和数字,仅能输入中文等
/* * 验证银行卡号是否正确 * 车牌号验证 * 检验邮箱地址是否正确 * 手机号中间四位密文显示 * 判断QQ号是否正确(5-11位) * 判断身份证号是否正确(如末位为字母请用“x” ...
- lemon批量蒯
RT,很久以前写的拿出来骗一骗访问量 把sh文件扔进source里面运行sh *.sh 从子目录蒯出来: #!/bin/bash for file in ./*/*/*.cpp do name=${f ...
- 大数据中HBase的Java接口封装
该文前提为已经搭建好的HBase集群环境,参见 HBase集群搭建与配置 ,本文主要是用Java编写一个Servlet接口,部署在Tomcat服务器上,用于提供http的接口供其他地方调用,接口中集成 ...
- ncl 函数源码 gc_inout
转自气象家园论坛 经过不懈努力,终于找到了gc_inout函数的源代码,原来在这个文件里面!一颗赛艇 位置:/ncl_ncarg-6.5.0-src/ni/src/lib/nfpfort/sg_too ...
- 经典笔试题:用C写一个函数测试当前机器大小端模式
“用C语言写一个函数测试当前机器的大小端模式”是一个经典的笔试题,如下使用两种方式进行解答: 1. 用union来测试机器的大小端 #include <stdio.h> union tes ...
- Axure 制作 轮播 左右按钮轮播图
1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111 ...
- mysql 连接超时解决方案: 怎样修改默认超时时间
mysql数据库有一个wait_timeout的配置,默认值为28800(即8小时). 在默认配置不改变的情况下,如果连续8小时内都没有访问数据库的操作,再次访问mysql数据库的时候,mysql数据 ...
- webpack构建Vue项目引入jQ时发生“'$' is defined but never used”的处理
今天公司需要新建个数据后台,就按照查到的方法构建了Vue框架的项目,引入jQ.bootstrap时,按照在线方法配置,发现 main.js 里的引用jQ一直显示红标,没多想,在按照网上配置完后,npm ...