Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 41ff95d

Browse files
feat: add solutions to lc problem: No. 0274.H-Index
1 parent 704af65 commit 41ff95d

File tree

4 files changed

+182
-3
lines changed

4 files changed

+182
-3
lines changed

‎solution/0200-0299/0274.H-Index/README.md‎

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
<pre>
2020
<strong>输入:</strong><code>citations = [3,0,6,1,5]</code>
21-
<strong>输出:</strong>3
21+
<strong>输出:</strong>3
2222
<strong>解释:</strong>给定数组表示研究者总共有 <code>5</code> 篇论文,每篇论文相应的被引用了 <code>3, 0, 6, 1, 5</code> 次。
2323
由于研究者有 <code>3 </code>篇论文每篇 <strong>至少 </strong>被引用了 <code>3</code> 次,其余两篇论文每篇被引用 <strong>不多于</strong> <code>3</code> 次,所以她的 <em>h </em>指数是 <code>3</code>。</pre>
2424

@@ -31,22 +31,91 @@
3131

3232
<!-- 这里可写通用的实现逻辑 -->
3333

34+
最简单的解法就是排序之后再判断,但是因为 `H` 不可能大于论文的总数 `n`,所以可以用计数排序进行优化。
35+
3436
<!-- tabs:start -->
3537

3638
### **Python3**
3739

3840
<!-- 这里可写当前语言的特殊实现逻辑 -->
3941

4042
```python
41-
43+
class Solution:
44+
def hIndex(self, citations: List[int]) -> int:
45+
n = len(citations)
46+
cnt = [0 for i in range(n + 1)]
47+
for c in citations:
48+
if c <= n:
49+
cnt[c] += 1
50+
else:
51+
cnt[n] += 1
52+
sum = 0
53+
for i in range(n, -1, -1):
54+
sum += cnt[i]
55+
if sum >= i:
56+
return i
57+
return 0
4258
```
4359

4460
### **Java**
4561

4662
<!-- 这里可写当前语言的特殊实现逻辑 -->
4763

4864
```java
65+
class Solution {
66+
public int hIndex(int[] citations) {
67+
int n = citations.length;
68+
int[] cnt = new int[n + 1];
69+
for (int c : citations) {
70+
if (c <= n) {
71+
++cnt[c];
72+
} else {
73+
++cnt[n];
74+
}
75+
}
76+
int sum = 0;
77+
for (int i = n; i >= 0; --i) {
78+
sum += cnt[i];
79+
if (sum >= i) {
80+
return i;
81+
}
82+
}
83+
return 0;
84+
}
85+
}
86+
```
4987

88+
### **Go**
89+
90+
利用二分查找,定位符合条件的最大值
91+
92+
```go
93+
func hIndex(citations []int) int {
94+
n := len(citations)
95+
left, right := 0, n
96+
for left+1 < right {
97+
mid := int(uint(left+right) >> 1)
98+
if check(citations, mid) {
99+
left = mid
100+
} else {
101+
right = mid
102+
}
103+
}
104+
if check(citations, right) {
105+
return right
106+
}
107+
return left
108+
}
109+
110+
func check(citations []int, mid int) bool {
111+
cnt := 0
112+
for _, citation := range citations {
113+
if citation >= mid {
114+
cnt++
115+
}
116+
}
117+
return cnt >= mid
118+
}
50119
```
51120

52121
### **...**

‎solution/0200-0299/0274.H-Index/README_EN.md‎

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,87 @@ Since the researcher has 3 papers with at least 3 citations each and the remaini
3939

4040
## Solutions
4141

42+
The simplest solution is to judge after sort, but because `H` cannot be greater than the total number of papers, it can be optimized by counting sort.
43+
4244
<!-- tabs:start -->
4345

4446
### **Python3**
4547

4648
```python
47-
49+
class Solution:
50+
def hIndex(self, citations: List[int]) -> int:
51+
n = len(citations)
52+
cnt = [0 for i in range(n + 1)]
53+
for c in citations:
54+
if c <= n:
55+
cnt[c] += 1
56+
else:
57+
cnt[n] += 1
58+
sum = 0
59+
for i in range(n, -1, -1):
60+
sum += cnt[i]
61+
if sum >= i:
62+
return i
63+
return 0
4864
```
4965

5066
### **Java**
5167

5268
```java
69+
class Solution {
70+
public int hIndex(int[] citations) {
71+
int n = citations.length;
72+
int[] cnt = new int[n + 1];
73+
for (int c : citations) {
74+
if (c <= n) {
75+
++cnt[c];
76+
} else {
77+
++cnt[n];
78+
}
79+
}
80+
int sum = 0;
81+
for (int i = n; i >= 0; --i) {
82+
sum += cnt[i];
83+
if (sum >= i) {
84+
return i;
85+
}
86+
}
87+
return 0;
88+
}
89+
}
90+
```
5391

92+
### **Go**
93+
94+
Use binary search to locate the maximum value that meets the conditions
95+
96+
```go
97+
func hIndex(citations []int) int {
98+
n := len(citations)
99+
left, right := 0, n
100+
for left+1 < right {
101+
mid := int(uint(left+right) >> 1)
102+
if check(citations, mid) {
103+
left = mid
104+
} else {
105+
right = mid
106+
}
107+
}
108+
if check(citations, right) {
109+
return right
110+
}
111+
return left
112+
}
113+
114+
func check(citations []int, mid int) bool {
115+
cnt := 0
116+
for _, citation := range citations {
117+
if citation >= mid {
118+
cnt++
119+
}
120+
}
121+
return cnt >= mid
122+
}
54123
```
55124

56125
### **...**
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
func hIndex(citations []int) int {
2+
n := len(citations)
3+
left, right := 0, n
4+
for left+1 < right {
5+
mid := int(uint(left+right) >> 1)
6+
if check(citations, mid) {
7+
left = mid
8+
} else {
9+
right = mid
10+
}
11+
}
12+
if check(citations, right) {
13+
return right
14+
}
15+
return left
16+
}
17+
18+
func check(citations []int, mid int) bool {
19+
cnt := 0
20+
for _, citation := range citations {
21+
if citation >= mid {
22+
cnt++
23+
}
24+
}
25+
return cnt >= mid
26+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution:
2+
def hIndex(self, citations: List[int]) -> int:
3+
n = len(citations)
4+
cnt = [0 for i in range(n + 1)]
5+
for c in citations:
6+
if c <= n:
7+
cnt[c] += 1
8+
else:
9+
cnt[n] += 1
10+
sum = 0
11+
for i in range(n, -1, -1):
12+
sum += cnt[i]
13+
if sum >= i:
14+
return i
15+
return 0

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /