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
相关推荐
故事和你913 小时前
sdut-程序设计基础Ⅰ-实验五一维数组(8-13)
开发语言·数据结构·c++·算法·蓝桥杯·图论·类和对象
像污秽一样3 小时前
算法与设计与分析-习题4.2
算法·排序算法·深度优先·dfs·bfs
Storynone4 小时前
【Day20】LeetCode:39. 组合总和,40. 组合总和II,131. 分割回文串
python·算法·leetcode
明明如月学长4 小时前
AI 更新太快学不过来?我用OpenClaw打造专属AI学习工作流
算法
黎阳之光4 小时前
【黎阳之光:以无线专网与视频孪生,赋能智慧广电与数字中国】
算法·安全·智慧城市·数字孪生
刀法如飞5 小时前
Agentic AI时代,程序员必备的算法思想指南
人工智能·算法·agent
刀法如飞6 小时前
Agentic AI时代程序员必备算法思想详解(附实战案例)
算法·ai编程·编程开发·agentic
飞Link6 小时前
告别盲目找Bug:深度解析 TSTD 异常检测中的预测模型(Python 实战版)
开发语言·python·算法·bug
记忆多7 小时前
c++名字空间 函数模版 左右值
开发语言·c++·算法
三伏5227 小时前
控制理论前置知识——相平面数学基础2(示例部分)
算法·平面·控制