Last active
August 29, 2015 13:57
-
-
Save Chryus/9547363 to your computer and use it in GitHub Desktop.
Test: I have an array of stock prices, where the index value corresponds to the time in minutes after midnight, and the value corresponds to the stock prices. Calculate the maximum profit i could make from the buy and sell of the stock.
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
stock_prices = [1, 5, 8, 3, 13, 18, 11, 44] | |
def max_profit stock_prices | |
profit_so_far = 0 | |
stock_prices.each_with_index do |buy_price, buy_time| | |
stock_prices.each_with_index do |sell_price, sell_time| | |
current_profit = buy_price - sell_price | |
if current_profit > profit_so_far && buy_time < sell_time | |
profit_so_far = current_profit | |
end | |
end | |
end | |
profit_so_far | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment