Skip to content

Commit 84bb3f3

Browse files
AnanyaaKoundalpre-commit-ci[bot]cclauss
authored
add: Minimum Deviation in Array to greedy_methods (#10860)
* add: Minimum Deviation in Array in greedy methods * min deviation * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * min deviation * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * min deviation * min deviation * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * min deviation * min deviation * min deviation * updating DIRECTORY.md --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Christian Clauss <cclauss@me.com> Co-authored-by: cclauss <cclauss@users.noreply.github.com>
1 parent 26f49ab commit 84bb3f3

2 files changed

Lines changed: 61 additions & 0 deletions

File tree

DIRECTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,7 @@
670670
* [Fractional Knapsack](greedy_methods/fractional_knapsack.py)
671671
* [Fractional Knapsack 2](greedy_methods/fractional_knapsack_2.py)
672672
* [Gas Station](greedy_methods/gas_station.py)
673+
* [Minimize Deviation In Array](greedy_methods/minimize_deviation_in_array.py)
673674
* [Minimum Coin Change](greedy_methods/minimum_coin_change.py)
674675
* [Minimum Waiting Time](greedy_methods/minimum_waiting_time.py)
675676
* [Optimal Merge Pattern](greedy_methods/optimal_merge_pattern.py)
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
"""
2+
You are given an array nums of n positive integers.
3+
4+
You can perform two types of operations on any
5+
element of the array any number of times:
6+
7+
If the element is even, divide it by 2.
8+
For example, if the array is [1,2,3,4], then you can do this operation
9+
on the last element, and the array will be [1,2,3,2].
10+
If the element is odd, multiply it by 2.
11+
For example, if the array is [1,2,3,4], then you can do this operation
12+
on the first element, and the array will be [2,2,3,4].
13+
The deviation of the array is the maximum difference between
14+
any two elements in the array.
15+
16+
Return the minimum deviation the array can have after performing
17+
some number of operations.
18+
"""
19+
20+
from heapq import heapify, heappop, heappush
21+
22+
23+
class Solution:
24+
def minimum_deviation(self, nums: list[int]) -> int:
25+
"""
26+
Function to find the minimum deviation of an array after performing operations.
27+
28+
Args:
29+
nums (List[int]): A list of positive integers.
30+
31+
Returns:
32+
temp_mindeviation (int): The minimum deviation of array after operations.
33+
34+
Examples:
35+
>>> solution = Solution()
36+
>>> solution.minimum_deviation([1, 2, 3, 4])
37+
1
38+
>>> solution.minimum_deviation([5, 10, 20, 30, 30])
39+
5
40+
>>> solution.minimum_deviation([8, 8, 8, 8, 8])
41+
0
42+
"""
43+
heapque = [-n * 2 if n % 2 else -n for n in nums]
44+
heapify(heapque)
45+
temp_min = -min(heapque, key=lambda num: -num)
46+
temp_mindeviation = -heapque[0] - temp_min
47+
48+
while heapque and heapque[0] % 2 == 0:
49+
n = heappop(heapque) // 2
50+
heappush(heapque, n)
51+
temp_min = min(temp_min, -n)
52+
temp_mindeviation = min(temp_mindeviation, -heapque[0] - temp_min)
53+
54+
return temp_mindeviation
55+
56+
57+
if __name__ == "__main__":
58+
import doctest
59+
60+
doctest.testmod()

0 commit comments

Comments
 (0)