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

没做出来,看的解析。

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

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

对于i=3时:如果hIndex想为4(4个引用4以上的论文),那么:citations3 需要大于等于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

如果要满足条件,citationsI 必须大于等于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 小时前
基于SpringBoot的课程预约系统
java·springboot·web开发·后端开发
圣保罗的大教堂2 小时前
leetcode 3517. 最小回文排列 I 中等
leetcode
土豆.exe2 小时前
Fastjson2 2.0.53 哈希碰撞 RCE:从原理到三种打法
算法·哈希算法
Leighteen3 小时前
`try-finally` 里的 `return`:为什么 `finally` 会悄悄改掉返回值、吞掉异常
java·开发语言
黄河123长江3 小时前
有限Abel群的结构()
算法
名字还没想好☜3 小时前
Go 的 time.After 在 select 循环里内存泄漏:定时器堆积原理与 timer.Reset 正确姿势
java·数据库·golang·go·goroutine
Jerry3 小时前
LeetCode 92. 反转链表 II
算法
骊城英雄4 小时前
Rust从入门到精通-trait
人工智能·算法·rust
圆山猫4 小时前
[Virtualization](三):RISC-V H-extension 与 Guest 执行模式
android·java·risc-v
caishenzhibiao4 小时前
期货先行者主图 同花顺期货通指标
java·c语言·c#