个人技术分享

力扣275.H指数 II

  • 二分答案

  •   class Solution {
      public:
          int hIndex(vector<int>& citations) {
              int n = citations.size();
              auto check = [&](int mid) -> bool
              {
                  int res=0;
                  for(int i=0;i<n;i++)
                  {
                      if(citations[i] >= mid) res++;
                      if(res >= mid) return true;
                  }
                  return false;
              };
      
              int l = 0,r = n;
              while(l<r)
              {
                  int mid = l+r+1>>1;
                  if(check(mid)) l = mid;
                  else r=mid-1;
              }
              return l;
          }
      };