leetcode

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

1509.cpp (489B)


      1 class Solution {
      2   public:
      3     int minDifference(vector<int> &nums) const {
      4         const int n = size(nums);
      5         if (n <= 4) return 0;
      6         partial_sort(begin(nums), begin(nums) + 4, end(nums));
      7         nth_element(begin(nums) + 4, end(nums) - 4, end(nums));
      8         sort(end(nums) - 4, end(nums));
      9         return min({
     10             nums[n - 1] - nums[3],
     11             nums[n - 2] - nums[2],
     12             nums[n - 3] - nums[1],
     13             nums[n - 4] - nums[0],
     14         });
     15     }
     16 };