Created
April 11, 2016 16:06
-
-
Save mmloveaa/ccc7746b9e40e3130634dcc2017556c8 to your computer and use it in GitHub Desktop.
4-11 Hacker Rank
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
Imagine you're a stock trader. You'd like to compare how you actually did trading a stock yesterday with how well you could have done. To do this, you'll need to find the maximum profit you could have made by buying once and selling once yesterday. | |
Given an array of stock prices representing the price of the stock each minute, write a function that returns the best profit you could have made making 1 purchase and 1 sale of that stock. | |
For example: | |
var stockPrices = [9, 6, 7, 5, 10, 8]; | |
findMaxProfit(stockPrices) | |
// returns 5 (buying at $5 and selling for $10) | |
Caveats: | |
1) You must buy before you sell. | |
2) You may not buy and sell at the same time (at least 1 minute must pass). | |
function findMaxProfit(stockPrices) { | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment