diff --git a/solution/0200-0299/0275.H-Index II/README.md b/solution/0200-0299/0275.H-Index II/README.md index 3543d8d226294..de10e53602ac8 100644 --- a/solution/0200-0299/0275.H-Index II/README.md +++ b/solution/0200-0299/0275.H-Index II/README.md @@ -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)$。 @@ -130,6 +132,26 @@ func hIndex(citations []int) int { } ``` +### **Rust** + +```rust +impl Solution { + pub fn h_index(citations: Vec) -> 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 @@ -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; + } +} +``` + ### **...** ``` diff --git a/solution/0200-0299/0275.H-Index II/README_EN.md b/solution/0200-0299/0275.H-Index II/README_EN.md index 461f685e16e2f..cfb0868c9b8fd 100644 --- a/solution/0200-0299/0275.H-Index II/README_EN.md +++ b/solution/0200-0299/0275.H-Index II/README_EN.md @@ -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)$. @@ -117,6 +123,26 @@ func hIndex(citations []int) int { } ``` +### **Rust** + +```rust +impl Solution { + pub fn h_index(citations: Vec) -> 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 @@ -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; + } +} +``` + ### **...** ``` diff --git a/solution/0200-0299/0275.H-Index II/Solution.cs b/solution/0200-0299/0275.H-Index II/Solution.cs new file mode 100644 index 0000000000000..6b17ee4bc3758 --- /dev/null +++ b/solution/0200-0299/0275.H-Index II/Solution.cs @@ -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; + } +} \ No newline at end of file diff --git a/solution/0200-0299/0275.H-Index II/Solution.rs b/solution/0200-0299/0275.H-Index II/Solution.rs new file mode 100644 index 0000000000000..caf02b391eb7f --- /dev/null +++ b/solution/0200-0299/0275.H-Index II/Solution.rs @@ -0,0 +1,15 @@ +impl Solution { + pub fn h_index(citations: Vec) -> 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 + } +}