leetcode

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

2616.cpp (634B)


      1 class Solution {
      2     int count(const vector<int> &nums, int treshold) {
      3         int cnt = 0;
      4         for (int i = 1; i < nums.size(); i++) {
      5             if (nums[i] - nums[i - 1] <= treshold) cnt++, i++;
      6         }
      7         return cnt;
      8     }
      9 
     10   public:
     11     int minimizeMax(vector<int> &nums, int p) {
     12         sort(nums.begin(), nums.end());
     13         int left = 0, right = nums.back() - nums.front();
     14         while (left < right) {
     15             int mid = left + (right - left) / 2;
     16             if (count(nums, mid) >= p)
     17                 right = mid;
     18             else
     19                 left = mid + 1;
     20         }
     21         return left;
     22     }
     23 };