FastPrepMaximum Income from Asset Trading

Maximum Income from Asset Trading

Jefferies logoJefferies● EasyFULLTIMEOA
Learn

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[]) → int

Examples

Example 1

prices = [4,1,2,3]return = 6

Sell 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 = 10

Sell 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 = 5

Sell the initial asset on day 0; no later trade is profitable.

Constraints

  • 1 <= prices.length <= 100000
  • 0 <= prices[i] <= 1000000000

More Jefferies problems

See Jefferies hiring insights
public int maximumIncome(int[] prices) {
    // Write your code here.
}
prices[4,1,2,3]
expected6
Checking account…