Similar Problems

Similar Problems not available

Minimize Deviation In Array - Leetcode Solution

Companies:

LeetCode:  Minimize Deviation In Array Leetcode Solution

Difficulty: Hard

Topics: greedy heap-priority-queue array  

Problem Statement:

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], you can perform the following operations:

  1. Divide the second element by 2 twice and get [1,1,3,4].
  2. Divide the third element by 2 and get [1,2,1,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: Input: nums = [1,2,3,4] Output: 1 Explanation: You can transform the array to [1,1,3,4], then the deviation will be 4-1 = 3, which is the minimum possible.

Approach:

To solve this problem, we can use a greedy approach. We first convert all odd numbers in the array to even numbers by multiplying them by 2. Then, we repeatedly perform the following steps until we cannot perform any further operations:

  1. Find the maximum number in the array.
  2. Divide the maximum number by 2.
  3. If the divided number is even, replace the maximum number with the divided number; otherwise, we add the original maximum number to a min heap and replace the maximum number with the minimum element in the heap.

Finally, we return the difference between the maximum and the minimum element in the array, which is the deviation we need to minimize.

Solution:

First, we will import the heapq library in Python to implement the min heap. Then, we will create an empty min heap and transform all odd numbers in the array to even numbers by multiplying them by 2.

import heapq

class Solution: def minimumDeviation(self, nums: List[int]) -> int: min_heap = [] for i in range(len(nums)): if nums[i] % 2 == 1: nums[i] *= 2 ...

Now, we will perform the steps mentioned above until we cannot perform any further operations. To find the maximum number in the array, we can simply use the max() function in Python. Then, we can divide the maximum number by 2 and check if the divided number is even. If it is, we can replace the maximum number with the divided number, and continue with the next iteration. Otherwise, we add the original maximum number to the heap and replace the maximum number with the minimum element in the heap using the heapreplace() function. We also keep track of the minimum deviation we have found so far.

    ...    
    max_num = max(nums)
    min_deviation = float('inf')
    while True:
        min_num = heapq.heappop(nums)
        min_deviation = min(min_deviation, max_num - min_num)
        if max_num % 2 == 1:
            heapq.heappush(min_heap, -max_num)
            max_num = max(max_num//2, min_heap[0])
            heapq.heapreplace(min_heap, -max_num)
        else:
            max_num //= 2
        if min_heap and max_num <= -min_heap[0]:
            break
    return min_deviation

Full Code:

import heapq

class Solution: def minimumDeviation(self, nums: List[int]) -> int: min_heap = [] for i in range(len(nums)): if nums[i] % 2 == 1: nums[i] *= 2 max_num = max(nums) min_deviation = float('inf') while True: min_num = heapq.heappop(nums) min_deviation = min(min_deviation, max_num - min_num) if max_num % 2 == 1: heapq.heappush(min_heap, -max_num) max_num = max(max_num//2, min_heap[0]) heapq.heapreplace(min_heap, -max_num) else: max_num //= 2 if min_heap and max_num <= -min_heap[0]: break return min_deviation

Minimize Deviation In Array Solution Code

1