[LeetCode#274]H-Index
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的更多相关文章
- Java实现 LeetCode 274 H指数
274. H指数 给定一位研究者论文被引用次数的数组(被引用次数是非负整数).编写一个方法,计算出研究者的 h 指数. h 指数的定义: "h 代表"高引用次数"(hig ...
- Leetcode 274.H指数
H指数 给定一位研究者论文被引用次数的数组(被引用次数是非负整数).编写一个方法,计算出研究者的 h 指数. h 指数的定义: "一位有 h 指数的学者,代表他(她)的 N 篇论文中至多有 ...
- [LeetCode] 274. H-Index H指数
Given an array of citations (each citation is a non-negative integer) of a researcher, write a funct ...
- 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 ...
- [LeetCode] Random Pick Index 随机拾取序列
Given an array of integers with possible duplicates, randomly output the index of a given target num ...
- LeetCode 274
H-Index Given an array of citations (each citation is a non-negative integer) of a researcher, write ...
- 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 ...
- [LeetCode] Find Pivot Index 寻找中枢点
Given an array of integers nums, write a method that returns the "pivot" index of this arr ...
- LeetCode 852. Peak Index in a Mountain Array C++ 解题报告
852. Peak Index in a Mountain Array -- Easy 方法一:二分查找 int peakIndexInMountainArray(vector<int>& ...
随机推荐
- 项目打包 weblogic部署
工作流打包: 由于没有集成单点,配置文件要修改 将webnocas.xml内容复制覆盖 web.xml 到这里修改完毕 选择weblogic项目,右键maven install,生成.ear文 ...
- 什么时候用using (SPSite site = new SPSite(SPContext.Current.Web.Url))
并不是所有时候都适合用using(){},只有当需要提升用户的权限的时候才会用到using,其他时候都可以直接使用SPContext.Current.Web; using ...
- 8第八章CTE递归及分组汇总高级部分(多维数据集)(转载)
8第八章CTE递归及分组汇总高级部分(多维数据集) 这里贴图太麻烦...算了 UNION 等集合操作符: UNION 等以第一个 SELECT 的 列明 作为 整个结果集的列明,整个结果集 唯一认可 ...
- [HttpException (0x80004005): Failed to Execute URL.]之画蛇添足之痛
最近很悲惨,发布的一个mvc站点,所有的静态内容,如js.css.图片都不能正常加载,服务器给出的响应是一个如下的异常黄页: Server Error in '/ua' Application.Fai ...
- wamp使用方法【总】
Apache与php配置:我们把php-5.2.9-Win32.zip解压到C盘根目录下,把文件夹名字改成PHP,这样方便一下. 1. 找到PHP目录下的“php.ini-dist”或者“php.in ...
- ios专题 - OCUnit
OCUnit是集成在Xcode开发环境的单元测试框架:OCUnit运行必须包含SenTestingKit.framework这个库: 针对需要测试的类,每个类写出自己的TestCase,独立组织一个文 ...
- 自己学习过程中关于以后有可能用到的技术的备份,微信广告滑屏组件 iSlider
转载: iSlider 是个非常平滑的滑块,支持移动端 WebApp,HTML5App 和混合型的 App. iSlider是移动端的滑动组件的最佳解决方案.他和普通的web 端的滑动插件有很大不同, ...
- Qt中使用信号和槽的一点心得
信号(Signal)与槽(Slot)-Qt中的典型机制 这一篇文章中都说得很详细了,这里不再重复,只说一点在实际使用中可能会遇到的问题. 1.一个信号不要同时连接几个槽函数,不然执行的顺序是随机的,最 ...
- PC110305/UVA10188
根据我的规律,每天solved3题就感觉不行了~但是今天好像做水题做上瘾了,不过PC的题目尽管水,水得还是可以让人有进步. 这题OJ自动测评真心坑,题目看起来十分简单,测评返回三种可能: Accept ...
- 【JAVA集合】HashMap源码分析(转载)
原文出处:http://www.cnblogs.com/chenpi/p/5280304.html 以下内容基于jdk1.7.0_79源码: 什么是HashMap 基于哈希表的一个Map接口实现,存储 ...