Skip to content

feat: add solutions to lc problem: No.0275 #1899

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 44 additions & 2 deletions solution/0200-0299/0275.H-Index II/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,11 @@

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

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

时间复杂度 O(logn)。
因此,我们二分枚举 $h$,获取满足条件的最大 $h$。由于要满足 $h$ 篇论文至少被引用 $h$ 次,因此 $citations[n - mid] \ge mid$。

时间复杂度 $O(\log n)$,其中 $n$ 是数组 $citations$ 的长度。空间复杂度 $O(1)$。

<!-- tabs:start -->

Expand Down Expand Up @@ -130,6 +132,26 @@ func hIndex(citations []int) int {
}
```

### **Rust**

```rust
impl Solution {
pub fn h_index(citations: Vec<i32>) -> i32 {
let n = citations.len();
let (mut left, mut right) = (0, n);
while left < right {
let mid = ((left + right + 1) >> 1) as usize;
if citations[n - mid] >= mid as i32 {
left = mid;
} else {
right = mid - 1;
}
}
left as i32
}
}
```

### **TypeScript**

```ts
Expand All @@ -149,6 +171,26 @@ function hIndex(citations: number[]): number {
}
```

### **C#**

```cs
public class Solution {
public int HIndex(int[] citations) {
int n = citations.Length;
int left = 0, right = n;
while (left < right) {
int mid = (left + right + 1) >> 1;
if (citations[n - mid] >= mid) {
left = mid;
} else {
right = mid - 1;
}
}
return left;
}
}
```

### **...**

```
Expand Down
48 changes: 47 additions & 1 deletion solution/0200-0299/0275.H-Index II/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,13 @@ Since the researcher has 3 papers with at least 3 citations each and the remaini

## Solutions

Binary search.
**Solution 1: Binary Search**

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.

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$.

The time complexity is $O(\log n)$, where $n$ is the length of the array $citations$. The space complexity is $O(1)$.

<!-- tabs:start -->

Expand Down Expand Up @@ -117,6 +123,26 @@ func hIndex(citations []int) int {
}
```

### **Rust**

```rust
impl Solution {
pub fn h_index(citations: Vec<i32>) -> i32 {
let n = citations.len();
let (mut left, mut right) = (0, n);
while left < right {
let mid = ((left + right + 1) >> 1) as usize;
if citations[n - mid] >= mid as i32 {
left = mid;
} else {
right = mid - 1;
}
}
left as i32
}
}
```

### **TypeScript**

```ts
Expand All @@ -136,6 +162,26 @@ function hIndex(citations: number[]): number {
}
```

### **C#**

```cs
public class Solution {
public int HIndex(int[] citations) {
int n = citations.Length;
int left = 0, right = n;
while (left < right) {
int mid = (left + right + 1) >> 1;
if (citations[n - mid] >= mid) {
left = mid;
} else {
right = mid - 1;
}
}
return left;
}
}
```

### **...**

```
Expand Down
15 changes: 15 additions & 0 deletions solution/0200-0299/0275.H-Index II/Solution.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
public class Solution {
public int HIndex(int[] citations) {
int n = citations.Length;
int left = 0, right = n;
while (left < right) {
int mid = (left + right + 1) >> 1;
if (citations[n - mid] >= mid) {
left = mid;
} else {
right = mid - 1;
}
}
return left;
}
}
15 changes: 15 additions & 0 deletions solution/0200-0299/0275.H-Index II/Solution.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
impl Solution {
pub fn h_index(citations: Vec<i32>) -> i32 {
let n = citations.len();
let (mut left, mut right) = (0, n);
while left < right {
let mid = ((left + right + 1) >> 1) as usize;
if citations[n - mid] >= mid as i32 {
left = mid;
} else {
right = mid - 1;
}
}
left as i32
}
}