leetcode

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

2009.cpp (458B)


      1 class Solution {
      2   public:
      3     int minOperations(vector<int> &nums) const {
      4         const int n = nums.size();
      5         sort(begin(nums), end(nums));
      6         nums.erase(unique(nums.begin(), nums.end()), nums.end());
      7 
      8         int res = INT_MIN, j = 0;
      9         for (int i = 0; i < nums.size(); i++) {
     10             while (j < nums.size() && nums[j] < nums[i] + n)
     11                 j++;
     12             res = max(res, j - i);
     13         }
     14         return n - res;
     15     }
     16 };