LeetCode刷题 (Python) | 275. H-Index II
来源:互联网 发布:淘宝第三层级交易额 编辑:程序博客网 时间:2024/06/14 01:53
题目链接
https://leetcode.com/problems/h-index-ii/
心得
274.H-Index的后续题目,假定了数组已经是有序的。按照上一篇博客的做法,对每一个可能的h值进去遍历判断,则时间复杂度为
回到h的定义:在数组中找到一个数字H,使得数组中至少有H个数字大于等于H,其余的数字小于等于H。如果这个数组是有序数组,则这个定义可以进一步变成:
如果h符合定义,则
其中
进一步思考,如果h不符合定义,会是什么情况:
有了这个公式之后,当我们判断完一个h值,我们就知道h应该往大还是往小。就可以用二分的方法来找到这个h值。
AC代码
class Solution(object): def hIndex(self, citations): """ :type citations: List[int] :rtype: int """ start, end = 1, len(citations) while start <= end: h = int((start+end)/2) if citations[len(citations)-h] < h: # h应该更小 end = h-1 elif len(citations)-h-1>=0 and citations[len(citations)-h-1] > h: # h应该更大 start = h+1 else: return h return 0if __name__ == '__main__': s = Solution() array = [1,1] array.sort() print(s.hIndex(array))
遗留问题
题目中提到:
Note: If there are several possible values for h, the maximum one is taken as the h-index.
但是用二分法的话,就有可能出现较小的h值被取得,然后程序就返回的情况。
如果避免这个问题?还是说这个问题根本不会出现?请指教。
0 0
- LeetCode刷题 (Python) | 275. H-Index II
- [leetcode] 275.H-Index II
- 275. H-Index II LeetCode
- leetcode 275. H-Index II
- LeetCode *** 275. H-Index II
- leetcode.275. H-Index II
- LeetCode - 275. H-Index II
- [leetcode] 275. H-Index II
- LeetCode 275. H-Index II
- 【LeetCode】 275. H-Index II
- Leetcode 275. H-Index II
- [LeetCode]275. H-Index II
- leetcode 275. H-Index II
- leetcode 275. H-Index II
- 【LEETCODE】275-H-Index II [Python]
- Leetcode: H-Index II
- Leetcode: H-Index II
- [LeetCode]H-Index II
- 数组名和数组名取地址& 的区别
- Hadoop copyToLocalFile报空指针错误
- 挑战NPC
- nyoj 1057
- 编写应用程序消息
- LeetCode刷题 (Python) | 275. H-Index II
- The Java™ Tutorials — Concurrency :前言
- Linux查看端口占用情况和开启端口命令
- 非常好用的颜色吸取器PicPick
- [HTTP]HTTP协议的状态码
- 297. Serialize and Deserialize Binary Tree
- 大数定理与中心极限定理
- 半小时还原原有Windows System
- JDK8 中的类型推断与重载解析