leetcode

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

1675.cpp (607B)


      1 class Solution {
      2   public:
      3     int minimumDeviation(vector<int> &nums) {
      4         int res = INT_MAX, mini = INT_MAX;
      5         for (auto &num : nums) {
      6             num = num % 2 ? num * 2 : num;
      7             mini = min(mini, num);
      8         }
      9 
     10         make_heap(begin(nums), end(nums));
     11         while (nums.front() % 2 == 0) {
     12             res = min(res, nums.front() - mini);
     13             mini = min(mini, nums.front() / 2);
     14             pop_heap(begin(nums), end(nums));
     15             nums.back() /= 2;
     16             push_heap(begin(nums), end(nums));
     17         }
     18 
     19         return min(res, nums.front() - mini);
     20     }
     21 };