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:
2.
[1,2,3,4], then you can do this operation on the last element, and the array will be [1,2,3,2].2.
[1,2,3,4], then you can do this operation on the first element, and the array will be [2,2,3,4].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.length2 <= n <= 5 * 1041 <= nums[i] <= 109This approach utilizes a max-heap to efficiently retrieve the largest element while adjusting it within the array to reduce deviation.
1. First, we convert all numbers to even (since an odd number can only become larger when even via multiplication). This is done by multiplying odd numbers by 2.
2. We utilize a max-heap where each iteration involves reducing the maximum number (if even) to reduce the deviation.
3. Throughout this process, track the minimum seen number to calculate and update the minimum deviation at every stage.
The core idea is to use sorting as a form of managing the max-heap. We multiply each odd by 2 and maintain an array. Sort it to effectively manage the largest element.
The element changes are managed in situ over each comparison step, reducing the maximum each time by half if it's even and updating the reference minimum.
C++
Java
Python
C#
JavaScript
Time Complexity: O(n log n) due to sorting operations on each iteration.
Space Complexity: O(n) for the heap structure representation.
In this alternative approach, the strategy focuses on an incremental adjustment instead of heap reliance. It involves directly mutating and managing the adjustment in a sorted sequence approach to target deviation reduction without standard heap operations.
1. First, transform any odd number by doubling it. Track the preliminary minimum number.
2. Sort the array and iteratively adjust the maximum through division by halving until reaching an odd number.
The minimum deviation is kept through direct comparison evaluations across the deviations achieved after each max adjustment.
The solution involves a sorting and comparison dynamic without direct priority queue adjustments. Key effect revolves around a sorted list whereby operations are performed directly, sorting on each maximum reduction progress.
C++
Java
Python
C#
JavaScript
Time Complexity: O(n^2 log n) due to multiple sorts.
Space Complexity: O(1) or O(n) considering in-place sorts might not require extra space.
| Approach | Complexity |
|---|---|
| Approach using Max-Heap | Time Complexity: O(n log n) due to sorting operations on each iteration. |
| Approach using Dynamic Adjustment | Time Complexity: O(n^2 log n) due to multiple sorts. |
Minimize Deviation in Array - Leetcode 1675 - Python • NeetCodeIO • 13,081 views views
Watch 9 more video solutions →Practice Minimize Deviation in Array with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor