Why my binary search c++ code Runtime Error ?


  • 0
    E
    class Solution {
    public:
        int hIndex(vector<int>& citations) {
            if( citations.empty() ) return 0;
    
            sort( citations.begin(), citations.end(), cmp);
            int left = 0, right = citations.size()-1;
            while( left+1 < right ){
                int mid = (left+right)/2;
                if( citations[mid] >= mid )
                    left = mid;
                else
                    right = mid;
            }
            if(citations[right] >= right)
                return right;
            else
                return left;
        }
    
        static bool cmp( const int & a, const int & b ){
            return a >= b;
        }
    };
    

    my code runtime error in the case:

    [127,198,132,59,47,107,159,65,180,197,65,16,92,143,157,70,73,31,138,10,202,20,63,80,110,63,52,183,97,54,26,70,126,200,233,247,224,50,164,195,117,187,182,94,225,124,157,48,221,250,247,63,7,80,75,76,18,102,161,53,116,105,108,14,25,159,180,224,7,18,107,210,178,186,56,163,15,128,22,71]
    

    But why? It works in my own computer.


Log in to reply
 

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