[Java][Leetcode middle] 274. H 指数

没做出来,看的解析。

一、 先对数组进行逆序,然后判断是否符合

3,0,6,1,5\] --\> \[6,5,3,1,0

对于i=3时:如果hIndex想为4(4个引用4以上的论文),那么:citations[3] 需要大于等于4(I+1)。 一旦不满足,说明不符合本条件,直接返回i即可。

java 复制代码
class Solution {
    public int hIndex(int[] citations) {
         Arrays.sort(citations);
         reverse(citations);

         for(int i=0;i<citations.length;i++){
              if(citations[i] <i+1){
                 return i;
              }
         }
         return citations.length;
    }

    public void reverse(int[] nums){
         int start = 0;
         int end = nums.length-1;
         while(start < end){
            int tmp = nums[start];
            nums[start] = nums[end];
            nums[end] = tmp;
            start++;
            end--;
         }
    }
}

二、 先对数组进行正序,然后判断是否符合

就是上一个的转置版本

3,0,6,1,5\] --\> \[0, 1, 3 ,5 ,6

n-i: 5 4 3 2 1

如果要满足条件,citations[I] 必须大于等于n-i

java 复制代码
class Solution {
    public int hIndex(int[] citations) {
         Arrays.sort(citations);
         int n = citations.length;
         for(int i=0;i<n ;i++){
              if(citations[i] >= n-i){
                 return n-i;
              }
         }
         return 0;
    }
}

三、使用一个数组统计 引用次数为i的论文数量

o(N)

java 复制代码
class Solution {
    public int hIndex(int[] citations) {
        int n = citations.length;
        // 引用次数为i的论文数量(0-n,共n+1项)
        int[] counts  = new int[n+1];

        for(int cite : citations){
            if(cite >=n){
                counts[n]++;
            }else{
                counts[cite]++;
            }
        }
        
        int total = 0;
        for(int i = n;i>=0;i--){
            total += counts[i];
            if(total >= i){
                return i;
            }
        }
        return 0;
    }

}

四、利用二分查找

  • O(N log N)
  • hIndex最小是0,最大是数组长度n
  • 先猜测一个hIndex值mid,然后在数组中统计这个值是否满足条件。如果满足,说明至少是mid,还有可能更多。如果不满足,则缩小查找范围。
java 复制代码
class Solution {
    public int hIndex(int[] citations) {
        
         int left = 0,right = citations.length;
         int mid = 0;
         int i;
         int cnt=0;

         while(left<right){
            mid = (left + right + 1) >>1;

            cnt = 0;
            for(i=0;i<citations.length;i++){
                if(citations[i] >= mid){
                    cnt ++;
                }
            }
            if(cnt >= mid){
               left = mid;
            }else{
               right = mid - 1;
            }
         }
         return left;
    }
}
相关推荐
一切皆是因缘际会2 小时前
从概率拟合到内生心智:2026 下一代 AI 架构演进与落地实践
人工智能·深度学习·算法·架构
Java成神之路-2 小时前
【LeetCode 刷题笔记】34. 在排序数组中查找元素的第一个和最后一个位置 | 二分查找经典刷题题解
算法·leetcode
qq_589568102 小时前
springbootweb案例,出现访问 http://localhost:8080/list 一直处于浏览器运转阶段
java·网络协议·http·list·springboot
不忘不弃2 小时前
用BFS方法求解平分汽油问题
算法·宽度优先
AI科技星3 小时前
全域数学·72分册·射影原本 无穷维射影几何卷细化子目录【乖乖数学】
人工智能·线性代数·算法·机器学习·数学建模·数据挖掘·量子计算
风落无尘3 小时前
《智能重生:从垃圾堆到AI工程师》——第四章 变化的艺术
人工智能·线性代数·算法
JAVA面经实录9173 小时前
计算机基础(完整版·超详细可背诵)
java·linux·数据结构·算法
AC赳赳老秦3 小时前
知识产权辅助:用 OpenClaw 批量生成专利交底书 / 软著申请材料,自动校验格式与内容合规性
java·人工智能·python·算法·elasticsearch·deepseek·openclaw
WBluuue3 小时前
Codeforces 1093 Div2(ABCD1D2)
c++·算法
浅念-3 小时前
「一文吃透 BFS:从层序遍历到锯齿形、最大宽度、每层最大值」
数据结构·算法