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++集成开发环境,...
java java_leetcode题解之K-th Smallest Prime Fraction.java
java java_leetcode题解之Find K Pairs with Smallest Sums.java
java java_leetcode题解之Find K-th Smallest Pair Distance.java
3. **定义尺寸**:在`dimens.xml`文件中,使用`<dimen>`标签定义尺寸值,如: ```xml <dimen name="button_width">150dp <dimen name="text_size">16sp ``` 这里的`button_width`和`text_size`是可以用在...
代码:// find K-th Smallest Pair Distancepublic int smallestDistancePair(int[] nums
378.Kth_Smallest_Element_in_a_Sorted_Matrix有序矩阵中第K小的元素【LeetCode单
"smallest3hq"可能指的是C8051F单片机的最小系统设计,该设计通常包括电源、晶振、复位电路以及必要的I/O接口。在C语言编程中,我们需要初始化这些硬件资源,如设置时钟频率、配置I/O口等。 五、实践与研究 ...
在编程领域,"k-th Smallest element in an array" 是一个常见的问题,它涉及到排序、查找和算法效率等核心概念。在这个问题中,目标是找到一个整数数组中第k小的元素,其中k是一个正整数。这个问题在数据结构和算法...
CP2K是一款开源程序,主要用于基于密度...本手册的最后更新时间为2011年11月3日,随着时间的推移,CP2K程序可能会进行更新和优化。因此,用户在使用本手册时,建议参考最新的CP2K官方网站以获取最新版本的使用信息。
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 ...
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 ...
java java_leetcode题解之Kth Smallest Number in Multiplication Table.java
java java_leetcode题解之Kth Smallest Element in a BST.java
java java_leetcode题解之Kth Smallest Element in a Sorted Matrix.java
python python_leetcode题解之2413_Smallest_Even_Multiple.py
c++ C++_leetcode题解之744. Find Smallest Letter Greater Than Target.cpp