leetcode-- H-Index I & II -- 重点常考
2015-12-22 11:11
288 查看
H-Index
https://leetcode.com/problems/h-index/参考http://bookshadow.com/weblog/2015/09/03/leetcode-h-index/
思路1
用hash table 统计,key 为citation count,这里的key是设定好的[0,N], 即所有h value的可能值。 value为有多少篇paper有这些citation count。然后从后向前扫,直到sum[-i:] >= i就return.class Solution(object): def hIndex(self, citations): """ :type citations: List[int] :rtype: int """ N = len(citations) cnts = [0] * (N + 1) for c in citations: cnts[[c, N][c > N]] += 1 sums = 0 for h in range(N, 0, -1): if sums + cnts[h] >= h: return h sums += cnts[h] return 0
思路2
线reverse排序,然后从左到右扫到index >= nums[i]就行H-Index II
https://wenyuanjiao.gitbooks.io/algorithm_practice/content/h-index_ii.html思路类似于H-index I中从右往左scan找到sum[-i:] >= i就return.
len - mid就是有多少paper的citations >= citations[mid]
琢磨一下bs的终止条件. 当i > j, 即input为[0,0]的时候
class Solution(object): def hIndex(self, citations): """ :type citations: List[int] :rtype: int """ N = len(citations) low, high = 0, N - 1 while low <= high: mid = (low + high) / 2 if citations[mid] == N - mid: return N - mid elif N - mid > citations[mid]: low = mid + 1 else: high = mid - 1 return N - low
相关文章推荐
- surfaceview缓存问题
- nginx服务器安装及配置文件详解
- 一篇文章入门Python生态系统
- 判断手机号码是否符合规则
- tableview去除多余分割线问题
- memcache的总结
- Docker学习一
- 【jQuery】检测两个节点的包含关系
- servlet 跳转到 jsp 乱码解决
- linux抓包工具tcpdump
- 【HDOJ】4162 Shape Number
- Word Pattern pattern = "abba", str = "dog cat cat dog" should return true
- WinForm窗体键盘事件,支持方向键和回车键
- 用TC、按键、易语言接入百宝云开发注册码系统
- hdoj2012水题
- Foundation框架中的NSArray类和NSMutableArray类
- 类名前自动添加前缀
- ListView分批加载数据
- Android中调用startActivity结果导致:java.lang.RuntimeException: Unable to start activity ComponentInfo{xxx}
- 打造Android一体式轮播广告条