Question: Among n integers , to find k smallest ones.
Sample Input : 5, 2, 1, 3 , 4, 7, 8, 6
Sample output : the 3 smallest ones are : 1, 2, 3.
My thought: Maximum-Heap is the first data structure comes to me. We can construct a maximum heap for the first k numbers and for each next number we compare it with the number on top of the heap, if it's bigger, we ignore it, otherwise we insert it into the heap and remove the number on top of the heap. Finally the numbers left in the heap is the smallest k numbers. The time complexity should be O(n log k)
Java Implementation (Java has its own implementation of heap as PriorityQueue. While here , for practice, we just provide a very specific maximum integer heap.):
public class KSmallest { public static void main(String[] args) { assertEquals(new int[]{1,2,3,4}, getKSmallest(new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, 4)); assertEquals(new int[]{1,2,3,4}, getKSmallest(new int[]{10, 9, 8, 7, 6, 5, 4, 3, 2, 1}, 4)); assertEquals(new int[]{21,22,14,18,9}, getKSmallest(new int[]{27, 14, 18, 22, 21, 91, 33, 36, 42, 78 , 9, 65, 101, 29}, 5)); } private static void assertEquals(int[] standard, int[] result) { Arrays.sort(standard); Arrays.sort(result); assert Arrays.equals(standard, result); } public static int[] getKSmallest(int[] nums, int k) { MaxIntHeap heap = new MaxIntHeap(nums, 0, k); for ( int i = k ; i < nums.length ; i ++ ) { if (nums[i] < heap.peek()) { heap.pop(); heap.add(nums[i]); } } return heap.getAll(); } public static class MaxIntHeap { private int[] nums; private int count; public MaxIntHeap ( int[] nums, int offset, int count){ this.nums = new int[count+1]; // index 0 will not be used System.arraycopy(nums, offset, this.nums, 1, count); this.count = count; for ( int i = count/2 ; i > 0 ; i --) { swimDown(i); } } public int[] getAll() { int result[] = new int[count]; System.arraycopy(this.nums, 1, result, 0, count); return result; } private void swimUp (int i) { int parent = i/2; while (parent != 0 && nums[i] > nums[parent]) { swap ( i, parent); i = parent; parent = i/2; } } private void swimDown(int i) { int lchild = i * 2; if ( lchild > count ) return; int bigger = lchild; int rchild = lchild + 1; if (rchild <= count && nums[rchild] > nums[lchild]) bigger = rchild; while( nums[bigger] > nums[i]) { swap ( bigger, i); i = bigger; lchild = i * 2; if ( lchild > count ) return; bigger = lchild; rchild = lchild + 1; if (rchild <= count && nums[rchild] > nums[lchild]) bigger = rchild; } } private void swap (int i , int j) { int temp = nums[i]; nums[i] = nums[j]; nums[j] = temp; } public int peek() { return nums[1]; } public int pop() { swap(1, count--); swimDown(1); return nums[count + 1]; } //don't concern resizing array here public void add (int num) { nums[++count] = num; swimUp(count); } } }
相关推荐
After finding the vulnerability, the attacker needs to scan for hosts that are vulnerable. The target is basically to compromise a series of systems by exploiting that particular vulnerability. Any ...
stm32+esp8266+mqtt/onenet智能家居
Android开发不用存储权限进行拍照,得到拍照后的图片效果。有一点难度,关键是存储路径的定义。
j
反向Lora提高画面细节。
小秘书(凤凰电话管理系统)【纽曼声卡版小秘书】,主要用来做为来电自动录音功能。
基于SpringBoot的疫情居家检测管理系统,系统包含三种角色:管理员、用户、医生,主要功能如下。 【用户功能】 1. 首页:获取系统信息。 2. 论坛:参与居民讨论和分享信息。 3. 公告:查看社区发布的各类公告。 4. 医保信息:了解医疗保障相关信息。 5. 个人中心:管理个人信息,查看预约和就诊历史。 【管理员功能】 1. 首页:查看系统整体。 2. 个人中心:管理管理员的个人信息。 3. 管理员管理:维护系统管理员的账户信息。 4. 医生管理:添加、编辑和删除医生信息。 5. 用户管理:查看和管理系统用户的信息。 6. 预约管理:审核和管理用户对医生的预约服务。 7. 就诊历史管理:查看和管理用户的就诊历史记录。 8. 健康信息管理:记录和查看用户的健康信息。 9. 药品管理:管理系统内的药品种类。 10. 药品入库管理:记录和管理药品的入库情况。 11. 药品使用管理:记录和管理药品的使用情况。 12. 医保信息管理:管理医保相关信息。 13. 论坛管理:审核和回复用户在论坛上的帖子。 14. 公告管理:发布、编辑和管理公告信息。 15. 基础数据管理:管理系统的基础数据。 16. 轮播图信息:管理系统首页的轮播图展示。 【医生功能】 1. 首页:查看医生个人信息。 2. 个人中心:管理医生的个人信息。 3. 预约管理:查看和管理用户对医生的预约服务。 4. 就诊历史管理:查看和管理用户的就诊历史记录。 5. 健康信息管理:记录和查看用户的健康信息。 6. 药品管理:管理系统内的药品种类。 7. 药品入库管理:记录和管理药品的入库情况。 8. 药品使用管理:记录和管理药品的使用情况。 9. 医保信息管理:管理医保相关信息。 10. 论坛管理:审核和回复用户在论坛上的帖子。 11. 公告管理:发布、编辑和管理公告信息。 12. 轮播图信息:管理系统首页的轮播
基于python的Opencv项目实战.zip
鸿蒙开发画廊效果功能,中间大,两边小的浏览效果,难度不小,进行了一定的封装。很好看的画廊效果
win32汇编环境,网络编程入门之十九
linux
【HD-RK3576-PI】定制用户升级固件
内容概要:本文是关于大规模L1正则化线性分类优化方法和软件比较的补充材料,由台湾大学计算机科学系的研究团队撰写。文章详细介绍了GLMNET算法的核心公式推导及其具体实现步骤,包括如何计算L¯j(0; X˜),以及如何维护关键变量以减少计算量。此外,文中对比了多种求解器(如CDN、IPM、TRON等)在不同数据集上的性能,涵盖达到特定停止准则所需时间、迭代次数及每次迭代的平均成本。研究结果显示,在大多数数据集上,CDN方法表现最优,但在极严格的条件下,IPM方法表现更好。对于L1和L2正则化的逻辑回归,文中指出L1正则化在某些数据类型上可能提供更好的准确性,但训练时间较长,因此推荐先尝试L2正则化用于分类任务,而L1正则化更适合特征选择。 适合人群:对机器学习算法尤其是正则化技术有一定了解的数据科学家和研究人员。 使用场景及目标:①需要进行大规模线性分类问题的优化;②比较不同优化方法和工具包在实际应用中的效果;③理解L1和L2正则化在逻辑回归中的区别及其适用情况。 其他说明:本文提供了详细的数学推导和实验结果分析,有助于深入理解各种优化方法的工作原理及其优劣。读者可以通过这些内容选择最适合自身需求的算法和工具包。
西电A测或通院微控温度仿真控制系统的proteus文件
华为ONT使能2.0工具
basalt_top
无极调速数控车床主轴箱装配CAD图.rar
乳液涂料生产流程图.rar
训练集数据
674322 Docker基础与实战.pdf