Description
Solutions
Future Stock Prices
🀘 INTERN

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:

  • You can trade fractional shares (e.g. if there were shares for $400.00, you could buy/sell 2.5 of them for $1,000.00)
  • All trades occur instantaneously and do not incur any transaction costs.
  • Shares may only be bought/sold on a date that you have a known price.
  • Short selling is not allowed.
  • You do not need to have a position at all times (at any time, if you cannot identify a profitable trade, you do not have to trade).
  • Round final answer to nearest dollar.
  • Do not assume input tuple will be sorted in any manner.
  • Future prices will be given as a list in the following format: [Stock, Date, Price]
  • 𓇼 ⋆.˚ 𓆝 Credit to robot 𓆑⋆.˚ π“‡Όπ“‚ƒπŸ–Š

    Example 1:

    Input:  prices = ["CSCO,10/18/2024,41.89", "AMZN,10/10/2024,113.67", "AMZN,10/18/2024,120.5", "CSCO,10/10/2024,43.12"]
    Output: 60
    Explanation:
    Buy 8.797 AMZN @ $113.67 on 10/10/2024 and sell @ $120.5 on 10/18/2024. Profit = $60

    Example 2:

    Input:  prices = ["IBM,12/01/2023,132.05", "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"]
    Output: 127
    Explanation:
    Buy 7.573 IBM @ $132.05 on 12/01/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

    Example 3:

    Input:  prices = ["INTC, 12/01/2023, 30.00", "INTC, 12/05/2023, 35.00", "INTC, 12/10/2023, 33.00", "AAPL, 12/02/2023, 150.00", "AAPL, 12/06/2023, 155.00", "AAPL, 12/11/2023, 160.00", "INTC, 12/15/2023, 36.00" "INTC, 09/10/2024, 36.26", "INTC, 12/04/2023, 33.39", "AAPL, 12/22/2023, 197.61", "AAPL, 07/20/2025, 181.71", "INTC, 10/27/2024, 35.66", "AAPL, 01/30/2025, 178.68", "AAPL, 12/21/2024, 176.22", "AAPL, 05/14/2024, 182.66", "INTC, 08/15/2024, 35.23", "AAPL, 02/20/2025, 196.47", "INTC, 03/29/2025, 32.13", "INTC, 06/28/2025, 32.96", "INTC, 07/11/2025, 35.63"]
    Output: 343
    Explanation:
    πŸ₯‘πŸ₯‘
    Constraints:
      N/A
    Thumbnail 0
    Thumbnail 1
    Testcase

    Result
    Case 1

    input:

    output: