leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

0275.cpp (474B)


      1 class Solution {
      2   public:
      3     int hIndex(const vector<int> &citations) const {
      4         const int n = size(citations);
      5         int low = 0, high = n - 1;
      6 
      7         while (low <= high) {
      8             const int mid = low + (high - low) / 2;
      9             if (citations[mid] == n - mid) return citations[mid];
     10             if (citations[mid] > n - mid)
     11                 high = mid - 1;
     12             else
     13                 low = mid + 1;
     14         }
     15 
     16         return n - high - 1;
     17     }
     18 };