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. HTML+CSS基础学习笔记(6)

    一.元素分类 CSS中html的标签元素大体分为三种类型 1.块状元素 @特点: #每个块级元素都从新的一行开始,并且其后的元素也另起一行(一个块级元素独占一行) #元素的高度.宽度.行高以及顶和底边 ...

  2. .NET 操作PDF文档以及PDF文件打印摸索总结

    关于生成 PDF 的操作,相信大家的在实际的工作过程中难免会碰到.以前我们通过生成 word 文档来进行文件的打印,但是由于太过依赖 office 软件,因此尝试能不能使用 PDF 进行文件打印. 在 ...

  3. Linq转成sql后的分页方法

    sql 分页 -- Region Parametersdeclare @pageindex int set @pageindex=2set @pagesize=10 DECLARE @p0 Int = ...

  4. SET QUOTED_IDENTIFIER OFF语句的作用

    先看下面几个sql语句  1 SET QUOTED_IDENTIFIER ON 2 SELECT * FROM "USER"    WHERE a='netasp'  3  4 S ...

  5. google code 上传源码

    在使用google code 的时候 做个备份, git clone https://wushuangzilong@code.google.com/p/maplebanana-proxy/ git c ...

  6. 组策略彻底解决windows 2003 终端数

         win2003的话可以从组策略修改: 组策略级别要高于终端服务配置,当启用组策略后终端服务配置中的相应选项会变成灰色不可修改 运行-gpedit.msc-计算机配置-管理模板-Windows ...

  7. ERROR ITMS-90167: "No .app bundles found in the package"

    http://stackoverflow.com/questions/37838487/error-itms-90167-no-app-bundles-found-in-the-package 简单说 ...

  8. Microsoft Visual Studio 2010 遇到了异常,可能是由某个扩展导致的。 转载

    问题: 今天打开好久没用的Microsoft Visual Studio 2010 ,刚才创建了一个C++工程,错误就出现了. 只要在VS2010源码编辑器中输入一个字符,它就报错 ":Mi ...

  9. iOS 异常汇总

    reason: 'invalid nib registered for identifier (cell) - nib must contain exactly one top level objec ...

  10. 使用APT减少MVP的冗余代码

    前言 不知道从何时起,移动端开发都开始采用MVP.我们在认识到MVP有点的时候,也不妨会察觉到它其实也有很多恼人的地方,比如,我们针对每种状态渲染不同的视图: private void renderI ...