[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>& ...
随机推荐
- RedHat7搭建无人值守自动安装Linux操作系统(PXE+Kickstart)
Kickstart服务器 IP: 192.168.136.253 掩码:255.255.255.0 网关:192.168.136.2 DNS:192.168.136.2 安装部署HTTP服 ...
- 第六篇:web之python框架之django
python框架之django python框架之django 本节内容 web框架 mvc和mtv模式 django流程和命令 django URL django views django te ...
- oracle 11g 安装及网络配置
非原创,纯属笔记 安装:基本按照默认下一步安装的 1)可执行安装文件[ setup.exe ]双击安装 2):配置安全更新,取消下面的“我希望通过My Oracle Support接受安全更新(W)” ...
- [视频转换] C#VideoConvert视频转换帮助类 (转载)
点击下载 VideoConvert.zip 主要功能如下 .获取文件的名字 .获取文件扩展名 .获取文件类型 .视频格式转为Flv .生成Flv视频的缩略图 .转换文件并保存在指定文件夹下 .转换文件 ...
- jmeter中webdriver插件,进行自动化压测
1.下载JMeterPlugins-WebDriver-1.1.2 2.将JMeterPlugins-WebDriver-1.1.2\lib\ext中的*.jar拷贝到D:\apache-jmeter ...
- ASCII 码表对照
ASCII码表 ASCII码大致可以分作三部分组成.第一部分是:ASCII非打印控制字符第二部分是:ASCII打印字符:第三部分是:扩展ASCII打印字符 第一部分:ASCII非打印控制字符表 ASC ...
- 在.Net中进行跨线程的控件操作(上篇:Control.Invoke)
本文的重点在于介绍如何在多线程编程中,从非UI线程上访问界面中的控件.有过多线程编程经验的人都知道,当我们在非UI线程上试图给一个界面中的控件赋值的时候,比如说label的Text属性,系统会抛出一个 ...
- 【实习记】2014-08-23网络安全XSS与CSRF总结
XSS:脚本中的不速之客XSS:跨站脚本(Cross-site scripting)CSRF:冒充用户之手CSRF:跨站请求伪造(Cross-site request forgery) 谷歌搜 ...
- ajax只是一个称呼
记得刚入行的时候,看到ajax,即异步的javascript和xml这样一个概念,一点感觉都没有.参加工作前的第一轮面试,被问到有没有自己实现过ajax,我觉得自己实现肯定很复杂吧. 从名字理解 从名 ...
- npm install express -g出错
npm ERR! Windows_NT npm ERR! argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Program ...