comments | difficulty | edit_url | tags | ||
---|---|---|---|---|---|
true |
中等 |
|
给你一个整数数组 citations
,其中 citations[i]
表示研究者的第 i
篇论文被引用的次数,citations
已经按照 升序排列 。计算并返回该研究者的 h 指数。
h 指数的定义:h 代表“高引用次数”(high citations),一名科研人员的 h
指数是指他(她)的 (n
篇论文中)至少 有 h
篇论文分别被引用了至少 h
次。
请你设计并实现对数时间复杂度的算法解决此问题。
示例 1:
输入:citations = [0,1,3,5,6]
输出:3
解释:给定数组表示研究者总共有5
篇论文,每篇论文相应的被引用了0, 1, 3, 5, 6
次。 由于研究者有3
篇论文每篇 至少 被引用了3
次,其余两篇论文每篇被引用 不多于3
次,所以她的 h 指数是3
。
示例 2:
输入:citations = [1,2,100]
输出:2
提示:
n == citations.length
1 <= n <= 105
0 <= citations[i] <= 1000
citations
按 升序排列
我们注意到,如果有至少
因此,我们二分枚举
时间复杂度
class Solution:
def hIndex(self, citations: List[int]) -> int:
n = len(citations)
left, right = 0, n
while left < right:
mid = (left + right + 1) >> 1
if citations[n - mid] >= mid:
left = mid
else:
right = mid - 1
return left
class Solution {
public int hIndex(int[] citations) {
int n = citations.length;
int left = 0, right = n;
while (left < right) {
int mid = (left + right) >>> 1;
if (citations[mid] >= n - mid) {
right = mid;
} else {
left = mid + 1;
}
}
return n - left;
}
}
class Solution {
public:
int hIndex(vector<int>& citations) {
int n = citations.size();
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;
}
};
func hIndex(citations []int) int {
n := len(citations)
left, right := 0, n
for left < right {
mid := (left + right + 1) >> 1
if citations[n-mid] >= mid {
left = mid
} else {
right = mid - 1
}
}
return left
}
function hIndex(citations: number[]): number {
const n = citations.length;
let left = 0,
right = n;
while (left < right) {
const mid = (left + right + 1) >> 1;
if (citations[n - mid] >= mid) {
left = mid;
} else {
right = mid - 1;
}
}
return left;
}
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
}
}
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;
}
}