Skip to content

Instantly share code, notes, and snippets.

@gbvanrenswoude
Created September 5, 2022 08:17
Show Gist options
  • Select an option

  • Save gbvanrenswoude/a8ffb5540080001e5bb729c6247e757d to your computer and use it in GitHub Desktop.

Select an option

Save gbvanrenswoude/a8ffb5540080001e5bb729c6247e757d to your computer and use it in GitHub Desktop.
Maximum Sequential Difference
from datetime import datetime
def maximum_sequential_difference(numbers):
"""Return the maximum difference between 2 numbers in a List where the index of the smallest number is greater than the index of the largest number, calculated with linear complexity."""
# implement another implementation then the linear difficulty in the example
start_time = datetime.now()
current_difference = 0
lenght = len(numbers)
if lenght == 0:
return current_difference
current_maximum_difference = numbers[lenght - 1]
for i in reversed(range(lenght - 1)):
if numbers[i] >= current_maximum_difference:
current_maximum_difference = numbers[i]
else:
current_difference = max(
current_difference, current_maximum_difference - numbers[i])
time_elapsed = datetime.now() - start_time
print('Time elapsed (hh:mm:ss.ms) {}'.format(time_elapsed))
return current_difference
def maximum_sequential_difference_on2(numbers):
"""Return the minimum difference between 2 numbers in a List where the index of the smallest number is greater than the index of the largest number, calculated with quadratic complexity."""
start_time = datetime.now()
a = 0
for i in range(len(numbers)):
for j in range(i+1, len(numbers)):
if a < numbers[j]-numbers[i]:
a = numbers[j]-numbers[i]
time_elapsed = datetime.now() - start_time
print('Time elapsed (hh:mm:ss.ms) {}'.format(time_elapsed))
return a
def maximum_profit(numbers):
return maximum_sequential_difference(numbers)
# print(maximum_profit([1])) # 0
# print(maximum_profit([1, 999])) # 998
# print(maximum_profit([999, 1])) # 0 → red flag if this fails
# print(maximum_profit([1, 2, 999])) # 998
# print(maximum_profit([999, 2, 1])) # 0
# print(maximum_profit([999, 1, 2])) # 1
# print(maximum_profit([3, 4, 999, 1, 2, 999])) # 998
small_list = [1, 20, 12]
huge_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 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 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8,
9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 13, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8,
9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 13, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8,
9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 13, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8,
9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 13, 1, 2, 3, 4, 5, 6, 7, 8, 9, 20]
print(maximum_sequential_difference(small_list))
print(maximum_sequential_difference_on2(small_list))
print(maximum_sequential_difference(huge_list))
print(maximum_sequential_difference_on2(huge_list))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment