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
相关推荐
aichitang202439 分钟前
矩阵详解:从基础概念到实际应用
线性代数·算法·矩阵
OpenCSG1 小时前
电子行业AI赋能软件开发经典案例——某金融软件公司
人工智能·算法·金融·开源
chao_7892 小时前
链表题解——环形链表 II【LeetCode】
数据结构·leetcode·链表
dfsj660112 小时前
LLMs 系列科普文(14)
人工智能·深度学习·算法
薛定谔的算法3 小时前
《盗梦空间》与JavaScript中的递归
算法
kaiaaaa3 小时前
算法训练第十一天
数据结构·算法
?!7143 小时前
算法打卡第18天
c++·算法
springfe01013 小时前
构建大顶堆
前端·算法
凌辰揽月4 小时前
Web后端基础(基础知识)
java·开发语言·前端·数据库·学习·算法
lifallen4 小时前
深入浅出 Arrays.sort(DualPivotQuicksort):如何结合快排、归并、堆排序和插入排序
java·开发语言·数据结构·算法·排序算法