275. H-Index II

来源:互联网 发布:怎样开发app软件 编辑:程序博客网 时间:2024/05/22 05:12

Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimize your algorithm?

Hint:

  1. Expected runtime complexity is in O(log n) and the input is sorted.
public static int hIndex(int[] citations) {        int start = 0;        int end = citations.length - 1;        int n = citations.length;        while(start <= end){            int middle = (start + end) / 2;            if(citations[middle] == n - middle){            return n - middle;            }            if(citations[middle] > n - middle){                end  = middle - 1;            }else{            start = middle + 1;            }        }        return n - start;    }


0 0
原创粉丝点击