c++ simple easy-understanding binary seacrh


  • 0
    X
       int hIndex(vector<int>& citations) {
            int size = citations.size();
            int left = 0, right = size;
            while(left < right){
                int mid = left + (right - left) / 2;
                // mid + 1 is a valid one , set left = mid + 1
                // It is very like lower/upper bound 
                // the difference is  "is => may be"
                if(citations[size - mid - 1] > mid){
                    left = mid + 1;
                }else{
                    right = mid;
                }
            }
            return left;
        }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.