Last active
June 25, 2021 22:41
-
-
Save airicbear/651127ecb45ebcba0a47015c0d0e062b 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
# frozen_string_literal: true | |
def stock_picker(prices) | |
curr_buy = curr_sell = best_buy = best_sell = max_profit = 0 | |
(1..(prices.length - 1)).each do |i| | |
diff = prices[i] - prices[curr_buy] | |
curr_sell = i | |
curr_buy = i if diff.negative? | |
next if diff < max_profit | |
new_choice = [diff, curr_buy, curr_sell] | |
max_profit, best_buy, best_sell = new_choice | |
end | |
[best_buy, best_sell] | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment