274. H-Index
2015-12-09 09:59
351 查看
题目:
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
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.
链接: http://leetcode.com/problems/h-index/
题解:
求H-Index,找在数组中有k个元素大于等于k。排序一下再进行计算就比较容易些了。 也可以用index-couting,但这样就需要一个额外的数组。
Time Complexity - O(nlogn), Space Complexity - O(1)。
Index counting:
Time Complexity - O(n), Space Complexity - O(n)
Reference:
https://leetcode.com/discuss/55958/my-easy-solution https://leetcode.com/discuss/56041/a-clean-o-n-solution-in-java https://leetcode.com/discuss/55952/my-o-n-time-solution-use-java https://leetcode.com/discuss/55950/1-line-ruby-5-lines-c-6-lines-java https://leetcode.com/discuss/66656/java-o-n-time-with-easy-explanation
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
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.
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.
链接: http://leetcode.com/problems/h-index/
题解:
求H-Index,找在数组中有k个元素大于等于k。排序一下再进行计算就比较容易些了。 也可以用index-couting,但这样就需要一个额外的数组。
Time Complexity - O(nlogn), Space Complexity - O(1)。
public class Solution { public int hIndex(int[] citations) { if(citations == null || citations.length == 0) { return 0; } int len = citations.length; for(int i = 0; i < len; i++) { if(citations[i] >= len - i) { // check if we have len - i elements larger than len - i return len - i; } } return 0; } }
Index counting:
Time Complexity - O(n), Space Complexity - O(n)
public class Solution { public int hIndex(int[] citations) { if(citations == null || citations.length == 0) { return 0; } int len = citations.length; int[] count = new int[len + 1]; for(int num : citations) { if(num > len) { count[len]++; } else { count[num]++; } } int sum = 0; for(int i = len; i >= 0; i--) { sum += count[i]; if(sum >= i) { return i; } } return 0; } }
Reference:
https://leetcode.com/discuss/55958/my-easy-solution https://leetcode.com/discuss/56041/a-clean-o-n-solution-in-java https://leetcode.com/discuss/55952/my-o-n-time-solution-use-java https://leetcode.com/discuss/55950/1-line-ruby-5-lines-c-6-lines-java https://leetcode.com/discuss/66656/java-o-n-time-with-easy-explanation
相关文章推荐
- mysql压力测试
- Infer of facebook使用简介 Mac篇
- JavaScript知识点整理
- Some notes for Java
- stand up meeting 12/8/2015
- 图层符号化
- NHibernate 集合映射深入 (第五篇) <set>,<list>,<map>,<bag>
- 夺命雷公狗---javascript NO:17 默认行为2
- 在hadoop上执行字数统计作业
- Android Studio 2.0 FileOpUtils Not found
- 经典算法
- 百度文库制作工具
- 新书《iOS8 Swift编程指南》货架
- 对象操作
- NHibernate 基本配置 (第一篇)
- Fortran 学习Hello World
- java.io.EOFException
- lambda表达式:变量作用域
- socket选项表
- centos CDH 离线安装步骤