-
-
Notifications
You must be signed in to change notification settings - Fork 51.1k
add: Minimum Deviation in Array to greedy_methods #10860
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 10 commits
adf63fb
b7f41c6
1e4a99f
1d42c0a
ace9533
ecbedb1
d6b40b3
d092a42
d534b86
94de189
0e8386b
726a02f
65b11d4
2fb09b3
a48057f
5b47252
cec99db
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| """ | ||
| 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. | ||
| """ | ||
|
|
||
| from heapq import heapify, heappop, heappush | ||
|
|
||
|
|
||
| class Solution: | ||
| def minimum_deviation(self, nums) -> int: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide type hint for the parameter: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide type hint for the parameter: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide type hint for the parameter: |
||
| """ | ||
| Function to find the minimum deviation of an array after performing operations. | ||
|
|
||
| Args: | ||
| nums (List[int]): A list of positive integers. | ||
|
|
||
| Returns: | ||
| temp_mindeviation (int): The minimum deviation of the array after operations. | ||
|
|
||
| Examples: | ||
| >>> solution = Solution() | ||
| >>> solution.minimumDeviation([1, 2, 3, 4]) | ||
| 1 | ||
| >>> solution.minimumDeviation([5, 10, 20, 30, 30]) | ||
| 5 | ||
| >>> solution.minimumDeviation([8, 8, 8, 8, 8]) | ||
| 0 | ||
| """ | ||
| heapque = [-n * 2 if n % 2 else -n for n in nums] | ||
| heapify(heapque) | ||
| temp_min = -min(heapque, key=lambda num: -num) | ||
| temp_mindeviation = -heapque[0] - temp_min | ||
|
|
||
| while h and h[0] % 2 == 0: | ||
| n = heappop(h) // 2 | ||
| heappush(h, n) | ||
| temp_min = min(temp_min, -n) | ||
| temp_mindeviation = min(temp_mindeviation, -h[0] - temp_min) | ||
|
|
||
| return temp_mindeviation | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| import doctest | ||
|
|
||
| doctest.testmod() | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please provide type hint for the parameter:
nums