【每日一题Day363】LC275H 指数Ⅱ | 二分答案

H 指数Ⅱ【LC275】

给你一个整数数组 citations ,其中 citations[i] 表示研究者的第 i 篇论文被引用的次数,citations 已经按照 升序排列 。计算并返回该研究者的 h 指数。

h 指数的定义:h 代表"高引用次数"(high citations),一名科研人员的 h 指数是指他(她)的 (n 篇论文中)总共有 h 篇论文分别被引用了至少 h 次。

请你设计并实现对数时间复杂度的算法解决此问题。

同昨天的二分 区别不用自己排序了

  • 思路

    • 二段性:存在最大值y使,少于等于y的数值一定满足条件;大于y的数值一定不满足条件
    • 二分答案y
      • 引用次数大于等于y的论文数目大于等于y,那么向右搜索获得更大的y
      • 引用次数大于等于y的论文数目小于y,那么向左搜索获得更大的y
    • check:排序后可以快速算出引用次数大于等于y的论文数目
  • 实现

    java 复制代码
    class Solution {
        public int hIndex(int[] citations) {
            
            int n = citations.length;
           // Arrays.sort(citations);
            int l = 1, r = n;
            int res = 0;
            while (l <= r){
                int mid =(l + r) >> 1;
                if (check(citations, mid) >= mid){
                    res = Math.max(res, mid);
                    l = mid + 1;            
                }else{
                     r = mid - 1;
                }
            }
            return res;
        }
        public int check(int[] citations, int target){
            int n = citations.length;
            int l = 0, r = n - 1;
            while (l <= r){
                int mid = l + r >> 1;
                if (citations[mid] < target){
                    l = mid + 1;
                }else{
                    r = mid - 1;
                }
            }
            // l为第一个符合的下标
            return n - l;// 大于等于target的引用次数的数目
    
        }
    }
    • 复杂度
      • 时间复杂度: O ( n l o g m ) O(nlogm) O(nlogm), n n n是数组的长度,m是二分查找的上界。二分查找的时间复杂度是 O ( l o g m ) O(logm) O(logm),每次判断需要的时间复杂度为 O ( n ) O(n) O(n)
      • 空间复杂度: O ( log ⁡ n ) O(\log n) O(logn)
相关推荐
水木流年追梦2 小时前
CodeTop Top 300 热门题目2-最长回文子串
开发语言·人工智能·python·算法·leetcode
_深海凉_2 小时前
LeetCode热题100-在排序数组中查找元素的第一个和最后一个位置
算法·leetcode·职场和发展
北顾笙9802 小时前
day28-数据结构力扣
数据结构·算法·leetcode
米粒13 小时前
力扣算法刷题 Day 48(单调栈)
算法·leetcode·职场和发展
Chase_______4 小时前
LeetCode 1456:定长子串中元音的最大数目
算法·leetcode
6Hzlia5 小时前
【Hot 100 刷题计划】 LeetCode 84. 柱状图中最大的矩形 | C++ 两次单调栈基础扫法
c++·算法·leetcode
6Hzlia6 小时前
【Hot 100 刷题计划】 LeetCode 416. 分割等和子集 | C++ 0-1背包 1D空间极致优化
c++·算法·leetcode
穿条秋裤到处跑6 小时前
每日一道leetcode(2026.04.21):执行交换操作后的最小汉明距离
java·算法·leetcode
Tina学编程6 小时前
算法训练Day10 | LeetCode 169 多数元素
算法·leetcode
sheeta19986 小时前
LeetCode 每日一题笔记 日期:2026.04.22 题目:2452. 距离字典两次编辑以内的单词
笔记·算法·leetcode