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的更多相关文章

  1. 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 ...

  2. UVA-11997 K Smallest Sums

    UVA - 11997 K Smallest Sums Time Limit: 1000MS   Memory Limit: Unknown   64bit IO Format: %lld & ...

  3. 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 ...

  4. 【暑假】[实用数据结构]UVa11997 K Smallest Sums

    UVa11997 K Smallest Sums  题目: K Smallest Sums You're given k arrays, each array has k integers. Ther ...

  5. 11997 - K Smallest Sums(优先队列)

    11997 - K Smallest Sums You’re given k arrays, each array has k integers. There are kk ways to pick ...

  6. [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 ...

  7. [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 ...

  8. UVa 11997 K Smallest Sums 优先队列&amp;&amp;打有序表&amp;&amp;归并

    UVA - 11997 id=18702" target="_blank" style="color:blue; text-decoration:none&qu ...

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

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

  10. Kth Smallest Element in Unsorted Array

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

随机推荐

  1. 如何在eclipse里的动态Web项目建立后缀为xml的文件

    1.右击Dynamic Web Project类型项目的WEB-INF->new->Other->XML,选择XML File2 点击next改名字为web(后缀不要动哦)3. 点击 ...

  2. 直播软件搭建,姓名,身份证input验证过滤

    直播软件搭建,姓名,身份证input验证过滤 姓名验证:需求,可输入英文.汉字 for(let i=0;i<e.length;i++){  if(/^[a-zA-Z\u4e00-\u9fa5]+ ...

  3. hadoop集群搭建之测试集群,配置历史服务器,日志聚集,时间同步

    hadoop集群搭建之测试集群,配置历史服务器,日志聚集,时间同步前期概要:我们完全分布式集群已经搭建完毕,并且还写了群启/群关的脚本, 接下来有必要来测试一遍集群,我们可以使用官方的案例,用它们在集 ...

  4. 【Java学习Day07】标识符

    标识符 Java使用的组成部分的需要名字.类名.变量名以及方法名都被称为标识符 标识符的注意点 所有的标识符都应该以字母(A-Z或者a-z),美元符($).或者下划线(_)开始 首字符之后可以是字母( ...

  5. docker的安装和命令

    一. 认识Docker 我们写的代码会接触好几个环境:开发环境,测试环境以及生产环境 开发环境:程序员开发代码的环境 测试环境:开发完的代码部署到测试环境 给测试人员进行测试 生产环境:测试完成后有运 ...

  6. ERA-Interim 的变量TCW和VIWV可降水量

    可降水量(Precipitable water) 气象上有一个名词"可降水量"(Precipitable water),可以用来衡量大气的水含量. 其公式为 \(W=\frac{1 ...

  7. ERA5气压层数据驱动WRF的一些问题

    感谢Dawn的建议,兰溪之水的WRF教程 参考了一些经验,并结合实际后,成功用ERA5驱动WRF.实际上,用ERA5数据驱动WRF的方法和用ERA-Interim 数据驱动WRF极其类似. 总结几点是 ...

  8. 超2T硬盘使用gpt分区及做成lvm

    1.超过2T分区不能用fdisk了,用parted 分区格式化后对新的分区做lvm

  9. 采集地图商家电话,导出到excel

    快速的把高德地图左边的搜索列表里的商家地图,电话,导出到EXCEL里. 采集地图商家电话,可以快速提高销售人员的业绩. 如何快速地将高德地图里的商家电话资料导出EXCEL? 操作步骤: 1. 选择你要 ...

  10. C#windows 服务 《转载》

    转自:https://blog.csdn.net/Code_May/article/details/123909870 c#应用Windows服务 背景 一.创建windows服务 1.创建windo ...