You are given an array nums
of n
positive integers.
You can perform two types of operations on any element of the array any number of times:
- If the element is even, divide it by
2
.- For example, if the array is
[1,2,3,4]
, then you can do this operation on the last element, and the array will be[1,2,3,2].
- For example, if the array is
- If the element is odd, multiply it by
2
.- For example, if the array is
[1,2,3,4]
, then you can do this operation on the first element, and the array will be[2,2,3,4].
- For example, if the array is
The deviation of the array is the maximum difference between any two elements in the array.
Return the minimum deviation the array can have after performing some number of operations.
Example 1:
Input: nums = [1,2,3,4] Output: 1 Explanation: You can transform the array to [1,2,3,2], then to [2,2,3,2], then the deviation will be 3 - 2 = 1.
Example 2:
Input: nums = [4,1,5,20,3] Output: 3 Explanation: You can transform the array after two operations to [4,2,5,5,3], then the deviation will be 5 - 2 = 3.
Example 3:
Input: nums = [2,10,8] Output: 3
Constraints:
n == nums.length
2 <= n <= 105
1 <= nums[i] <= 109
Solution: Priority Queue
If we double an odd number it becomes an even number, then we can only divide it by two which gives us back the original number. So we can pre-double all the odd numbers and only do division in the following process.
We push all numbers including pre-doubled odd ones onto a priority queue, and track the difference between the largest and smallest number.
Each time, we pop the largest number out and divide it by two then put it back to the priority queue, until the largest number becomes odd. We can not discard it and divide any other smaller numbers by two will only increase the max difference, so we can stop here.
ex1: [3, 5, 8] => [6, 8, 10] (pre-double) => [5, 6, 8] => [4, 5, 6] => [3, 4, 5] max diff is 5 – 3 = 2
ex2: [4,1,5,20,3] => [2, 4, 6, 10, 20] (pre-double) => [2, 4, 6, 10] => [2, 4, 5, 6] => [2,3,4,5] max diff = 5-2 = 3
Time complexity: O(n*logm*logn)
Space complexity: O(n)
C++/Set
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
class Solution { public: int minimumDeviation(vector<int>& nums) { set<int> s; for (int x : nums) s.insert(x & 1 ? x * 2 : x); int ans = *rbegin(s) - *begin(s); while (*rbegin(s) % 2 == 0) { s.insert(*rbegin(s) / 2); s.erase(*rbegin(s)); ans = min(ans, *rbegin(s) - *begin(s)); } return ans; } }; |
C++/PQ
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
class Solution { public: int minimumDeviation(vector<int>& nums) { priority_queue<int> q; int lo = INT_MAX; for (int x : nums) { x = x & 1 ? x * 2 : x; q.push(x); lo = min(lo, x); } int ans = q.top() - lo; while (q.top() % 2 == 0) { int x = q.top(); q.pop(); q.push(x / 2); lo = min(lo, x / 2); ans = min(ans, q.top() - lo); } return ans; } }; |