Leetcode Problem 1675. Minimize Deviation in Array

1675. Minimize Deviation in Array

Leetcode Solutions

Approach: Simulation + Heap

  1. Initialize a max-heap evens and an integer minimum to track the smallest element.
  2. For each number in nums, if it is odd, multiply it by 2. Add all numbers to evens and update minimum if necessary.
  3. While the maximum number in evens is even: a. Extract the maximum number from evens. b. Update the minimum deviation using the extracted number and minimum. c. If the extracted number is even, divide it by 2 and add it back to evens. d. Update minimum if the new number is smaller.
  4. Return the minimum deviation.
UML Thumbnail

Approach: Pretreatment + Sorting + Sliding Window

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...