第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

【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;
        }
    }
}


點(diǎn)擊查看更多內(nèi)容
TA 點(diǎn)贊

若覺得本文不錯(cuò),就分享一下吧!

評(píng)論

作者其他優(yōu)質(zhì)文章

正在加載中
  • 推薦
  • 評(píng)論
  • 收藏
  • 共同學(xué)習(xí),寫下你的評(píng)論
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說多少就多少
贊賞金額會(huì)直接到老師賬戶
支付方式
打開微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊有機(jī)會(huì)得

100積分直接送

付費(fèi)專欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會(huì)
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)

舉報(bào)

0/150
提交
取消