[LintCode] Kth Largest Element [PriorityQueue]

news/2024/7/1 4:52:53

Problem

Find K-th largest element in an array.

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.

Note

可以不要用太简单的方法。
什么和Arrays.sort()最接近?PriorityQueue.
An unbounded priority queue based on a priority heap. The elements of the priority queue are ordered according to their natural ordering, or by a Comparator provided at queue construction time, depending on which constructor is used.
A priority queue does not permit null elements.
A priority queue relying on natural ordering also does not permit insertion of non-comparable objects (doing so may result in ClassCastException).

做一个大小为k的PriorityQueue,peek()到的最顶端元素是队列中最小的元素,那么PQ就像一个自动过滤掉比顶端元素更小元素并把内部所有元素进行排序的容器。先把它装满,再和队列顶端的数字比较,大的就替换掉,小的就continue。遍历完所有元素之后,顶部的数就是第k大的数。
熟悉PriorityQueue的操作,.add(), .peek(), .remove().

Solution

1.

class Solution {
    public int kthLargestElement(int k, int[] nums) {
        Arrays.sort(nums);
        int len = nums.length;
        return nums[len - k];
    }
}

2.

class Solution {
    public int kthLargestElement(int k, int[] nums) {
        PriorityQueue<Integer> queue = new PriorityQueue<Integer>(k);
        for (int num : nums) {
            if (queue.size() < k) {
                queue.add(num);
            } else if (queue.peek() < num) {
                queue.remove();
                queue.add(num);
            }
        }
        return queue.peek();
    }
}

http://www.niftyadmin.cn/n/1046433.html

相关文章

win7 64 eclipse 导入hadoop2.6.0 源码

Software&#xff1a;链接: http://pan.baidu.com/s/1jHba8To 密码: t8ti一、安装ant、maven&#xff08;1&#xff09;首先下载ant&#xff0c;maven的安装包apache-ant-1.9.4-bin.zipapache-maven-3.3.9-bin.zip&#xff08;2&#xff09;配置环境变量添加系统环境变量ANT_HO…

CSS left 属性

定义和用法left 属性规定元素的左边缘。该属性定义了定位元素左外边距边界与其包含块左边界之间的偏移。注释&#xff1a;如果 "position" 属性的值为 "static"&#xff0c;那么设置 "left" 属性不会产生任何效果。说明对于 static 元素&#xf…

mysql 64 zip download

open the url :: http://dev.mysql.com/downloads/file/?id461109 and click the location "no thanks..." 转载于:https://www.cnblogs.com/rocky-fang/p/5190501.html

openstack 网络

物理节点hosts解析配置 neutron网络 根据多套云平台架构搭建及运维知--openstack的管理网络流量非常较大&#xff01;&#xff01;&#xff01; <二.>nova-network

plsql提示列快捷键_plsql 常用快捷键(自动替换)

plsql 常用快捷键CreateTime--2018年4月23日17:33:05Author:Marydon说明&#xff1a;这里的快捷键&#xff0c;不同于以往的快捷键&#xff0c;输入指定字符&#xff0c;按快捷键&#xff0c;可以自动替换成你所配置的指定内容iiINSERT INTOinsINSERTupdUPDATEselSELECTfroFROM…

微信小程序js数组初始化_微信小程序常用赋值方法小结

本文实例讲述了微信小程序常用赋值方法。分享给大家供大家参考&#xff0c;具体如下&#xff1a;1.微信小程序将值赋值给局部变量: ""实例&#xff1a;var nameoptions.goodsName2.微信小程序将值赋值给全局变量: "" 或 this.setData({ })实例&#xff1a;…

access怎么查询工龄_ACCESS查询操作题及答案详解.doc

ACCESS查询操作题及答案详解2&#xff0e;简单应用题在考生文件夹下有“xxx.mdb”数据库。(1)以雇员表为数据源&#xff0c;创建查询“查询1”&#xff0c;查询职务为“销售主管”的雇员信息。结果显示雇员的全部字段。(2)以工资表为数据源&#xff0c;创建参数更新查询“工资调…

js 小数自动补0_在js中做数字字符串补0(js补零)

通常遇到的一个问题是日期的“1976-02-03 HH:mm:ss”这种格式 &#xff0c;我的比较简单的处理方法是这样&#xff1a;function formatDate(d) {var D[00,01,02,03,04,05,06,07,08,09]with (d || new Date) return [[getFullYear(), D[getMonth()1]||getMonth()1, D[getDate()]…