博客
关于我
【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/

你可能感兴趣的文章
Netty工作笔记0046---TaskQueue自定义任务
查看>>
Netty工作笔记0046---异步模型原理剖析
查看>>
Netty工作笔记0047---Http服务程序实例
查看>>
Netty工作笔记0048---Http服务过滤资源
查看>>
Netty工作笔记0049---阶段内容梳理
查看>>
Netty工作笔记0050---Netty核心模块1
查看>>
Netty工作笔记0051---Netty核心模块2
查看>>
Netty工作笔记0052---Pipeline组件剖析
查看>>
Netty工作笔记0053---Netty核心模块梳理
查看>>
Netty工作笔记0054---EventLoop组件
查看>>
Netty工作笔记0055---Unpooled应用实例1
查看>>
Netty工作笔记0056---Unpooled应用实例2
查看>>
Netty工作笔记0057---Netty群聊系统服务端
查看>>
Netty工作笔记0058---Netty群聊系统客户端
查看>>
Netty工作笔记0059---Netty私聊实现思路
查看>>
Netty工作笔记0060---Netty心跳机制实例
查看>>
Netty工作笔记0060---Tcp长连接和短连接_Http长连接和短连接_UDP长连接和短连接
查看>>
Netty工作笔记0061---Netty心跳处理器编写
查看>>
Netty工作笔记0062---WebSocket长连接开发
查看>>
Netty工作笔记0063---WebSocket长连接开发2
查看>>