Leetcode 275. H-Index II

Problem

Given an array of integers citations where citations[i] is the number of citations a researcher received for their 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.

Algorithm

Bineary search.

Code

python3 复制代码
class Solution:
    def hIndex(self, citations: List[int]) -> int:
        K = len(citations)
        if 1 == K:
            return (citations[0] >= 1) * 1
        
        L, R, = 0, len(citations) - 1
        while L < R:
            Mid = (L + R) // 2
            if citations[Mid] >= K - Mid:
                R = Mid
            else: L = Mid + 1

        while L <= R and citations[L] < K - L:
            L += 1
        return K - L
相关推荐
0 0 08 分钟前
CCF-CSP 39-2 水印检查(watermark)【C++】
c++·算法
plus4s40 分钟前
2月15日(78,80,81题)
c++·算法·图论
能源系统预测和优化研究1 小时前
【原创改进代码】考虑碳交易与电网交互波动惩罚的共享储能电站优化配置与调度模型
算法·能源
935961 小时前
机考27 翻译21 单词14
c语言·数据结构·算法
回敲代码的猴子2 小时前
2月14日打卡
算法
blackicexs3 小时前
第四周第七天
算法
期末考复习中,蓝桥杯都没时间学了3 小时前
力扣刷题19
算法·leetcode·职场和发展
Renhao-Wan3 小时前
Java 算法实践(四):链表核心题型
java·数据结构·算法·链表
踩坑记录4 小时前
递归回溯本质
leetcode
zmzb01034 小时前
C++课后习题训练记录Day105
开发语言·c++·算法