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;
    }
};
相关推荐
BHXDML19 小时前
第七章:类与对象(c++)
开发语言·c++
玄冥剑尊19 小时前
贪心算法进阶
算法·贪心算法
玄冥剑尊19 小时前
贪心算法深化 I
算法·贪心算法
52Hz11819 小时前
力扣73.矩阵置零、54.螺旋矩阵、48.旋转图像
python·算法·leetcode·矩阵
BHXDML19 小时前
第一章:线性回归& 逻辑回归
算法·逻辑回归·线性回归
yyf1989052519 小时前
C++ 跨平台开发的挑战与应对策略
c++
iAkuya20 小时前
(leetcode)力扣100 二叉搜索树种第K小的元素(中序遍历||记录子树的节点数)
算法·leetcode·职场和发展
又见野草20 小时前
C++类和对象(中)
开发语言·c++
-To be number.wan20 小时前
B 树 vs B+ 树:为什么 MySQL 用 B+ 树,而不是 B 树?
数据结构
杨间20 小时前
《排序算法全解析:从基础到优化,一文吃透八大排序!》
c语言·数据结构·排序算法