Skip to content

Commit 726a02f

Browse files
min deviation
1 parent 94de189 commit 726a02f

1 file changed

Lines changed: 10 additions & 10 deletions

File tree

‎greedy_methods/minimize_deviation_in_array.py‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
"""
22
You are given an array nums of n positive integers.
33
4-
You can perform two types of operations on any
4+
You can perform two types of operations on any
55
element of the array any number of times:
66
77
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
8+
For example, if the array is [1,2,3,4], then you can do this operation
99
on the last element, and the array will be [1,2,3,2].
1010
If the element is odd, multiply it by 2.
1111
For example, if the array is [1,2,3,4], then you can do this operation
1212
on the first element, and the array will be [2,2,3,4].
13-
The deviation of the array is the maximum difference between
13+
The deviation of the array is the maximum difference between
1414
any two elements in the array.
1515
16-
Return the minimum deviation the array can have after performing
16+
Return the minimum deviation the array can have after performing
1717
some number of operations.
1818
"""
1919

2020
from heapq import heapify, heappop, heappush
21-
21+
2222

2323
class Solution:
2424
def minimum_deviation(self, nums) -> int:
@@ -29,7 +29,7 @@ def minimum_deviation(self, nums) -> int:
2929
nums (List[int]): A list of positive integers.
3030
3131
Returns:
32-
temp_mindeviation (int): The minimum deviation of the array after operations.
32+
temp_mindeviation (int): The minimum deviation of array after operations.
3333
3434
Examples:
3535
>>> solution = Solution()
@@ -45,11 +45,11 @@ def minimum_deviation(self, nums) -> int:
4545
temp_min = -min(heapque, key=lambda num: -num)
4646
temp_mindeviation = -heapque[0] - temp_min
4747

48-
while h and h[0] % 2 == 0:
49-
n = heappop(h) // 2
50-
heappush(h, n)
48+
while heapque and heapque[0] % 2 == 0:
49+
n = heappop(heapque) // 2
50+
heappush(heapque, n)
5151
temp_min = min(temp_min, -n)
52-
temp_mindeviation = min(temp_mindeviation, -h[0] - temp_min)
52+
temp_mindeviation = min(temp_mindeviation, -heapque[0] - temp_min)
5353

5454
return temp_mindeviation
5555

0 commit comments

Comments
 (0)