Skip to content

Instantly share code, notes, and snippets.

@gavinanderegg
Last active August 31, 2020 16:23
Show Gist options
  • Save gavinanderegg/106430e25726b304d999373c9c1657e2 to your computer and use it in GitHub Desktop.
Save gavinanderegg/106430e25726b304d999373c9c1657e2 to your computer and use it in GitHub Desktop.
# A rewrite of an originally terrible solution to the interview challenge
# in the 2020-08-31 issue of @cassidoo's newsletter:
# https://buttondown.email/cassidoo/archive/9bbcae4c-b24e-4d91-9332-115a9c286041
prices = [110, 1, 999, 180, 260, 40, 310, 535, 695, 1, 999, 1]
highest = 0
buy_day = 0
sell_day = 0
for index, price in enumerate(prices):
sub_prices = prices[index:]
for sub_index, sub_price in enumerate(sub_prices):
if sub_price - price > highest:
highest = sub_price - price
buy_day = (index + 1)
sell_day = index + (sub_index + 1)
print(f'Buy on day {buy_day} sell on day {sell_day} to earn {highest}')
@gavinanderegg
Copy link
Author

The recursion idea was kinda not-great. It was the first idea that took me, and I kept going because it was fun. This is much simpler and better.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment