Skip to content

Commit 687a2f7

Browse files
committed
H-Index, H-Index II
1 parent 05b82cf commit 687a2f7

7 files changed

Lines changed: 138 additions & 2 deletions

File tree

‎java/SUMMARY.md‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,13 @@
123123
* [Merge Two Sorted Lists](sorting/merge-sort/merge-two-sorted-lists.md)
124124
* [Merge k Sorted Lists](sorting/merge-sort/merge-k-sorted-lists.md)
125125
* [Sort List](sorting/merge-sort/sort-list.md)
126-
* [桶排序](sorting/bucket-sort/README.md)
127-
* [First Missing Positive](sorting/bucket-sort/first-missing-positive.md)
128126
* [快速排序](sorting/quick-sort/README.md)
129127
* [Sort Colors](sorting/quick-sort/sort-colors.md)
130128
* [Kth Largest Element in an Array](sorting/quick-sort/kth-largest-element-in-an-array.md)
129+
* [桶排序](sorting/bucket-sort/README.md)
130+
* [First Missing Positive](sorting/bucket-sort/first-missing-positive.md)
131+
* [计数排序](sorting/counting-sort/README.md)
132+
* [H-Index](sorting/counting-sort/h-index.md)
131133
* [基数排序](sorting/radix-sort/README.md)
132134
* [Maximum Gap](sorting/radix-sort/maximum-gap.md)
133135
* [其他](sorting/other/README.md)
@@ -143,6 +145,7 @@
143145
* [Find Minimum in Rotated Sorted Array](search/find-minimum-in-rotated-sorted-array.md)
144146
* [Find Minimum in Rotated Sorted Array II](search/find-minimum-in-rotated-sorted-array-ii.md)
145147
* [Median of Two Sorted Arrays](search/median-of-two-sorted-arrays.md)
148+
* [H-Index II](search/h-index-ii.md)
146149
* [暴力枚举法](brute-force/README.md)
147150
* [Subsets](brute-force/subsets.md)
148151
* [Subsets II](brute-force/subsets-ii.md)

‎java/code/h-index-1.java‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// H-Index
2+
// Time complexity: O(nlogn), Space complexity: O(1)
3+
public class Solution {
4+
public int hIndex(int[] citations) {
5+
Arrays.sort(citations);
6+
reverse(citations);
7+
for (int i = 0; i < citations.length; ++i) {
8+
if (i + 1 == citations[i]) return i+1;
9+
if (i + 1 > citations[i]) return i;
10+
}
11+
return citations.length;
12+
}
13+
private static void reverse(int[] nums) {
14+
int left = 0;
15+
int right = nums.length - 1;
16+
while (left < right) {
17+
final int tmp = nums[left];
18+
nums[left] = nums[right];
19+
nums[right] = tmp;
20+
++left;
21+
--right;
22+
}
23+
}
24+
}

‎java/code/h-index-2.java‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// H-Index
2+
// Time complexity: O(n), Space complexity: O(n)
3+
public class Solution {
4+
public int hIndex(int[] citations) {
5+
final int n = citations.length + 1;
6+
final int[] histogram = new int[n+1];
7+
8+
for (int x : citations) {
9+
++histogram[x > n ? n : x];
10+
}
11+
12+
int sum = 0; // current number of papers
13+
for (int i = n; i > 0; --i) {
14+
sum += histogram[i];
15+
if (sum >= i) {
16+
return i;
17+
}
18+
}
19+
return 0;
20+
}
21+
}

‎java/code/h-index-ii.java‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// H-Index II
2+
// Time complexity: O(logn), Space complexity: O(1)
3+
public class Solution {
4+
public int hIndex(int[] citations) {
5+
final int n = citations.length;
6+
int begin = 0;
7+
int end = citations.length;
8+
9+
while (begin < end) {
10+
final int mid = begin + (end - begin) / 2;
11+
if (citations[mid] < n - mid) {
12+
begin = mid + 1;
13+
} else {
14+
end = mid;
15+
}
16+
}
17+
return n - begin;
18+
}
19+
}

‎java/search/h-index-ii.md‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
## H-Index II
2+
3+
4+
### 描述
5+
6+
**Follow up** for [H-Index](../sorting/counting-sort/h-index.md): What if the citations array is sorted in ascending order? Could you optimize your algorithm?
7+
8+
9+
### 分析
10+
11+
设数组长度为`n`,那么`n-i`就是引用次数大于等于`nums[i]`的文章数。如果`nums[i]<n-i`,说明`i`是有效的H-Index, 如果一个数是H-Index,那么最大的H-Index一定在它后面(因为是升序的),根据这点就可以进行二分搜索了。
12+
13+
14+
### 代码
15+
16+
{% if book.java %}
17+
{% codesnippet "./code/h-index-ii."+book.suffix, language=book.suffix %}{% endcodesnippet %}
18+
{% endif %}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
**计数排序**(Counting Sort)是一种O(n)的排序算法,其思路是开一个长度为`maxValue-minValue+1`的数组,然后
2+
3+
1. 分配。扫描一遍原始数组,以当前值-`minValue`作为下标,将该下标的计数器增1。
4+
1. 收集。扫描一遍计数器数组,按顺序把值收集起来。
5+
6+
举个例子,`nums=[2, 1, 3, 1, 5]`, 首先扫描一遍获取最小值和最大值,`maxValue=5`, `minValue=1`,于是开一个长度为5的计数器数组`counter`,
7+
8+
1. 分配。统计每个元素出现的频率,得到`counter=[2, 1, 1, 0, 1]`,例如`counter[0]`表示值`0+minValue=1`出现了2次。
9+
2. 收集。`counter[0]=2`表示`1`出现了两次,那就向原始数组写入两个1,`counter[1]=1`表示`2`出现了1次,那就向原始数组写入一个2,依次类推,最终原始数组变为`[1,1,2,3,5]`,排序好了。
10+
11+
计数排序本质上是一种特殊的桶排序,当桶的个数最大的时候,就是计数排序。
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
## H-Index
2+
3+
4+
### 描述
5+
6+
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.
7+
8+
According to the [definition of h-index on Wikipedia](https://en.wikipedia.org/wiki/H-index): "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."
9+
10+
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.
11+
12+
**Note**: If there are several possible values for `h`, the maximum one is taken as the h-index.
13+
14+
15+
### 分析
16+
17+
H-Index的含义是,如果一个人发表的所有论文中,有`h`篇论文分别被引用了至少`h`次,那么他的H-Index就是`h`。
18+
19+
思路一:先从大到小排序,然后从前往后扫描,如果当前文章数(即当前下标+1)等于值本身,则返回当前文章数作为 h-index;如果当前文章数大于值本身,则返回当前文章数-1作为H-Index, 因为当前文章的引用数小于当前文章数,不能算在内。时间复杂度$$O(n\log{}n$$,空间复杂度O(1)。
20+
21+
思路二:跟思路一类似,不过排序算法换成了计数排序。有一个小技巧,因为H-Index最大不可能超过论文综述,所以我们只需要开一个长度为`n+1`的数组,如果某篇论文的引用数超过了`n`,就将其当做`n`。
22+
23+
24+
### 代码1 全排序
25+
26+
{% if book.java %}
27+
{% codesnippet "./code/h-index-1."+book.suffix, language=book.suffix %}{% endcodesnippet %}
28+
{% endif %}
29+
30+
31+
### 代码2 计数排序
32+
33+
{% if book.java %}
34+
{% codesnippet "./code/h-index-2."+book.suffix, language=book.suffix %}{% endcodesnippet %}
35+
{% endif %}
36+
37+
38+
### 相关题目
39+
40+
* [H-Index II](../../search/h-index-ii.md)

0 commit comments

Comments
 (0)