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

    web: 签到:,更改学号,找规律,用笔记本记录出现的数据. 我太喜欢哔哩哔哩大学啦--中北大学:就往下面翻找flag,就会看见一个flag的语句,这个就是答案. ezgame-浙江师范大学:这个就是 ...

  2. pr 如何给视频进行加速,慢速处理

    pr 如何给视频进行加速,慢速处理 1.首先导入视频素材,并将其拖拽到时间轴上 2.然后右键视频素材,点击"速度/持续时间" 3.然后会弹出这个界面,改变速度值,就可以更改视频速度 ...

  3. 集群activemq重启报错java.lang.OutOfMemoryError: GC overhead limit exceeded

    最近安全部门同事说我们环境有个弱密码漏洞,activemq后台的密码不够复杂,需要改为复杂密码. 我登录了他们扫出来的url,输入admin admin,果然进来了.不得不说上一位已离职同事的安全意识 ...

  4. win11恢复完整右键菜单

    使用注册表修改 首先,通过修改注册表,我们就可以将 Win11 的右键菜单改为老样式.下面是具体的方法. 运行"regedit",开启注册表编辑器,定位到"HKEY_CU ...

  5. MySQL表操作(上篇)

    1.存储引擎的介绍 (1)存储引擎 1.什么是存储引擎? mysql中建立的库===>文件夹 库中建立的表===>文件 现实生活中我们用来存储数据的文件有不同的类型,每种文件类型对应各自不 ...

  6. saml login的流程

    用户会访问首页/, 然后进入到指定的一个URL, 就是admin在site-settings里面的设置的那个地址, 发现权限不够,进入到403accesslogin, 然后调用user4032logi ...

  7. js 评论时间换算

    //时间戳换算 let dateTime=2020-10-10 10:10:10 getDateDiff(dateTime){ let dateTimeStamp = new Date(dateTim ...

  8. 面试之AQS

    https://blog.csdn.net/wwwzhouzy/article/details/119702170 https://copyfuture.com/blogs-details/20200 ...

  9. .Net Core 中使用NLog替代默认日志

    1.添加引用nlog.config和Nlog.Web.AspNetCore 2.配置NLog 配置文件 添加一个Web配置文件xxxx.Config <?xml version="1. ...

  10. java hibernate +mysql demo

    origin article:http://www.javatpoint.com/example-to-create-hibernate-application-in-eclipse-ide requ ...