Problem · Greedy
Future Stock Prices
Given an unordered list of future stock prices, what is the maximum amount of profit that you could generate from a starting amount of $1,000.00
Rules:
Complete the function calculateMaxProfit in the editor.
calculateMaxProfit has the following parameter:
String[] stockPrices: an array of stock price information
Returns
int: the maximum profit rounded to the nearest dollar
Examples
01 · Example 1
stockPrices = ["CSCO,10/18/2024,41.89", "AMZN,10/10/2024,113.67", "AMZN,10/18/2024,120.5", "CSCO,10/10/2024,43.12"] return = 60
Buy 8.797 AMZN @ $113.67 on 10/10/2024 and sell @ $120.5 on 10/18/2024. Profit = $60
02 · Example 2
stockPrices = ["IBM,12/01/2023,132.05", "IBM,12/03/2023,135.19", "IBM,12/18/2023,134.07", "AAPL,12/01/2023,187.19", "AAPL,12/04/2023,164.33", "AAPL,12/20/2023,180.94", "AAPL,12/21/2023,179.65", "GOOG,12/01/2023,116.41", "GOOG,12/07/2023,111.36", "GOOG,12/19/2023,112.19"] return = 127
Buy 7.573 IBM @ $132.05 on 12/1/2023 and sell @ 135.19 on 12/3/2023. Buy 6.230 AAPL on 12/4/2023 @ $164.33 and sell on 12/20/2023 @ $180.94. Profit = $127
Constraints
🍓🍓More Hudson River Trading problems
public int calculateMaxProfit(String[] stockPrices) {
// write your code here
}
stockPrices["CSCO,10/18/2024,41.89", "AMZN,10/10/2024,113.67", "AMZN,10/18/2024,120.5", "CSCO,10/10/2024,43.12"]
expected60
sign in to submit