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. pytorch自定义RNN结构(附代码)

    pytorch自定义LSTM结构(附代码) 有时我们可能会需要修改LSTM的结构,比如用分段线性函数替代非线性函数,这篇博客主要写如何用pytorch自定义一个LSTM结构,并在IMDB数据集上搭建了 ...

  2. ClickHouse 使用

    最近mysql报表数据太多,要转移数据到 clickHouse ,顺便学学该数据仓库的使用 中文文档:https://clickhouse.com/docs/zh/ B站学习视频 : https:// ...

  3. c++文件中,头文件与实现文件该写什么内容

    1. 参考https://www.cnblogs.com/fenghuan/p/4794514.html

  4. 2017GPLT

    PTA天梯赛2017GPLT 7-6 整除光棍 给定一个不以5结尾的奇数\(x\),求出数字\(n\)使得\(n*x=11...111\),输出数字n和1的位数 题解:模拟竖式除法 我们一开始发现n只 ...

  5. 7.mysql索引失效

          失效的七字口诀: 模型数空运最快 模:模糊查询以%开始索引失效: 型:数据类型转换 函数:函数的索引 空:索引列为空不走索引, 运:对索引列进行加减乘除会失效 最:不按聚合索引的最左匹配会 ...

  6. postman脚本语法大全,不包括插件语法

    官方语法例子:https://learning.postman.com/docs/writing-scripts/script-references/test-examples/ 官方语法库:http ...

  7. Unity旧版图集和新版图集

    1.关于旧版图集 ===>结论:设置PackingTag就可以了. ===>分析:设置好PackingTag,那么在进行打包的时候,同一个标签的会被打到1个图集里面,图集最大为2048x2 ...

  8. 【NPDP专项练习】第七章 产品生命周期管理

    第七章 产品生命周期管理 1.以下哪一项是产品生命周期缩短的原因之一? A 技术停滞不前 B 减少竞争 C 顾客要求更高 D 沟通障碍正在增加 答案:C 解析 A技术持续进步:B竞争加剧:D沟通增加 ...

  9. Redis 突然变慢了如何排查并解决?

    业务场景 某购物平台打算举行"双十一"大型购物狂欢活动,到了半夜12点用户数量暴增,出现了一个技术故障,用户发现自己无法下单!!! 技术组立即组织人手进行故障排查,结论是 Redi ...

  10. sqlalchemy 数据类型