Last active
May 25, 2017 04:15
-
-
Save jpoechill/b800c9d848d7141ba8867515849e9496 to your computer and use it in GitHub Desktop.
Apple Stocks via. InterviewCake
This file contains 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
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