Advertisement
CR7CR7

hFindIndex

Jun 25th, 2023
1,110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.51 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     int hIndex(vector<int>& citations) {
  4.         int n = citations.size();
  5.         int left = 0, right = n - 1;
  6.  
  7.         while (left <= right) {
  8.             int mid = left + (right - left) / 2;
  9.             int h = n - mid;
  10.  
  11.             if (citations[mid] == h) {
  12.                 return h;
  13.             } else if (citations[mid] < h) {
  14.                 left = mid + 1;
  15.             } else {
  16.                 right = mid - 1;
  17.             }
  18.         }
  19.  
  20.         return n - left;
  21.     }
  22. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement