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); } } }
相关推荐
Anagrams by Robert Rayment.Finding anagrams quickly for up to 9 letters in VB is impossible challenge!).
Mobile Marketing Finding Your Customers No Matter Where They Are,英文版本,PDF 格式,大小 5 Mb,作者:Cindy Krum。 Topics include • Getting started fast with mobile marketing • Understanding the ...
3. **提升论证力度**:可靠的资料能够支撑作者的观点,使论文更加有说服力。 4. **避免抄袭问题**:正确引用资料有助于避免学术不端行为,保护作者权益。 ### 三、寻找最佳资料来源的方法 #### 1. 学术数据库与...
Chapter 3. Finding Sentences Chapter 4. Finding People and Things Chapter 5. Detecting Part of Speech Chapter 6. Classifying Texts and Documents Chapter 7. Using Parser to Extract Relationships ...
### 3. 特征测定原理 SeamTech Finding软件支持对多种特征进行测定,包括但不限于: - **重叠焊缝**:适用于金属板材间重叠部分的焊接。 - **半V形焊缝**:类似于V形焊缝,但只有一个侧面,用于不同的焊接需求。 ##...
标题中的"root-finding.rar_ROOT_root finding"暗示了这是一个关于根查找算法的资料包,而描述提到了三种方法:二分法、最近邻法和错点法。 **二分法**(Bisection Method)是最基础且最直观的根查找算法之一。它...
Sorting Strings with Embedded Numbers Recipe 5.6. Processing All of a List's Items in Random Order Recipe 5.7. Keeping a Sequence Ordered as Items Are Added Recipe 5.8. Getting the First Few ...
Finding Top-k Shortest Paths with Diversity 在计算机科学和信息技术领域中,寻找 Top-k 最短简单路径问题是一个经典的问题。该问题的应用非常广泛,例如在路线规划、交通导航、物流等领域都有重要的实践价值。本...
CSE-121-项目一个c ++程序,最多接受100x100个矩阵以对其执行多个操作。这些操作包括: 1....] where this matrix is a 3 by 3 square matrix.The system check if the input matrix was given corre
finding a majority among n votes.pdffinding a majority among n votes.pdffinding a majority among n votes.pdffinding a majority among n votes.pdfv
3 New LanguageFeatures 13 3.1 New C++11 Language Features . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 3.1.1 Important MinorSyntax Cleanups . . . . . . . . . . . . . . . . . . . . . 13 ...
《寻找阿尔法:量化构建交易策略》是一本深入探讨金融投资领域中量化交易策略的专著。阿尔法(Alpha)在投资领域中通常指的是超越市场平均收益的能力,是投资者追求的高回报目标。本书旨在通过严谨的定量方法,帮助...
总的来说,"Finding the Shortest"项目结合了图论与C++编程,提供了一个实际应用Dijkstra算法的实例。通过学习和实践这个项目,不仅可以深化对Dijkstra算法的理解,还能提升C++编程和问题解决的能力。在实际应用中,...
其中包含: 1.OpenSceneGraph_快速安装及学习.pdf 2.StepIntoOpenSceneGraph_郑州大学版.pdf 3.OSG程序设计教程.pdf ...14.Finding+and+Manipulating+a+Switch+and+DOF+Node.pdf 15.t2-Realism and Animation.pdf
标题 "Finding Top-k Min-Cost Connected Trees in Databases 2007" 指出了这篇论文的核心研究问题,即在数据库中寻找最小成本的前k个连通树。从标题可以推导出以下几点: 1. **数据库**:数据库技术是现代信息系统...
Line Numbers and Spaces ............................................................................................................ 4 Text Wrapping in This Book .........................................
3. LDA模型(Latent Dirichlet Allocation):文章中提到的由Blei、Ng和Jordan介绍的生成模型,很有可能是指著名的概率主题模型LDA。LDA是目前最流行的用于发现文本数据中隐含话题的模型之一。 4. 马尔可夫链蒙特...