Last active
August 31, 2020 16:23
-
-
Save gavinanderegg/106430e25726b304d999373c9c1657e2 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# 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}') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.