Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit ef47ea0

Browse files
authored
feat: add solutions to lc problem: No.0275 (doocs#1899)
No.0275.H-Index II
1 parent 4bfdb7a commit ef47ea0

File tree

4 files changed

+121
-3
lines changed

4 files changed

+121
-3
lines changed

solution/0200-0299/0275.H-Index II/README.md

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,11 @@
4646

4747
**方法一:二分查找**
4848

49-
二分枚举 h,获取满足条件的最大 h。由于要满足 h 篇论文至少被引用 h 次,因此 `citations[n - mid] >= mid`
49+
我们注意到,如果有至少 $x$ 篇论文的引用次数大于等于 $x$,那么对于任意 $y \lt x$,其引用次数也一定大于等于 $y$。这存在着单调性
5050

51-
时间复杂度 O(logn)。
51+
因此,我们二分枚举 $h$,获取满足条件的最大 $h$。由于要满足 $h$ 篇论文至少被引用 $h$ 次,因此 $citations[n - mid] \ge mid$。
52+
53+
时间复杂度 $O(\log n)$,其中 $n$ 是数组 $citations$ 的长度。空间复杂度 $O(1)$。
5254

5355
<!-- tabs:start -->
5456

@@ -130,6 +132,26 @@ func hIndex(citations []int) int {
130132
}
131133
```
132134

135+
### **Rust**
136+
137+
```rust
138+
impl Solution {
139+
pub fn h_index(citations: Vec<i32>) -> i32 {
140+
let n = citations.len();
141+
let (mut left, mut right) = (0, n);
142+
while left < right {
143+
let mid = ((left + right + 1) >> 1) as usize;
144+
if citations[n - mid] >= mid as i32 {
145+
left = mid;
146+
} else {
147+
right = mid - 1;
148+
}
149+
}
150+
left as i32
151+
}
152+
}
153+
```
154+
133155
### **TypeScript**
134156

135157
```ts
@@ -149,6 +171,26 @@ function hIndex(citations: number[]): number {
149171
}
150172
```
151173

174+
### **C#**
175+
176+
```cs
177+
public class Solution {
178+
public int HIndex(int[] citations) {
179+
int n = citations.Length;
180+
int left = 0, right = n;
181+
while (left < right) {
182+
int mid = (left + right + 1) >> 1;
183+
if (citations[n - mid] >= mid) {
184+
left = mid;
185+
} else {
186+
right = mid - 1;
187+
}
188+
}
189+
return left;
190+
}
191+
}
192+
```
193+
152194
### **...**
153195

154196
```

solution/0200-0299/0275.H-Index II/README_EN.md

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

4040
## Solutions
4141

42-
Binary search.
42+
**Solution 1: Binary Search**
43+
44+
We notice that if there are at least $x$ papers with citation counts greater than or equal to $x$, then for any $y \lt x$, its citation count must also be greater than or equal to $y$. This exhibits monotonicity.
45+
46+
Therefore, we use binary search to enumerate $h$ and obtain the maximum $h$ that satisfies the condition. Since we need to satisfy that $h$ papers are cited at least $h$ times, we have $citations[n - mid] \ge mid$.
47+
48+
The time complexity is $O(\log n)$, where $n$ is the length of the array $citations$. The space complexity is $O(1)$.
4349

4450
<!-- tabs:start -->
4551

@@ -117,6 +123,26 @@ func hIndex(citations []int) int {
117123
}
118124
```
119125

126+
### **Rust**
127+
128+
```rust
129+
impl Solution {
130+
pub fn h_index(citations: Vec<i32>) -> i32 {
131+
let n = citations.len();
132+
let (mut left, mut right) = (0, n);
133+
while left < right {
134+
let mid = ((left + right + 1) >> 1) as usize;
135+
if citations[n - mid] >= mid as i32 {
136+
left = mid;
137+
} else {
138+
right = mid - 1;
139+
}
140+
}
141+
left as i32
142+
}
143+
}
144+
```
145+
120146
### **TypeScript**
121147

122148
```ts
@@ -136,6 +162,26 @@ function hIndex(citations: number[]): number {
136162
}
137163
```
138164

165+
### **C#**
166+
167+
```cs
168+
public class Solution {
169+
public int HIndex(int[] citations) {
170+
int n = citations.Length;
171+
int left = 0, right = n;
172+
while (left < right) {
173+
int mid = (left + right + 1) >> 1;
174+
if (citations[n - mid] >= mid) {
175+
left = mid;
176+
} else {
177+
right = mid - 1;
178+
}
179+
}
180+
return left;
181+
}
182+
}
183+
```
184+
139185
### **...**
140186

141187
```
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
public class Solution {
2+
public int HIndex(int[] citations) {
3+
int n = citations.Length;
4+
int left = 0, right = n;
5+
while (left < right) {
6+
int mid = (left + right + 1) >> 1;
7+
if (citations[n - mid] >= mid) {
8+
left = mid;
9+
} else {
10+
right = mid - 1;
11+
}
12+
}
13+
return left;
14+
}
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
impl Solution {
2+
pub fn h_index(citations: Vec<i32>) -> i32 {
3+
let n = citations.len();
4+
let (mut left, mut right) = (0, n);
5+
while left < right {
6+
let mid = ((left + right + 1) >> 1) as usize;
7+
if citations[n - mid] >= mid as i32 {
8+
left = mid;
9+
} else {
10+
right = mid - 1;
11+
}
12+
}
13+
left as i32
14+
}
15+
}

0 commit comments

Comments
 (0)