[LeetCode] 274. H-Index H指数
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."
Example:
Input:citations = [3,0,6,1,5]
Output: 3
Explanation:[3,0,6,1,5]means the researcher has5papers in total and each of them had
received3, 0, 6, 1, 5citations respectively.
Since the researcher has3papers with at least3citations each and the remaining
two with no more than3citations each, her h-index is3.
Note: If there are several possible values for h, the maximum one is taken as the h-index.
H指数(H index)是一个混合量化指标,可用于评估研究人员的学术产出数量与学术产出水平
可以按照如下方法确定某人的H指数:
将其发表的所有SCI论文按被引次数从高到低排序;
从前往后查找排序后的列表,直到某篇论文的序号大于该论文被引次数。所得序号减一即为H指数。
解法1: 先将数组排序,T:O(nlogn), S:O(1)。然后对于每个引用次数,比较大于该引用次数的文章,取引用次数和文章数的最小值,即 Math.min(citations.length-i, citations[i]),并更新 level,取最大值。排好序之后可以用二分查找进行遍历,这样速度会更快,可见:275. H-Index II H指数 II
解法2: Counting sort,T:O(n), S:O(n)。使用一个大小为 n+1 的数组count统计引用数,对于count[i]表示的是引用数为 i 的文章数量。从后往前遍历数组,当满足 count[i] >= i 时,i 就是 h 因子,返回即可,否则返回0。
为什么要从后面开始遍历? 为什么 count[i] >= i 时就返回?
一方面引用数引用数大于 i-1 的数量是i-1及之后的累加,必须从后往前遍历。另一方面,h 因子要求尽可能取最大值,而 h 因子最可能出现最大值的地方在后面,往前值只会越来越小,能尽快返回就尽快返回,所以一遇到 count[i] >= i 就返回。参考:Code_Granker
Java:
public class Solution {
public int hIndex(int[] citations) {
Arrays.sort(citations);
int level = 0;
for(int i = 0; i < citations.length; i++)
level = Math.max(level,Math.min(citations.length - i,citations[i]));
return level;
}
}
Java:
public class Solution {
public int hIndex(int[] citations) {
int n = citations.length;
int[] count = new int[n + 1];
for(int c : citations)
if(c >= n) count[n]++; //当引用数大于等于 n 时,都计入 count[n]中
else count[c]++;
for(int i = n; i > 0; i--) { //从后面开始遍历
if(count[i] >= i) return i;
count[i-1] += count[i]; //引用数大于 i-1 的数量是i-1及之后的累加
}
return 0;
}
}
Python: Counting sort.
class Solution(object):
def hIndex(self, citations):
"""
:type citations: List[int]
:rtype: int
"""
n = len(citations);
count = [0] * (n + 1)
for x in citations:
# Put all x >= n in the same bucket.
if x >= n:
count[n] += 1
else:
count[x] += 1 h = 0
for i in reversed(xrange(0, n + 1)):
h += count[i]
if h >= i:
return i
return h
Python: T: O(nlogn) O: O(1)
class Solution2(object):
def hIndex(self, citations):
"""
:type citations: List[int]
:rtype: int
"""
citations.sort(reverse=True)
h = 0
for x in citations:
if x >= h + 1:
h += 1
else:
break
return h
Python: T: O(nlogn) O: O(n)
class Solution3(object):
def hIndex(self, citations):
"""
:type citations: List[int]
:rtype: int
"""
return sum(x >= i + 1 for i, x in enumerate(sorted(citations, reverse=True)))
Python:
class Solution(object):
def hIndex(self, citations):
"""
:type citations: List[int]
:rtype: int
"""
if not citations: return 0
return max([min(i + 1, c) for i, c in enumerate(sorted(citations, reverse=True))])
C++:
class Solution {
public:
int hIndex(vector<int>& citations) {
sort(citations.begin(), citations.end(), greater<int>());
for (int i = 0; i < citations.size(); ++i) {
if (i >= citations[i]) return i;
}
return citations.size();
}
};
类似题目:
[LeetCode] 275. H-Index II H指数 II
All LeetCode Questions List 题目汇总
[LeetCode] 274. H-Index H指数的更多相关文章
- [LeetCode] 275. H-Index II H指数 II
Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimize ...
- [LeetCode] H-Index II 求H指数之二
Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimize ...
- 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 274
H-Index Given an array of citations (each citation is a non-negative integer) of a researcher, write ...
- [LeetCode#274]H-Index
Problem: Given an array of citations (each citation is a non-negative integer) of a researcher, writ ...
- Sed命令n,N,d,D,p,P,h,H,g,G,x解析3
摘自:https://blog.csdn.net/WMSOK/article/details/78463199 Sed命令n,N,d,D,p,P,h,H,g,G,x解析 2017年11月06日 23: ...
- sed命令n,N,d,D,p,P,h,H,g,G,x解析2
摘自: https://blog.csdn.net/xiexingshishu/article/details/50514132 sed命令n,N,d,D,p,P,h,H,g,G,x解析 2016年0 ...
- oc 与 swift 之间的桥接文件 (ProjectNmae-Bridging-Header.h) (ProjectNmae-Swift.h)
oc 与 Swift 是2种不同的语言, oc代码只能写带oc文件里, Swift代码只能写在Swift文件里, 虽然2者不同语言, 但却能互相调用, 不过需要进行一下桥接, 就是下面的2个文件 (P ...
- 86-Money Flow Index 资金流量指数指标.(2015.7.3)
Money Flow Index 资金流量指数指标 计算: 1.典型价格(TP)=当日最高价.最低价与收盘价的算术平均值 2.货币流量(MF)=典型价格(TP)×N日内成交金额 3.如果当日MF> ...
随机推荐
- c和c++区别(未整理)
学习完C语言和c++比较一下他们之间的区别: c++是c语言的基础上开发的一种面向对象的编程语言,应用十分广泛,按理说c++可以编译任何c的程序,但是两者还是有细微的差别. c++在c的基础上添加了类 ...
- Beta之前-凡事预则立(校园帮-追光的人)
所属课程 软件工程1916 作业要求 Beta之前-凡事预则立 团队名称 追光的人 作业目标 在Beta冲刺之前,提前做好准备和规划 议题 1.讨论组长是否重选的议题和结论. 2.下一阶段需要改进完善 ...
- python开发笔记-DataFrame的使用
今天详细做下关于DataFrame的使用,以便以后自己可以翻阅查看 DataFrame的基本特征: 1.是一个表格型数据结构 2.含有一组有序的列 3.大致可看成共享同一个index的Series集合 ...
- Windows 2008R2 定时备份PostgreSQL 11.6及还原操作
PostgreSQL 自动备份,并删除10天前的备份文件. 第一步,创建脚本,命名back.bat文件,可直接点击执行或者CMD执行此批处理命令. @ECHO OFF @setlocal enable ...
- 纯js制作九宫格
Demo实现了对任意方格进行拖拽,可以交换位置,其中Demo-1利用了勾股定理判断距离! Demo-1整体思路: 1.首先div实现自由移动,一定需要脱离标准文档流,所以我们给它使用绝对定位. 2.利 ...
- 使用go-mysql-server 开发自己的mysql server
go-mysql-server是一个golang 的mysql server 协议实现包,使用此工具我们可以用来做好多方便的东西 基于mysql 协议暴露自己的本地文件为sql 查询 基于mysql ...
- cyyz: Day 4 网络流整理
Day 4 网络流的理论性知识(算了..我自己都看不下去,还是整理些例题以后复习用吧qaq): 一.PPT(主要内容) 二.搜自度娘 定义: 年,L.R. 福特和 D.R. 富尔克森等人给出了解决 ...
- 缺失值处理(Missing Values)
什么是缺失值?缺失值指数据集中某些变量的值有缺少的情况,缺失值也被称为NA(not available)值.在pandas里使用浮点值NaN(Not a Number)表示浮点数和非浮点数组中的缺失值 ...
- mysql 修改表名
//重命名表 rename table table1 to table2; //重命名多个表 rename table table1 to table2,table3 to table4,table5 ...
- Echarts 入门操作
Echarts具有丰富的图表,可以说是数据可视化的神器: 1.下载Echarts 到官网或者点击以下文字[下载Echarts]即可下载: ①官网下载地址:https://echarts.baidu.c ...