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
相关推荐
xiaoye-duck12 分钟前
《算法题讲解指南:递归,搜索与回溯算法--综合练习》--14.找出所有子集的异或总和再求和,15.全排列Ⅱ,16.电话号码的字母组合,17.括号生成
c++·算法·深度优先·回溯
OOJO12 分钟前
c++---vector介绍
c语言·开发语言·数据结构·c++·算法·vim·visual studio
汀、人工智能14 分钟前
05 - 函数基础
数据结构·算法·数据库架构·05 - 函数基础
HAPPY酷31 分钟前
Python高级架构师之路——从原理到实战
java·python·算法
枫叶林FYL1 小时前
第9章 因果推理与物理理解
人工智能·算法·机器学习
小白zlm1 小时前
预畸变双线性变换
单片机·嵌入式硬件·算法·电机控制
wuweijianlove2 小时前
算法复杂度估算的实验建模与可视化表达的技术6
算法
执笔画流年呀2 小时前
7大排序算法
java·算法·排序算法
AI成长日志2 小时前
【算法学习专栏】动态规划基础·中等两题精讲(198.打家劫舍、322.零钱兑换)
学习·算法·动态规划
计算机安禾2 小时前
【数据结构与算法】第28篇:平衡二叉树(AVL树)
开发语言·数据结构·数据库·线性代数·算法·矩阵·visual studio