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 ...
随机推荐
- Python之闭包函数、装饰器
1.闭包函数 #作用域关系在函数定义阶段时就已经固定死了,与调用位置无关 # 即:在任意位置调用函数都需要跑到定义函数时寻找作用域关系 # def f1(): # x=1 # def inner(): ...
- async/await工作机制探究--NodeJS
ES6中的async/await让Promise变得更加简便,通常await处理的链式Promise会包裹在函数中,返回结果仍然是一个Promise对象. 但是当await直接处理链式Promise时 ...
- jmeter—操作数据库
添加JDBC Request,添加需要执行的sql语句 在这个界面需要配置Variabke Name,内容要与上表中的Name值相同:数据库的用户名.密码.URL.驱动这些基本信息要在这里配置:其他选 ...
- .Net MVC缓存
https://www.cnblogs.com/JoeSnail/p/7993903.html
- Netty源码分析第1章(Netty启动流程)---->第1节: 服务端初始化
Netty源码分析第一章: Server启动流程 概述: 本章主要讲解server启动的关键步骤, 读者只需要了解server启动的大概逻辑, 知道关键的步骤在哪个类执行即可, 并不需要了解每一步的 ...
- MySQL基础练习(三)
经过之前两次的学习,这次用MySQL进行略微复杂的操作练习 各部门工资最高的员工 首先创建表employee和表department.如下 我们需要查询每个部门工资最高的员工 select a.Nam ...
- Shader做剪影效果
某渣渣甩了一个需求给我,并且说我不会写.我明知是激将法,但是想想这需求也太简单了,我好像也不怎么会QAQ.为了表示我对shader的热爱,写就写. 需求是这样的: 这是一个漂亮的MM,但是渣渣不想让人 ...
- 华策光通信: LED可见光通信室内定位项目获最具投资价值奖
3月21日上午,一场持续3个多小时的O2O领域的创业DemoShow在深圳科兴科学园会议中心激烈上演.来自华策光通信的基于LED可见光通信室内精准定位项目作为LED与室内定位领域的跨界融合项目经过精彩 ...
- CSS命名规范(规则)常用的CSS命名规则
CSS命名规范(规则)常用的CSS命名规则 CSS命名规范(规则)常用的CSS命名规则 头:header 内容:content/container 尾:footer ...
- 20135332 第一次JAVA实验报告
课程:Java程序设计 班级: 1353 姓名:武西垚 学号:20135332 成绩: 指导教师:娄嘉鹏 实验日期:2 ...