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
相关推荐
好学且牛逼的马6 小时前
【Hot100 | 2 LeetCode49 字母异位词分组问题】
算法
2301_795167207 小时前
Rust 在内存安全方面的设计方案的核心思想是“共享不可变,可变不共享”
算法·安全·rust
czhc11400756637 小时前
Java117 最长公共前缀
java·数据结构·算法
java 乐山7 小时前
蓝牙网关(备份)
linux·网络·算法
云泽8087 小时前
快速排序算法详解:hoare、挖坑法、lomuto前后指针与非递归实现
算法·排序算法
数字化脑洞实验室7 小时前
智能决策算法的核心原理是什么?
人工智能·算法·机器学习
流烟默7 小时前
机器学习中拟合、欠拟合、过拟合是什么
人工智能·算法·机器学习
Brianna Home7 小时前
现代C++:从性能泥潭到AI基石
开发语言·c++·算法
再卷也是菜7 小时前
算法基础篇(10)递归型枚举与回溯剪枝
算法·深度优先·剪枝
吃着火锅x唱着歌7 小时前
LeetCode 2016.增量元素之间的最大差值
数据结构·算法·leetcode