4ms c++ sort and binary search


  • 0
    S
    class Solution {
    public:
        int hIndex(vector<int>& citations) {
    		citations.push_back(0);
            sort(citations.begin(), citations.end());
            if(citations.empty() || citations[citations.size()-1] == 0) return 0;
            int s = 0, e = citations.size(), mid, n = citations.size();
            while(s < e){
                if(e-s <= 2){
                    return n-e+1;
                }
                mid = (s+e)/2;
                if(citations[mid] == n-mid)
                    return citations[mid];
                else if(citations[mid] < n-mid)
                    s = mid;
                else
                    e = mid+1;
            }
        }
    };

Log in to reply
 

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