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

文章目录

一、题目

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.

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 <= citationsi <= 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;
    }
};
相关推荐
凌波粒9 分钟前
LeetCode--53. 最大子序和(贪心算法)
算法·leetcode·贪心算法
Hesionberger17 分钟前
快速求解完全平方数的最少数量
开发语言·数据结构·python·算法·leetcode·c#
c2385617 分钟前
《序列 DP:C++ 中的“最长”套路与编辑距离》
c++·算法·动态规划
aqiu11111119 分钟前
【算法日记 19】LeetCode 1. 两数之和:梦开始的地方,哈希表的降维打击
算法·leetcode·职场和发展
鱼子星_20 分钟前
【C++】类和对象(下)——初始化列表,类型转换,static成员,友元,内部类,匿名对象,编译器的优化拓展
c语言·c++·笔记
王老师青少年编程8 小时前
2025年【江苏“信息与未来”编程思维】真题及题解(T2:坐标变换)
c++·题解·真题·坐标变换·编程思维·江苏·信息与未来
CS创新实验室10 小时前
算法、齿轮与硅基大脑:数值计算发展简史
人工智能·算法·数值计算
海石11 小时前
1563分的简单题,可能就简单在能被暴力AC
算法·leetcode
海石12 小时前
1400分的dp汗流浃背之【交替子数组计数】
算法·leetcode
奋发向前wcx12 小时前
P2590 树的统计 题目解析
数据结构·算法·深度优先