Leetcode 275. H-Index II

Problem

Given an array of integers citations where citationsi 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
相关推荐
Zane199417 分钟前
冒泡、选择、插入排序都是O(n²),希尔排序凭什么说自己能更快
算法·排序算法
余额瞒着我当琳1 小时前
C++多态深入解析:多态概念,多态实现条件,虚函数表与内存分布,常见问题及注意事项
c++·算法
维克兜率天1 小时前
【维克】模块3总结:从一行空数据,到一个能跑的模型
python·深度学习·算法
飞Link1 小时前
定积分理论与 Python 仿真完全指南
python·算法
闲研随记2 小时前
RL算法学习:ArgMaxRL
算法·llm·强化学习·rl
飞Link2 小时前
零基础:离散数据积分与 Python 实现保姆级教程
python·算法
圣保罗的大教堂2 小时前
leetcode 836. 矩形重叠 简单
leetcode
淡海水2 小时前
11-03-Unity-List-T-和Dictionary-TKey-TValue-的性能调优实战
算法·unity·c#·list·dictionary
土司大王2 小时前
LeetCode hot100——394.字符串解码:Java 双栈模拟
java·算法·leetcode