K Smallest In Unsorted Array
Find the K smallest numbers in an unsorted integer array A. The returned numbers should be in ascending order.
Assumptions
- A is not null
- K is >= 0 and smaller than or equal to size of A
Return
- an array with size K containing the K smallest numbers in ascending order
Examples
A = {3, 4, 1, 2, 5}, K = 3, the 3 smallest numbers are {1, 2, 3}
public class Solution {
// use a priority queue(max heap with size of k) to figure it out.
// we traverse the array one time, whenever we met a element, we check it if smaller then the top element of the heap
// if it is, we poll and insert that new element in
// if it isn't, we do nothing
// time: O(n)
// space: O(k)
//
// thing to konw better: the vanilla priority q in java is max heap or min heap?
// how to turn a priority q into a array or a list in a efficient way?
//
// assumption: all element in array is integer and small than Integer.MAX_VALUE and larger than Integer.MIN_VALUE
// array is not null, array's length can be 0
// k can be zero, k is not larger than the length of array
public int[] kSmallest(int[] array, int k) {
// Write your solution here
int[] res = new int[k];
if(k==0){
return res;
}
PriorityQueue<Integer> pq = new PriorityQueue<>(k,Collections.reverseOrder());
for(int i=0; i<array.length; i++){
if(pq.size()<k){
pq.offer(array[i]);
}else{
if(array[i]<pq.peek()){
pq.poll();
pq.offer(array[i]);
}
}
}
for(int i=0; i<k; i++){
res[k-i-1] = pq.poll();
}
return res;
}
}
K Smallest In Unsorted Array的更多相关文章
- 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 a ...
- UVA-11997 K Smallest Sums
UVA - 11997 K Smallest Sums Time Limit: 1000MS Memory Limit: Unknown 64bit IO Format: %lld & ...
- D - K Smallest Sums(多路归并+贪心)
Problem K K Smallest Sums You're given k arrays, each array has k integers. There are kk ways to pic ...
- 【暑假】[实用数据结构]UVa11997 K Smallest Sums
UVa11997 K Smallest Sums 题目: K Smallest Sums You're given k arrays, each array has k integers. Ther ...
- 11997 - K Smallest Sums(优先队列)
11997 - K Smallest Sums You’re given k arrays, each array has k integers. There are kk ways to pick ...
- [Algorithm] How to use Max Heap to maintain K smallest items
Let's say we are given an array: [,,,,,,] We want to get K = 3 smallest items from the array and usi ...
- [Algorithm] Find Nth smallest value from Array
Q1: Find the smallest value from array: function findMin(arr) { let min = arr[0]; for (let i = 1; i ...
- UVa 11997 K Smallest Sums 优先队列&&打有序表&&归并
UVA - 11997 id=18702" target="_blank" style="color:blue; text-decoration:none&qu ...
- 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 ...
随机推荐
- openGL 学习笔记 (一) 了解 OpenGL,创建第一个OpenGL窗口
// 序章最开始我以为OpenGL是一系列的API,他给出了一系列对计算机图像的操作接口.但其实OpenGL其实并不是一个API,他是由khronos组织制定并维护的规范. 早期的OpenGL使用立即 ...
- 浅谈Atlassian产品搭建的敏捷管理体系(一)概述
准备把敏捷管理的专题在今年完成,主要谈一下Atlassian的实践,先做一下搬运工,讲去年写的两篇弄过来. Dream big, work smart, deliver fast 使用Atlassia ...
- Python学习的第一次总结
执行Python的方式:1.交互器(不易保存,闪现,不方便看)2.用win-cmd 来执行(用notepad++保存后执行) 开始 >> cmd >> cd c:\ #切c盘& ...
- 软件开发流程-路飞项目需求- pip永久换源-虚拟环境-路飞项目前后端创建-包导入-后端项目调整目录
目录 软件开发流程-路飞项目需求- pip永久换源-虚拟环境-路飞项目前后端创建-包导入-后端项目调整目录 今日内容概要 今日内容详细 1 软件开发流程 2 路飞项目需求 3 pip永久换源 4 虚拟 ...
- Object.create() 方浅析
Object.create()方法创建一个新对象,使用现有的对象来提供新创建的对象的__proto__. Object.create(proto[, propertiesObject]) 参数 pro ...
- Mysql 系统参数查看
1.查看数据库版本 select version(); 2.查看是否支持分区 show variables like '%partition%';show plugins;
- G1 垃圾回收详解
引用: https://www.cnblogs.com/ciel717/p/16190562.html
- hex 函数
python的内置函数. 一.作用: 用于将十进制数改为十六进制数,并以字符串的形式表示. 二.语法: hex(number) # 其中number为十进制的整数. 三.返回值: 以字符串形式表示的 ...
- 浪潮QQ群成员提取器 V2022
浪潮QQ群成员提取软件 V2021 最新升级版 使用扫描安全登录QQ,批量获取群列表,然后在读取指定群的群成员列表支持过滤群主.群管理员 支持按最后发言时间提取活跃用户 支持识别僵尸粉和过滤可以导出文 ...
- docker 运行环境
步骤 1 - 启用适用于 Linux 的 Windows 子系统 需要先启用"适用于 Linux 的 Windows 子系统"可选功能,然后才能在 Windows 上安装 Linu ...