Skip to content

Instantly share code, notes, and snippets.

@jpoechill
Last active May 25, 2017 04:15
Show Gist options
  • Save jpoechill/b800c9d848d7141ba8867515849e9496 to your computer and use it in GitHub Desktop.
Save jpoechill/b800c9d848d7141ba8867515849e9496 to your computer and use it in GitHub Desktop.
Apple Stocks via. InterviewCake
function getMaxProfit(stockPrices) {
var allPurchases = {}
var allPurchasesArr = []
for (var i = 0; i < (stockPrices.length); i++) {
var stockClone = stockPrices.slice(0)
var currRemoved = stockClone.splice(0, i+1)
var maxStockRight = Math.max(...stockClone)
allPurchases[stockPrices[i]] = stockPrices[i] - maxStockRight
allPurchasesArr.push(stockPrices[i] - maxStockRight);
}
var bestBuy = Math.min(...allPurchasesArr)
for (var t in allPurchases) {
if (allPurchases[t] == bestBuy) {
console.log("The best buy is: " + [t]);
}
}
}
var stockPricesYesterday = [10, 7, 5, 8, 11, 9];
getMaxProfit(stockPricesYesterday);
// grab current element
// find highest value to the right of element
// gather sum
// add to hash
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment