Leetcode 215. Kth Largest Element in an Array
来源:互联网 发布:node.js实战 第1季 编辑:程序博客网 时间:2024/06/04 18:55
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.
Analysis: I can use the heap sort to sort for the element, the priority queue is the heap implmentation
//Here is the heap implementation
http://algs4.cs.princeton.edu/24pq/
public class Solution { public int findKthLargest(int[] nums, int k) { PriorityQueue<Integer> queue = new PriorityQueue<Integer>(); int length = nums.length; for(int i = 0; i < length; i++){ if(queue.size() < k) queue.add(nums[i]); else if( queue.peek() < nums[i]){ queue.remove(); queue.add(nums[i]); } } return queue.peek(); }}
0 0
- Leetcode Kth Largest Element in an Array
- Leetcode: Kth Largest Element in an Array
- LeetCode Kth Largest Element in an Array
- [LeetCode] Kth Largest Element in an Array
- [leetcode] Kth Largest Element in an Array
- leetcode--Kth Largest Element in an Array
- #leetcode#Kth Largest Element in an Array
- LeetCode Kth Largest Element in an Array
- 【Leetcode】Kth Largest Element in an Array
- Leetcode: Kth Largest Element in an Array
- Kth Largest Element in an Array -- leetcode
- [Leetcode]Kth Largest Element in an Array
- [LeetCode]Kth Largest Element in an Array
- *LeetCode-Kth Largest Element in an Array
- LeetCode----Kth Largest Element in an Array
- LeetCode Kth Largest Element in an Array
- leetcode-Kth Largest Element in an Array
- LeetCode -- Kth Largest Element in an Array
- Leetcode 208. Implement Trie (Prefix Tree)
- Mac笔电盒盖保持声音输出?
- Leetcode 29 - Divide Two Integers
- POJ 1661 Help Jimmy
- Leetcode - 137. Single Number II
- Leetcode 215. Kth Largest Element in an Array
- Leetcode 295. Find Median from Data Stream
- 有return的情况下try catch finally的执行顺序(最有说服力的总结)
- Leetcode 32 - Longest Valid Parentheses
- Leetcode 146. LRU Cache
- JavaSE014_数组之应用举例(实现浮点数转换成人民币读法字符串)
- thinkphp 静态 伪静态 路由
- Scanner类获取键盘输入
- Thymeleaf的标准方言(Standard dialects)