Maximum Income from Asset Trading
Updated
Problem statement
You are given prices, where prices[i] is the price of one asset on day i. You begin before day 0 holding exactly one asset at no purchase cost.
On each day, you may perform at most one action:
- If you hold an asset, you may sell it at that day's price.
- If you do not hold an asset, you may buy one at that day's price. You always have enough cash to buy.
- You may also do nothing.
You may hold at most one asset at any time. Income is total sale proceeds minus purchase costs. Return the maximum possible income modulo 1,000,000,000 (the last nine digits).
Function
maximumIncome(prices: int[]) → intExamples
Example 1
prices = [4,1,2,3]return = 6Sell the initial asset for 4, buy for 1, and sell again for 3. The income is 4 - 1 + 3 = 6.
Example 2
prices = [1,5,3,8]return = 10Sell at 5 after the first rise, then buy at 3 and sell at 8. Including the free initial asset, the maximum income is 10.
Example 3
prices = [5,4,3]return = 5Sell the initial asset on day 0; no later trade is profitable.
Constraints
1 <= prices.length <= 1000000 <= prices[i] <= 1000000000
More Jefferies Online Assessment questions
Similar questions at other companies
- Best Time to Buy and Sell StockFlexTrade · OAFlexTrade interview questions
- Split Array to Minimize the Largest SumInfosys · OAInfosys interview questions
- Split Array Largest SumAuxoAI · OA
- Get Minimum IncrementIBM · OAIBM interview questions
- Maximum Even-Sum Neighboring PairsDRW · OADRW interview questions
- Minimize Total Input Cost (for LTMS)Salesforce · OASalesforce interview questions
- Minimum Number of Taps to Water a GardenGoldman Sachs · OAGoldman Sachs interview questions
- Maximize Protected City PopulationAmazon · OA
- Find Minimum Cost to Shift MachinesCisco · OACisco interview questions
- Network FormationPalantir · OAPalantir interview questions