【LEETCODE】模拟面试-215. Kth Largest Element in an Array

图:新生大学

https://leetcode.com/problems/kth-largest-element-in-an-array/

Find the kth largest element in an unsorted array.
Note that it is the kth largest element in the sorted order, not the kth distinct element.
For example,Given [3,2,1,5,6,4]
and k = 2, return 5.
**Note: **You may assume k is always valid, 1 ≤ k ≤ array's length.

**input: **an unsorted array, an integer k
output: return an integer, which is the kth largest in the given array, including duplicates
corner: when the array is null, or its length is less than k, there is no such element

The primitive idea is to sort the array, and count the kth element, if we use merge sort, it will take O(n logn), plus O(k) to count.

Or we can optimize that we just sort the largest k elements, ignoring the remain n-k elements. It will raise the Heap structure, since it's fast to find the largest element.

So, we scan from left to right in the array, first put k elements into a heap to heapify. Time is O(k)
This heap is a minHeap where its top is the smallest.
Every time we scan an element in the array, we compare it with the top of the heap.
If it's smaller than or equal to top, we keep moving on.
If it's larger than top, we pop the top, and put it in the top, and main the heap to be a minHeap. Time is O(logk)
After we scanned all the elements in the array, we just need to pop the top, which is the right largest one. Need to compare n - k times.

Finally, time complexity is O((n - k)logk + k)
space is O(k)

public class Solution {
    public int findKthLargest(int[] nums, int k){
    if ( nums == null || nums.length < k ){
        return Integer.MIN_VALUE;
    }
    
    //method: maintain a min heap with size k
    PriorityQueue<Integer> minHeap = new PriorityQueue<Integer>(k, new MyComparator());
    
    //1.add first k elements
    for ( int i = 0; i < k; i++ ){
        minHeap.offer(nums[i]);
    }
    
    //2.compare remain elements, add
    for ( int i = k; i < nums.length; i++ ){
        if ( nums[i] > minHeap.peek() ){
            minHeap.poll();
            minHeap.offer(nums[i]);
        }
    }
    
    return minHeap.poll();
    
}

class MyComparator implements Comparator<Integer>{
    @Override
    public int compare(Integer o1, Integer o2){
        if ( o1.equals(o2) ){
            return 0;
        }else{
            //from small to large, minHeap
            return o1 - o2;
        }
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 12,164评论 0 10
  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 13,487评论 0 23
  • 1,从这篇文章中,我学到的最重要的概念人要有自己个人的立场,坚持自己的立场,绝不退让父母是孩子的榜样,做好榜样很重...
    朕的小哆啦阅读 1,930评论 3 0
  • 早上临时安排有变化,照顾孩子吃饭的时间不得不提前离开。因为公公在楼下催促,只好匆忙离开。被人催促的时候我特别着急,...
    记与忆阅读 1,586评论 0 0
  • 一1一 晚上一边刷微博,一边听音乐。手机收藏的歌单,每一首顺序播放。 终于放到了卢冠廷的《一生所爱》。 “过去,现...
    印大钞阅读 3,214评论 2 0