H-Index I, II
2015-09-24 06:20
316 查看
I. Given an array of citations (each citation is a non-negative integer) of a researcher, write a function to compute the researcher's h-index.
According to the definition of h-index on Wikipedia: "A scientist has index h if h of his/her N papers have at least h citations each, and the other N − h papers have no more than h citations each."
For example, given
Note: If there are several possible values for
Runtime: 4ms.
II. Follow up for H-Index: What if the
Runtime: 12ms.
According to the definition of h-index on Wikipedia: "A scientist has index h if h of his/her N papers have at least h citations each, and the other N − h papers have no more than h citations each."
For example, given
citations = [3, 0, 6, 1, 5], which means the researcher has
5papers in total and each of them had received
3, 0, 6, 1, 5citations respectively. Since the researcher has
3papers with at least
3citations each and the remaining two with no more than
3citations each, his h-index is
3.
Note: If there are several possible values for
h, the maximum one is taken as the h-index.
Runtime: 4ms.
class Solution { public: int hIndex(vector<int>& citations) { int n = citations.size(); if(n == 0) return 0; int i = 1; sort(citations.begin(), citations.end()); while(i <= n){ while(citations[n - i] >= i) i++; return i - 1; } return citations[0]; } };
II. Follow up for H-Index: What if the
citationsarray is sorted in ascending order? Could you optimize your algorithm?
Runtime: 12ms.
class Solution { public: int hIndex(vector<int>& citations) { int n = citations.size(); if(n == 0) return 0; int low = 0, high = n - 1; while(low <= high){ int mid = (low + high) / 2; if(n - mid <= citations[mid]) high = mid - 1; else low = mid + 1; } return n - low; } };
相关文章推荐
- "分享:围观龙虎斗:谷歌GCE
- 简洁的for(3)的define
- 云计算学习
- "Nginx服务器在使用HTML5
- "Vcenter
- 分享:高访问量Web应用跨云端迁移的原因:权限和易用性
- 关于hadoop调度问题
- 网络防火墙策略限制定位问题
- Double类parseDouble和valueOf方法的区别
- 提问:从EBS卷中删除Marketplace代码
- "分享:AWS
- **Word Break
- 安装单机Hadoop时格式化HDFS出现问题
- "Hadoop集群运行时总是抛出Child
- Hadoop的Map和Reduce中能不能打开HDFS文件系统中的文件?
- *LeetCode-Single Number III
- Minimum Depth of Binary Tree 解答
- *LeetCode-Single Number II
- OC 数组以及字符串拼接与分割
- iOS presentViewController背景色透明