Kth largest numbers ii
来源:互联网 发布:淘宝店铺行业排名 编辑:程序博客网 时间:2024/06/07 18:44
description:
Find K-th largest element in an array. and N is much larger than k.
Notice
You can swap elements in the array
Have you met this question in a real interview? Yes
Example
In array [9,3,2,4,8], the 3rd largest element is 4.
In array [1,2,3,4,5], the 1st largest element is 5, 2nd largest element is 4, 3rd largest element is 3 and etc.
这一道题目,可以使用PriorityQueue来做,
PriorityQueue是一种用于解决Top K问题的非常好的手段。插入方法的(offer、remove、poll、add)的时间复杂度都是O(logn); size、peek的时间复杂度为 O(1)
class Solution { /** * @param nums an integer unsorted array * @param k an integer from 1 to n * @return the kth largest element */ public int kthLargestElement2(int[] nums, int k) { // Write your code here PriorityQueue<Integer> pq = new PriorityQueue<>(k); for (int num : nums) { pq.offer(num); if (pq.size() > k) { pq.poll(); } } return pq.peek(); }}
0 0
- Kth largest numbers ii
- Kth Largest Element II
- Top k Largest Numbers II
- Top k Largest Numbers II
- Kth Largest
- Kth Largest
- Lintcode - kth largest element
- #5 Kth Largest Element
- **[Lintcode]Kth Largest Element
- LeetCode215. Kth largest element
- hrbeu Kth Largest (二重二分)
- Kth Largest Element 解题报告
- LintCode 5: Kth Largest Element
- find kth largest number in an array
- Kth Largest Element in an Array
- Leetcode Kth Largest Element in an Array
- Kth Largest Element in an Array
- Kth Largest Element in an Array
- Extreme type erasure via std::function 通过模板(而不是继承)实现接口
- iOS手把手教会自定义刷新控件
- java环境搭建及配置
- 几点建议,让Redis在你的系统中发挥更大作用
- 《Android高级进阶》小密圈开通啦
- Kth largest numbers ii
- 【cocos2d-x】CCEAGLView背景为黑色,遮挡ios中的UIView的解决办法
- Scala匿名类的函数是private还是public?
- GLFW
- SQL SERVER【非域环境】同步复制(合并)之搭建篇
- Gradle for Android 第五篇( 多模块构建 )
- Xmlspy中如何创建元素组和属性组
- (七)RabbitMQ消息队列-通过fanout模式将消息推送到多个Queue中
- Android Token的作用