Skip to main content

Command Palette

Search for a command to run...

Solving Leetcode Interviews in Seconds with AI: Minimize Deviation in Array

Updated
3 min read

Introduction

In this blog post, we will explore how to solve the LeetCode problem "1675" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.

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], then you can do this operation on the last element, and the array will be [1,2,3,2]. 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]. 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 <= 5 * 104 1 <= nums[i] <= 109

Explanation

Here's a solution to minimize the deviation of the array, focusing on efficiency:

  • Core Idea: Reduce the maximum values as much as possible and increase the minimum values as much as possible to shrink the range. Even numbers can only decrease, while odd numbers can only increase initially. We maintain a set of possible numbers each element can become, prioritizing reducing the maximum and increasing the minimum as needed.

  • Priority Queue (Heap): Use a min-heap (priority queue) to efficiently track the smallest possible values and a max-heap to track the largest possible values that each original number can become after a series of operations.

  • Deviation Tracking: Continuously update the minimum deviation by considering the difference between the current maximum and minimum values.

  • Complexity:

    • Runtime: O(n log n log max_num) where max_num is the largest number in the input. We process each element at most log max_num times, and heap operations are O(log n).
    • Storage: O(n)

Code

    import heapq

def minimumDeviation(nums):
    """
    Calculates the minimum deviation of an array after performing operations.

    Args:
        nums: A list of positive integers.

    Returns:
        The minimum deviation achievable.
    """

    min_heap = []
    max_val = float('-inf')

    for num in nums:
        options = set()
        if num % 2 == 0:
            options.add(num)
            while num % 2 == 0:
                options.add(num)
                num //= 2
            options.add(num)
        else:
            options.add(num)
            options.add(num * 2)

        min_val = min(options)
        max_val_local = max(options)

        heapq.heappush(min_heap, (min_val, options))
        max_val = max(max_val, max_val_local)



    min_deviation = float('inf')
    while True:
        min_val, options = heapq.heappop(min_heap)
        min_deviation = min(min_deviation, max_val - min_val)

        options.remove(min_val)

        if not options:
            break

        next_min = min(options)
        next_max = max(options)
        heapq.heappush(min_heap, (next_min, options))
        max_val = max(max_val, next_max)

        if min_val == next_min:
            continue


        max_val = max(max_val, next_max)
        if min_val == next_min:
            continue

        max_val = max(max_val, next_max)



    return min_deviation

More from this blog

C

Chatmagic blog

2894 posts