LeetCode每日一题——275. H-Index II

文章目录

一、题目

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.

Example 1:

Input: citations = [0,1,3,5,6]

Output: 3

Explanation: [0,1,3,5,6] means the researcher has 5 papers in total and each of them had received 0, 1, 3, 5, 6 citations respectively.

Since the researcher has 3 papers with at least 3 citations each and the remaining two with no more than 3 citations each, their h-index is 3.

Example 2:

Input: citations = [1,2,100]

Output: 2

Constraints:

n == citations.length

1 <= n <= 105

0 <= citations[i] <= 1000

citations is sorted in ascending order.

二、题解

cpp 复制代码
class Solution {
public:
    int hIndex(vector<int>& citations) {
        int n = citations.size();
        int left = 0, right = n - 1;
        while(left <= right){
            int mid = (left + right) >> 1;
            int ret = n - mid;
            if(citations[mid] >= ret) right = mid - 1;
            else if(citations[mid] < ret) left = mid + 1;
        }
        return n - left;
    }
};
相关推荐
mjhcsp9 小时前
C++ 背包DP解析
开发语言·c++
重生之后端学习9 小时前
78. 子集
java·数据结构·算法·职场和发展·深度优先
摸鱼仙人~9 小时前
0-1背包与完全背包:遍历顺序背后的秘密
人工智能·算法
juleskk9 小时前
2.15 复试训练
开发语言·c++·算法
kronos.荒10 小时前
滑动窗口+哈希表:最小覆盖子串
数据结构·python·散列表
那起舞的日子10 小时前
斐波那契数列
java·算法
wostcdk10 小时前
筛质数汇总
数据结构·算法
不吃橘子的橘猫10 小时前
《集成电路设计》复习资料4(Verilog HDL概述)
学习·算法·fpga开发·集成电路·仿真·半导体
宇木灵10 小时前
C语言基础-五、数组
c语言·开发语言·学习·算法