Skip to main content

Maximize Portfolio Value

easy
Software engineer

You are given a list of securities. For the $i^{th}$ security, you are provided with its symbol, current price ($P_i$), predicted future price ($S_i$), and a maximum number of shares you are willing to purchase ($A_i$). With a limited amount of funds $m$, your goal is to maximize the potential future value of your portfolio.

Note that fractional share trading is allowed. If a security's predicted future price is lower than its current price, it is better to keep your cash rather than purchasing that security, as cash maintains its value ($1 today = $1 in the future).

Return the maximum total future value of the portfolio (including remaining cash).

Example 1

Input

m = 140, sec = [["AAPL", "15", "45", "3"],["BYND", "40", "50", "3"], ["SNAP", "25", "35", "3"],["TSLA", "30", "25", "4"]]

Output

265.0

Explanation

To maximize future value, we calculate the 'return ratio' ($S_i / P_i$) for each security where $S_i > P_i$. 
1. AAPL: $45/15 = 3.0$
2. SNAP: $35/25 = 1.4$
3. BYND: $50/40 = 1.25$
4. TSLA: $25/30 = 0.83$ (Ignore, as return is < 1)

We buy the maximum shares of the highest return first:
- Buy 3 shares of AAPL: Cost $45, Future Value $135. Remaining $m = 95$.
- Buy 3 shares of SNAP: Cost $75, Future Value $105. Remaining $m = 20$.
- Buy 0.5 shares of BYND: Cost $20, Future Value $25. Remaining $m = 0$.
Total Future Value: $135 + 105 + 25 = 265$.

Example 2

Input

m = 100, sec = [["AAPL", "15", "10", "3"], ["BYND", "40", "35", "3"]]

Output

100.0

Explanation

Since all predicted future prices are lower than current prices, we buy nothing. The future value is just our remaining cash.

Example 3

Input

m = 50, sec = [["SNAP", "20", "30", "5"], ["AAPL", "50", "70", "2"]]

Output

75.0

Constraints

  • 1 <= sec.length <= 10⁴
  • 1 <= Pi, Si <= 10⁵
  • m > 0
  • Fractional shares are allowed.

ScreeningOnsite

ArrayGreedyMathSorting
Language
Code editor loads in the browser.

Output

Input

140
[["AAPL","15","45","3"],["BYND","40","50","3"],["SNAP","25","35","3"],["TSLA","30","25","4"]]

Expected

265

Your output

Run to see your output.