Continue reading the solutions , I found a very clever optimization to the previous algorithm:
After heapfying the whole array into a minimum heap and when poping up minimum elements from the heap , it's not necessary to sink down the last element ( the last element will be swapped with the first element in the pop operation) to the bottom , you just need to sink down it for k-1 times ( and then in the next pop k-2 times and so on), Because we just need to find the k smallest elements , for those elements under the layer k they are unrelated. (i.e. After first pop operation, the last element will only sink k-1 times and the whole heap is not a minimum heap anymore but the k-1 smallest elements are still guaranteed to be in level 1 - level k-1 which the sub heap is still a minimum heap.)
It's a very specific optimization to this scenario and the time complexity is O(2n + k^2) ( for the pop operation the time spent is 1 + 2 + 3 + ... + k-1 ).
Java Implementation:
public class KSmallestB { 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) { heapfy (nums); int[] result = new int[k]; for (int i = k-1 ; i >= 0 ; i --) { // nums.length + i - (k-1) is the total count left in the heap result[i] = pop(nums, nums.length + i - (k-1) , i); } return result; } public static void heapfy (int[] nums) { for (int i = nums.length /2 ; i > 0 ; i -- ) { sink(nums, nums.length, i, Integer.MAX_VALUE); } } // return the smallest and sink at most depth times for the last element public static int pop ( int[] nums, int length, int depth) { swap(nums, 1, length--); sink(nums, length, 1 , depth); return nums[length]; } // sink at most depth times public static void sink ( int[] nums , int length, int i , int depth) { int j = i * 2; while( j <= length && depth> 0 ) { // -1 is to adjust the index , because this array starts from 0 if ( j < length && nums[j+1 -1] < nums[j -1]) j++; if (nums[j -1] < nums[i -1]) { swap(nums, i, j); i = j; j = 2*i; } else { break; } } } public static void swap ( int[] nums, int i , int j) { int temp = nums[i-1]; nums[i-1] = nums[j-1]; nums[j-1] = temp; } }
相关推荐
373._Find_K_Pairs_with_Smallest_Sums查找和最小的k对数字【LeetCode单题讲解系列】
标题 "the kth smallest elem" 暗示我们要讨论的是如何在数组或集合中找到第k小的元素,这是一个常见的算法问题,在计算机科学和数据结构领域具有重要意义。描述中提到的 "Clion" 是一个流行的C/C++集成开发环境,...
在编程题库平台LeetCode上,有一道著名的算法题,名为“Find K Pairs with Smallest Sums”。这道题要求求出两个未排序数组的k个最小和的组合。具体来说,假设有两个整数数组nums1和nums2,返回这两数组中所有可能的...
在本文档中,我们将会探索Java语言编写的LeetCode题解,该题解针对的是"Find K-th Smallest Pair Distance"这一具体问题。此问题要求我们对一个整数数组找出第k小的数对距离。数对距离是指一对整数之间的差的绝对值...
代码:// find K-th Smallest Pair Distancepublic int smallestDistancePair(int[] nums
3. **定义尺寸**:在`dimens.xml`文件中,使用`<dimen>`标签定义尺寸值,如: ```xml <dimen name="button_width">150dp <dimen name="text_size">16sp ``` 这里的`button_width`和`text_size`是可以用在...
3、用途:项目具有较高的学习借鉴价值,不仅适用于小白学习入门进阶。也可作为毕设项目、课程设计、大作业、初期项目立项演示等。 4、如果基础还行,或热爱钻研,亦可在此项目代码基础上进行修改添加,实现其他不同...
题目要求我们找出第K小的素数分数。在这个场景下,素数分数是指分子和分母都是素数的分数。 为了解决这个问题,首先需要了解素数的概念,素数是只有1和它本身两个正因数的自然数。然后需要了解如何对分数进行排序,...
378.Kth_Smallest_Element_in_a_Sorted_Matrix有序矩阵中第K小的元素【LeetCode单
在处理这个问题之前,我们首先需要理解题目“Kth Smallest Element in a Sorted Matrix”的意思。这道题是LeetCode上的算法题目,要求求解在一个按行和列都排好序的矩阵中,找到第k小的元素。此类问题通常涉及到矩阵...
"smallest3hq"可能指的是C8051F单片机的最小系统设计,该设计通常包括电源、晶振、复位电路以及必要的I/O接口。在C语言编程中,我们需要初始化这些硬件资源,如设置时钟频率、配置I/O口等。 五、实践与研究 ...
CP2K是一款开源程序,主要用于基于密度...本手册的最后更新时间为2011年11月3日,随着时间的推移,CP2K程序可能会进行更新和优化。因此,用户在使用本手册时,建议参考最新的CP2K官方网站以获取最新版本的使用信息。
在编程领域,"k-th Smallest element in an array" 是一个常见的问题,它涉及到排序、查找和算法效率等核心概念。在这个问题中,目标是找到一个整数数组中第k小的元素,其中k是一个正整数。这个问题在数据结构和算法...
A = 1 2 3 then B = 3 2 1 4 5 6 6 5 4 7 8 9 9 8 7 Write a main program to call reverse(A) for the matrix A = magic(5). Print to the screen both A and reverse(A). 2) Write a program which accepts ...
在Java语言实现的LeetCode题解中,"Kth Smallest Element in a BST"这一问题尤为引人注目,它涉及到了二叉树的遍历以及对树的特性进行深度利用。 二叉搜索树具有独特的特性:对于任意节点,其左子树中的所有元素都...
labview实现两路电压采集。设计一个VI程序,进行两路电压信号采集。一路电压信号的范围0~3.3V, 每隔100ms采一个点,共采集50个点,另一路电压信号的范围为5~10V,采样间隔是50ms,共采集100个点。...
3. Use a single-subscripted array to solve the following problem. Read in 20 numbers, each of which is between 10 and 100, inclusive. As each number is read, print it only if it is not a duplicate of...
3.Use a single-subscripted array to solve the following problem. Read in 20 numbers, each of which is between 10 and 100, inclusive. As each number is read, print it only if it is not a duplicate of ...
C程序,计算n个整数序列中的第k个最小数字。 该程序将最多进行9次计数排序,每个数字位置一次。 第一种将对“百毫”数字进行运算。 最后一种排序将对“一个”数字进行操作(MSD基数排序受启发)。 每种分类都...
在处理多层嵌套循环生成的乘法表时,寻找第k小的数是一项具有挑战性的任务,需要高效的算法来优化查找性能。乘法表通常是一个矩阵,其行和列代表两个自然数序列的乘积,例如,第一行是1乘以1到n,第二行是2乘以1到n...