Skip to content

Instantly share code, notes, and snippets.

@airicbear
Last active June 25, 2021 22:41
Show Gist options
  • Save airicbear/651127ecb45ebcba0a47015c0d0e062b to your computer and use it in GitHub Desktop.
Save airicbear/651127ecb45ebcba0a47015c0d0e062b to your computer and use it in GitHub Desktop.
# 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