博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Leetcode: H-Index
阅读量:6209 次
发布时间:2019-06-21

本文共 2303 字,大约阅读时间需要 7 分钟。

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.

Hint:

  1. An easy approach is to sort the array first.
  2. What are the possible values of h-index?
  3. A faster approach is to use extra space.

 

Sort first then scan: O(NlogN) time, O(1)space

use another array: O(N) time, O(N) space(有点像counting sort)

我们额外使用一个大小为N+1的数组stats。stats[i]表示有多少文章被引用了i次,这里如果一篇文章引用大于N次,我们就将其当为N次,因为H指数不会超过文章的总数。为了构建这个数组,我们需要先将整个文献引用数组遍历一遍,对相应的格子加一。统计完后,我们从N向1开始遍历这个统计数组。如果遍历到某一个引用次数时,大于或等于该引用次数的文章数量,大于等于引用次数本身时,我们可以认为这个引用次数是H指数。这里引用次数是连续的,而书本数量不连续,所以用引用次数来表示H指数(如底下code中的j)

 we can then easily locate his h-index by scanning from right (L) to left (0). By definition, index k is his h-index if the summation of all elements fromcounts[k] to counts[L] is no less than k.  

(重要思路)Think in this way, the summation is the number of books with citatations at least k each, summation>=k means we are guaranteed to have k books with citation at least k each, the other books won't have k citations, so index k is 

之所以不用再向下找,因为我们要取最大的H指数。那如何求大于或等于某个引用次数的文章数量呢?我们可以用一个变量,从高引用次的文章数累加下来。因为我们知道,如果有x篇文章的引用大于等于3次,那引用大于等于2次的文章数量一定是x加上引用次数等于2次的文章数量。

1 public class Solution { 2     public int hIndex(int[] citations) { 3         int N = citations.length; 4         int[] summary = new int[N+1]; 5         for (int m : citations) { 6             if (m <= N) summary[m]++; 7             else summary[N]++; 8         } 9         int sum = 0;10         for (int j=summary.length-1; j>=0; j--) {11             sum += summary[j]; //sum means # of papers that has citation greater than or equal to j12             if (sum >= j) return j;13         }14         return 0;15     }16 }

 

转载地址:http://nvzja.baihongyu.com/

你可能感兴趣的文章
学习笔记之-------UIScrollView 基本用法 代理使用
查看>>
如何理解运维
查看>>
Dom学习笔记
查看>>
JAVA流程控制学习总结
查看>>
配置yum,nc,telnet
查看>>
nib和xib的区别
查看>>
== 和 is 的区别
查看>>
Apple Swift编程语言新手教程
查看>>
linux服务器加入windows域时报错Ticket expired
查看>>
2018年,牛客网小白月赛5
查看>>
sql 锁
查看>>
生死相依:说说JQuery中die()、live()详解[翻译]
查看>>
UML建模工具Visio 、Rational Rose、PowerDesign的比较
查看>>
压缩映象原理的一个应用
查看>>
一个疑难故障,坑了我半年青春-----知识就是生产力
查看>>
javascript控制页面(含iframe进行页面跳转)跳转、刷新的方法汇总
查看>>
JAVA常见算法题(九)
查看>>
leetcode解题文件夹
查看>>
MySQL复制原理-加强版
查看>>
dotnet 命令实战
查看>>