0271-0280

271. Encode and Decode Strings $\star\star$

272. Closest Binary Search Tree Value II $\star\star\star$

273. Integer to English Words $\star\star\star$

274. H-Index $\star\star$

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
class Solution {
 public:
  int hIndex(vector<int>& citations) {
    sort(citations.begin(), citations.end());

    for (int i = 0; i < citations.size(); ++i)
      if (citations[i] >= citations.size() - i) return citations.size() - i;

    return 0;
  }
};

275. H-Index II $\star\star$

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
class Solution {
 public:
  int hIndex(vector<int>& citations) {
    int l = 0;
    int r = citations.size() - 1;

    while (l <= r) {
      int m = (l + r) >> 1;
      if (citations[m] == citations.size() - m)
        return citations[m];
      else if (citations[m] > citations.size() - m)
        r = m - 1;
      else
        l = m + 1;
    }

    return citations.size() - (r + 1);
  }
};

276. Paint Fence $\star$

277. Find the Celebrity $\star\star$

278. First Bad Version $\star$

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
bool isBadVersion(int version);

class Solution {
 public:
  int firstBadVersion(int n) {
    int l = 1;
    int r = n;

    while (l < r) {
      int m = l + (r - l) / 2;
      if (isBadVersion(m))
        r = m;
      else
        l = m + 1;
    }

    return l;
  }
};

279. Perfect Squares $\star\star$

280. Wiggle Sort $\star\star$