H-Index Total Accepted: 3191 Total Submissions: 14098
来源:互联网 发布:java中string转map 编辑:程序博客网 时间:2024/06/05 08:51
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 citations = [3, 0, 6, 1, 5]
, which means the researcher has 5
papers in total and each of them had received 3, 0, 6, 1, 5
citations respectively. Since the researcher has 3
papers with at least 3
citations each and the remaining two with no more than 3
citations each, his h-index is 3
.
Note: If there are several possible values for h
, the maximum one is taken as the h-index.
Hint:
- An easy approach is to sort the array first.
- What are the possible values of h-index?
- A faster approach is to use extra space.
Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.
class Solution {public: int hIndex(vector<int>& citations) { vector<int> countArray(citations.size()+1,0); for(int i=0;i<citations.size();++i) { if(citations[i]>=citations.size()) ++countArray[citations.size()]; else ++countArray[citations[i]]; } int ans=0; if(countArray[citations.size()]>=citations.size() ) return citations.size(); for(int j=citations.size()-1;j>=0;--j) { countArray[j]+=countArray[j+1]; if(ans<j&&countArray[j]>=j) ans=j; } return ans; } };
2。O(nlgn)的算法
class Solution {public: int hIndex(vector<int>& citations) { int ans=0; sort(citations.begin(),citations.end()); for(int i=0;i<citations.size();++i) { if(citations[i]>=citations.size()-i) { if(ans<citations.size()-i) ans=citations.size()-i; } } return ans; }};
Follow up for H-Index: What if the citations
array is sorted in ascending order? Could you optimize your algorithm?
Hint:
- Expected runtime complexity is in O(log n) and the input is sorted.
class Solution {public: int hIndex(vector<int>& citations) { int ans=0; for(int i=0;i<citations.size();++i) { if(citations[i]>=citations.size()-i) { if(ans<citations.size()-i) ans=citations.size()-i; } } return ans; }};
- H-Index Total Accepted: 3191 Total Submissions: 14098
- leetcode: Total Accepted: 4338 Total Submissions: 20460
- Path Sum Total Accepted: 50216 Total Submissions: 168425
- Minimum Path Sum Total Accepted: 34975 Total Submissions: 109325
- Count Primes Total Accepted: 831 Total Submissions: 6167
- Remove Duplicates from Sorted List Total Accepted: 40004 Total Submissions: 116304
- Bitwise AND of Numbers Range Total Accepted: 2859 Total Submissions: 11145
- Populating Next Right Pointers in Each Node Total Accepted: 46429 Total Submissions: 128383
- Flatten Binary Tree to Linked List Total Accepted: 45093 Total Submissions: 156588
- poj 3617 贪心 不难 容易错 Total Submissions: 8060Accepted: 2489
- leetcode Intersection of Two Linked Lists My Submissions Question Solution Total Accepted: 43555 To
- Language: Cable master Time Limit: 1000MSMemory Limit: 10000K Total Submissions: 46653Accepted:
- Excuses, Excuses! Time Limit: 1000MSMemory Limit: 10000K Total Submissions: 4346Accepted: 1497 D
- total
- poj3641Pseudoprime numbers Time Limit: 1000MSMemory Limit: 65536K Total Submissions: 8854Accepte
- qmail total
- Total Commander
- Total Commander
- 制作Javascript弹出窗口技巧九则
- js 格式化时间日期函数小结
- (&a+1)
- iOS时间戳与标准时间转换
- Codeforces Gym 100342H Hard Test 构造
- H-Index Total Accepted: 3191 Total Submissions: 14098
- Java Code review 一些原则的原因探讨
- 对软件架构设计的一些总结和理解
- Linux下安装JDK
- SQLServer中获取特定表的所有列名
- Spring4整合Junit4 测试
- Git 使用 Sublime Text 为默认编辑器
- VC使用GSOAP调用C#WCF服务
- python 去掉 pyc