LeetCode //C - 275. H-Index II

这篇文章介绍了一种在对论文引用数组进行排序后,使用二分查找算法快速计算研究人员H-指数的方法。H-指数定义为满足至少被引用次数等于其值的论文数量的最大整数。算法通过不断调整搜索边界来确定最大可能的H值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

275. H-Index II

Given an array of integers citations where citations[i] is the number of citations a researcher received for their i t h i^{th} ith paper and citations is sorted in ascending order, return the researcher’s h-index.

According to the definition of h-index on Wikipedia: The h-index is defined as the maximum value of h such that the given researcher has published at least h papers that have each been cited at least h times.

You must write an algorithm that runs in logarithmic time.
 

Example 1:

Input: citations = [0,1,3,5,6]
Output: 3
Explanation: [0,1,3,5,6] means the researcher has 5 papers in total and each of them had received 0, 1, 3, 5, 6 citations respectively.
Since the researcher has 3 papers with at least 3 citations each and the remaining two with no more than 3 citations each, their h-index is 3.

Example 2:

Input: citations = [1,2,100]
Output: 2

Constraints:
  • n == citations.length
  • 1 < = n < = 1 0 5 1 <= n <= 10^5 1<=n<=105
  • 0 <= citations[i] <= 1000
  • citations is sorted in ascending order.

From: LeetCode
Link: 275. H-Index II


Solution:

Ideas:

1. Initialization: Set the binary search bounds. left is initialized to 0, and right to citationsSize (the total number of papers).

2. Binary Search Loop:

  • Midpoint Calculation: Compute mid as the average of left and right, adjusting for integer division.
  • Condition Check: Look at the (citationsSize - mid - 1)th position in the array. This position corresponds to the (mid + 1)th last paper.
    • The index is derived from considering the mid value as the hypothesized h-index. You need mid + 1 papers with at least mid + 1 citations each.
    • By checking the paper at citationsSize - mid - 1, you are effectively checking if there are enough papers with sufficient citations to validate the hypothesized h-index.
  • Adjusting Bounds:
    • If the paper at this position has at least mid + 1 citations (citations[citationsSize - mid - 1] >= mid + 1), it suggests that it’s possible there are mid + 1 papers with at least mid + 1 citations, so the h-index could be larger. Thus, move the left boundary up to explore higher h-values.
    • If not, it means mid + 1 is too high for an h-index, so adjust right to narrow the search downwards.

3. Termination: When left meets right, the binary search has honed in on the largest possible value of h that satisfies the h-index condition. Here, left or right can be returned as they converge to the same value.

Code:
int hIndex(int* citations, int citationsSize) {
    int left = 0;
    int right = citationsSize; // This will represent the maximum possible h-index which cannot exceed the number of papers
    while (left < right) {
        int mid = left + (right - left) / 2;
        if (citations[citationsSize - mid - 1] >= mid + 1) {
            left = mid + 1; // Try for a larger h
        } else {
            right = mid; // Reduce the search space
        }
    }
    return left; // 'left' will be the maximum h-index found
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Navigator_Z

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值