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. OS-lab2

    OS-lab2 启动之后,我们的操作系统要能运行,需要一个载体,就是内存.内存的访问不是直接的,而是通过MMU完成.在完成MMU之前,我们先看看一些需要用到的宏定义和函数. include pmap. ...

  2. Win10用户目录迁移后变成英文的修改办法

    比如我的目录从C:\Users\Lemon修改到D:\Users\Lemon后: 1.首先可以将Windows文件管理器选项中的"隐藏受保护的操作系统文件"去掉,就可以看到每个目录 ...

  3. 自我介绍&学习心得

    这个作业属于哪个课程 https://edu.cnblogs.com/campus/fzzcxy/2023learning/join?id=CfDJ8GXQNXLgcs5PrnWvMs4xAGN4cH ...

  4. MySql密码忘记怎么办?

    第一步服务 net stop mysql 第二步,执行指令 mysqld --console --skip-grant-tables --user=mysql --shared-memory 第三步, ...

  5. EMQX 在 Kubernetes 中如何进行优雅升级

    背景 为了降低 EMQX 在 Kubernetes 上的部署.运维成本,我们将一些日常运维能力进行总结.抽象并整合到代码中,以 EMQX Kubernetes Operator 的方式帮助用户实现 E ...

  6. SpringBoot整合MyBatis-Plus详细使用方法

    SpringBoot整合mp 一.添加依赖pom.xml <dependency> <groupId>mysql</groupId> <artifactId& ...

  7. 使用CSS 绘制各种形状

      如何使用CSS 绘制各种形状? 很多小伙伴在做开发的时候,遇到一些要画很多形状的时候,就很纠结了,知道怎么用CSS去布局,就是不知道怎么画图案. 其实使用CSS可以绘制出很形状的,比如三角形,梯形 ...

  8. Python游戏开发常用库

    PyWeek:编程挑战,主要是Python游戏开发方面的 PyGame:PyGame在优秀的SDL库之上添加了更多功能.允许使用python语言创建功能齐全的游戏和多媒体程序.具有高度的可移植性,几乎 ...

  9. ReactJS单页面应用之项目搭建

    初衷 因接手的项目前端采用reactjs+antd,为把控项目中的各个细节,所以想做一些整理,以免后期遗忘. 创建及启动项目 # 全局安装create-react-app # 如果曾经安装过,可先移除 ...

  10. dynamics 365 复制(克隆)现有组织

    很多时候需要复制现有的开发环境作为新的开发环境,以保留原有测试数据,旧的开发环境作为问题修复环境,以下是复制步骤: 1. 登录数据库服务器,备份现有数据库 2. 还原数据库为新的数据库名称 3. 在前 ...