Skip to content

Instantly share code, notes, and snippets.

@jonog

jonog/stocks.md Secret

Last active August 16, 2020 01:43
Show Gist options
  • Save jonog/ecd6a02adbe3bd496cefcd63d363c876 to your computer and use it in GitHub Desktop.
Save jonog/ecd6a02adbe3bd496cefcd63d363c876 to your computer and use it in GitHub Desktop.

Scenario

Suppose we could access yesterday's stock prices as a list, where:

  • The indices are the time in minutes past trade opening time, which was 10:00am local time.
  • The values are the price in dollars of the Latitude Financial stock at that time.
  • So if the stock cost $5 at 11:00am, stock_prices_yesterday[60] = 5.

Write an efficient function that takes an array of stock prices and returns the best profit I could have made from 1 purchase and 1 sale of 1 Latitude Financial stock yesterday.

For example:

var stock_prices_yesterday = [10, 7, 5, 8, 11, 9];

get_max_profit(stock_prices_yesterday)
# returns 6 (buying for $5 and selling for $11)

You must buy before you sell. You may not buy and sell in the same time step (at least 1 minute must pass).

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