Created
May 18, 2019 21:50
-
-
Save brunogfranca/5d57964e73a44e4bfd4de8aedbf59858 to your computer and use it in GitHub Desktop.
Test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from collections import defaultdict | |
def solution(A): | |
ordered_values = get_ordered_values(A) | |
ocurrences = get_ocurrences(A) | |
max_left = max(A) | |
max_right = 0 | |
max_diff = 0 | |
left = A | |
right = [] | |
while left: | |
new_item = left.pop() | |
if not left: | |
break | |
right.append(new_item) | |
ocurrences[new_item] -= 1 | |
if new_item == max_left and ocurrences[new_item] == 0: | |
while ordered_values: | |
max_left = ordered_values[0] | |
if ocurrences[max_left] > 0: | |
break | |
ordered_values.pop(0) | |
max_right = max(max_right, new_item) | |
diff = get_difference(max_left, max_right) | |
max_diff = max(max_diff, diff) | |
return max_diff | |
def get_ordered_values(arr): | |
return sorted(list(set(arr)), reverse=True) | |
def get_ocurrences(arr): | |
ocurrences = defaultdict(lambda: 0) | |
for item in arr: | |
ocurrences[item] += 1 | |
return ocurrences | |
def get_difference(left, right): | |
return max( | |
abs(left - right), | |
abs(right - left), | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment