博客
关于我
【Leetcode】275. H-Index II
阅读量:201 次
发布时间:2019-02-28

本文共 867 字,大约阅读时间需要 2 分钟。

题目地址:

题意是,给定一个单调增非负数组,找出满足这样性质的数 h h h:有多于等于 h h h个数是大于等于 h h h的。返回满足这样条件的最大的那个 h h h

思路是二分。首先考虑解的范围。显然 0 0 0是满足条件的,并且 h h h最大不超过数组长度 n n n,否则的话,要存在多于 n + 1 n+1 n+1个大于等于 n + 1 n+1 n+1的数,超出数组长度了,是不可能的。接下来 h h h有这样的性质:如果 h h h满足条件,那么 0 , . . . , h − 1 0,...,h-1 0,...,h1也满足条件。这为二分创造了条件。注意到 h h h满足条件,等价于数组倒数第 h h h个数是大于等于 h h h的,这就是判断条件。代码如下:

public class Solution {       public int hIndex(int[] citations) {           int l = 0, r = citations.length;        while (l < r) {               int m = l + (r - l + 1 >> 1);            // 判断倒数第m个数是不是大于等于m            if (citations[citations.length - m] >= m) {               	// 如果是,那么m满足条件,            	// 由于要找最大的满足条件的数,所以向右搜索                l = m;            } else {               	// 否则m不满足条件,需要向左搜索。                r = m - 1;            }        }                return l;    }}

时间复杂度 O ( log ⁡ n ) O(\log n) O(logn)

转载地址:http://flbs.baihongyu.com/

你可能感兴趣的文章
nginx+php的搭建
查看>>
nginx+tomcat+memcached
查看>>
Nginx+Tomcat实现动静分离
查看>>
nginx+Tomcat性能监控
查看>>
nginx+uwsgi+django
查看>>
nginx+vsftp搭建图片服务器
查看>>
Nginx-http-flv-module流媒体服务器搭建+模拟推流+flv.js在前端html和Vue中播放HTTP-FLV视频流
查看>>
nginx-vts + prometheus 监控nginx
查看>>
Nginx/Apache反向代理
查看>>
Nginx: 413 – Request Entity Too Large Error and Solution
查看>>
nginx: [emerg] getpwnam(“www”) failed 错误处理方法
查看>>
nginx: [emerg] the “ssl“ parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/nginx.conf:
查看>>
nginx:Error ./configure: error: the HTTP rewrite module requires the PCRE library
查看>>
Nginx:objs/Makefile:432: recipe for target ‘objs/src/core/ngx_murmurhash.o‘解决方法
查看>>
Nginx、HAProxy、LVS
查看>>
Nginx下配置codeigniter框架方法
查看>>
Nginx中使用expires指令实现配置浏览器缓存
查看>>
nginx中配置root和alias的区别
查看>>
nginx主要流程(未完成)
查看>>
Nginx之二:nginx.conf简单配置(参数详解)
查看>>