【LEETCODE】模擬面試-215. Kth Largest Ele
圖:新生大學
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 minHeap = new PriorityQueue(k, new MyComparator()); //1.add first k elements for ( int i = 0; i minHeap.peek() ){ minHeap.poll(); minHeap.offer(nums[i]); } } return minHeap.poll(); }class MyComparator implements Comparator { @Override public int compare(Integer o1, Integer o2){ if ( o1.equals(o2) ){ return 0; }else{ //from small to large, minHeap return o1 - o2; } } }
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/1762/viewspace-2806027/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- python leetcode 215. Kth Largest Element in an ArrayPythonLeetCode
- 215. Kth Largest Element in an Array
- LeetCode Kth Largest Element in an ArrayLeetCode
- LeetCode C++ 703. Kth Largest Element in a Stream【Heap/Design】簡單LeetCodeC++
- [LeetCode/LintCode] Largest Palindrome ProductLeetCode
- leetcode 368. Largest Divisible SubsetLeetCode
- [LeetCode] 230. Kth Smallest Element in a BSTLeetCode
- [LeetCode] 378. Kth Smallest Element in a Sorted MatrixLeetCode
- [LeetCode] 1545. Find Kth Bit in Nth Binary StringLeetCode
- 尋找模擬面試夥伴,共同成長面試
- [leetcode] 1624. Largest Substring Between Two Equal CharactersLeetCode
- [LeetCode] 2275. Largest Combination With Bitwise AND Greater Than ZeroLeetCode
- 使用OkHttp模擬登陸LeetCodeHTTPLeetCode
- 976. Largest Perimeter Triangle(Leetcode每日一題-2020.11.29)LeetCode每日一題
- LeetCode 面試題01.06LeetCode面試題
- 面試官:請用SQL模擬一個死鎖面試SQL
- ChatGPT模擬Go語言面試 (一次小的測試)ChatGPTGo面試
- 對Largest函式的測試函式
- 面試官問:能否模擬實現JS的bind方法面試JS
- linux模擬windows嘗試LinuxWindows
- LeetCode-20. 有效的括號(棧模擬)LeetCode
- leetcode 68. 文字左右對齊 模擬LeetCode
- 2020年焊工(高階)模擬試題及焊工(高階)模擬考試
- 面試官問:能否模擬實現JS的new操作符面試JS
- jmeter模擬spike測試(尖峰測試)JMeter
- 智駕模擬測試實戰之自動泊車HiL模擬測試
- Charles 模擬弱網測試
- ---ios模擬弱網測試---iOS
- LeetCode 面試題16.07[最大數值]LeetCode面試題
- leetcode-面試經典150題LeetCode面試
- 模擬面試,解鎖大廠 ——從Android的事件分發說起面試Android事件
- RHCE - EX2000模擬測試
- 單元測試模擬框架Mockito框架Mockito
- C#專案—模擬考試C#
- 模擬考試參考程式碼
- 【LeetCode】面試題 01.03. URL化(Java)LeetCode面試題Java
- LeetCode#面試題 17.10. 主要元素LeetCode面試題
- 記一次面試題——call、apply、bind模擬實現的更好方式面試題APP