FastPrepMinimum Amount

Minimum Amount

Belvedere Trading logoBelvedere Trading● EasyFULLTIMEOA
Learn

Problem statement

You are given an integer array prices. The items must be purchased from left to right.

  • The first item is purchased at full price.
  • For every later item, the discount equals the minimum original price among all previously purchased items.
  • An item's final price cannot be negative.

Return the total amount paid for all items.

Function

minimumAmount(prices: int[]) → long

Examples

Example 1

prices = [2,5,1,4]return = 8

The four costs are 2, 5 - 2 = 3, max(1 - 2, 0) = 0, and 4 - 1 = 3. Their sum is 8.

Example 2

prices = [4,9,2,3]return = 10

The four costs are 4, 9 - 4 = 5, max(2 - 4, 0) = 0, and 3 - 2 = 1. Their sum is 10.

Example 3

prices = [7]return = 7

The only item is the first item, so it is purchased at full price.

Constraints

  • 1 <= prices.length <= 10^5.
  • 1 <= prices[i] <= 10^7.

More Belvedere Trading problems

See Belvedere Trading hiring insights
public long minimumAmount(int[] prices) {
  // write your code here
}
prices[2,5,1,4]
expected8
Checking account…