Problem:

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.

Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.

Analysis:

This problem is interesting!!! It tests your coding skill and logic ability.

Since h-index defines that at least h papers should exceed(include) h citation, you may wrongly think this is a simple count problem, why not use HashMap<citation, count>.
However, even paper that have citation' larger than citation should be counted! What a pity, Right?
Apparently the HashMap should not be used!!! Since there is good prperty : all papers have citation exceed certain index, could be counted for that index. Why not use sort??? Then, sorting the citation array in descending order, (i+1) is the total citations exceed citation[i].
Then I have following implementations:

Wrong solution 1:

public class Solution {
public int hIndex(int[] citations) {
if (citations == null)
throw new IllegalArgumentException("The citaions' reference is null!");
Arrays.sort(citations, Collections.reverseOrder());
for (int i = 0; i < citations.length; i++) {
if (i+1 >= citations[i])
return citations[i];
}
return citations.length;
}
}
Error:
Line 5: error: no suitable method found for sort(int[],Comparator<Object>)
Mistake 1:
Arrays.sort(citations, Collections.reverseOrder());
Not work for primitive type, it only works for Integer, Double ....

Wrong solution 2:

Since we should give up the way of sorting citations in descending order, we should just use ascending order.
For the citation in ascending order, citation[i]'s useful count is the number of papers after it (inclusive).
for (int i = 0; i < citations.length; i++) {
if (citations.length - i >= citations[i])
...
} public class Solution {
public int hIndex(int[] citations) {
if (citations == null)
throw new IllegalArgumentException("The citaions' reference is null!");
Arrays.sort(citations);
int max = -1;
for (int i = 0; i < citations.length; i++) {
if (citations.length - i >= citations[i])
max = Math.max(max, citations[i]);
}
return (max == -1 ? citations.length : max);
}
} Errors:
Input:
[4,4,0,0]
Output:
0
Expected:
2 However, the above solution only consider the situation of "citations.length - i >= citations[i]" and no citation[i] is valid case (at the end).
Even we may not be able to find citations[i] meet:
if (citations.length - i >= citations[i])
max = Math.max(max, citations[i]); We still should have a valid h-index!
Suppose we have no "citations.length - i >= citations[i]" case, it means
citations.length - i < citations[i] since "citations[i]"" < "citations[citaions.length - i]"(thus all citations[i] account into citations[citaions.length - i]).
Thus we must have (possible)hindex = citations.length - i.

Solution:

public class Solution {
public int hIndex(int[] citations) {
if (citations == null)
throw new IllegalArgumentException("The citaions' reference is null!");
Arrays.sort(citations);
int max = 0;
for (int i = 0; i < citations.length; i++) {
if (citations.length - i >= citations[i])
max = Math.max(max, citations[i]);
else
max = Math.max(max, citations.length - i);
}
return max;
}
}

[LeetCode#274]H-Index的更多相关文章

  1. Java实现 LeetCode 274 H指数

    274. H指数 给定一位研究者论文被引用次数的数组(被引用次数是非负整数).编写一个方法,计算出研究者的 h 指数. h 指数的定义: "h 代表"高引用次数"(hig ...

  2. Leetcode 274.H指数

    H指数 给定一位研究者论文被引用次数的数组(被引用次数是非负整数).编写一个方法,计算出研究者的 h 指数. h 指数的定义: "一位有 h 指数的学者,代表他(她)的 N 篇论文中至多有 ...

  3. [LeetCode] 274. H-Index H指数

    Given an array of citations (each citation is a non-negative integer) of a researcher, write a funct ...

  4. leetcode@ [274/275] H-Index & H-Index II (Binary Search & Array)

    https://leetcode.com/problems/h-index/ Given an array of citations (each citation is a non-negative ...

  5. [LeetCode] Random Pick Index 随机拾取序列

    Given an array of integers with possible duplicates, randomly output the index of a given target num ...

  6. LeetCode 274

    H-Index Given an array of citations (each citation is a non-negative integer) of a researcher, write ...

  7. LeetCode 599. Minimum Index Sum of Two Lists (从两个lists里找到相同的并且位置总和最靠前的)

    Suppose Andy and Doris want to choose a restaurant for dinner, and they both have a list of favorite ...

  8. [LeetCode] Find Pivot Index 寻找中枢点

    Given an array of integers nums, write a method that returns the "pivot" index of this arr ...

  9. LeetCode 852. Peak Index in a Mountain Array C++ 解题报告

    852. Peak Index in a Mountain Array -- Easy 方法一:二分查找 int peakIndexInMountainArray(vector<int>& ...

随机推荐

  1. Linux查看当前系统登录用户、登录日志、登录错误日志

    1.查看当前系统的登录用户 w who 2.查看成功登录历史记录 last -n 3.查看尝试登录失败的历史记录 lastb -n 4.显示每个用户最近一次登录成功的信息 lastlog

  2. mysql-distinct去重、mysql-group&nbsp;…

    一.MYSQL-distinct用法 在使用mysql时,有时需要查询出某个字段不重复的记录,虽然mysql提供有distinct这个关键字来过滤掉多余的重复记录只保留一条,但往往只用它来返回不重复记 ...

  3. ASP.NET项目中使用CKEditor +CKFinder 实现上传图片

    CKEditor是什么 CKEidtor是一个在线富文本编辑器,可以将让用户所见即所得的获得编辑在线文本,编辑器或自动将用户编辑的文字格式转换成html代码. 在ASP.NET工程中添加CKEdito ...

  4. 浅析Activity不可见与透明

    http://blog.csdn.net/lincyang/article/details/6868582 看见标题也许你会有疑问,不可见和透明不是一个意思吗? 从字面上看,这还真是差不多.但在Act ...

  5. 一个自定义线程池的小Demo

    在项目中如果是web请求时候,IIS会自动分配一个线程来进行处理,如果很多个应用程序共享公用一个IIS的时候,线程分配可能会出现一个问题(当然也是我的需求造成的) 之前在做项目的时候,有一个需求,就是 ...

  6. 网页快照 - C#实现

    /// <summary> /// 图片类型枚举 /// </summary> public enum ImageType { GIF = , JPG = , PNG = } ...

  7. angular2 组件之间通讯-使用服务通讯模式 2016.10.27 基于正式版ng2

    工作中用到ng2的组件通讯 奈何官方文档言简意赅 没说明白 自己搞明白后 整理后分享下 rxjs 不懂的看这篇文章 讲很详细 http://www.open-open.com/lib/view/ope ...

  8. vs2013 使用vs调试器,发现调试器显示的数据错误。查看内存,发现内存是正确的。

    有可能只是调试器的问题,程序可以正常运行的! 网上没找到此种情况解释.感觉有可能是那里堆被破坏了.

  9. 生成dll文件的示例

    看了好多网上写的关于dll文件生成和实用的资料发现多尔不全,都是抄来抄去,有的干脆就是搬用msdn上的原文,实在没有创意和可看的东西.于是本着学和实用的目的自己实践的东西分享给大家. 大前提:使用VS ...

  10. Windows phone 之Xml操作

    最近在做天气预报应用,这个应用中需要用到Xml来存储关注的城市列表,保存一下代码,方便以后使用,也给博友们一个参考: 其中:添加关注城市的操作代码是: 其实就是, (1)先把数据从CareCityCo ...