Dodomeki
Sign in

51. Discount at the Next Lower Price

Easy

A store records product prices in precos, in the order they were presented. For each product, look for the first product to its right whose price is less than or equal to its own; if one exists, subtract that price from the current price. If none exists, the price stays the same.

Return a list of the final prices in the same order as precos.

Example 1
Input: precos = [8, 4, 6, 2, 3]
Output: [4, 2, 4, 2, 3]
Explanation: The price 8 gets a discount of 4, the first price to its right that is less than or equal to it.
Example 2
Input: precos = [1, 2, 3]
Output: [1, 2, 3]
Explanation: No price has a price less than or equal to it on its right.
Example 3
Input: precos = [5, 5]
Output: [0, 5]
Explanation: The first product gets a discount of 5; the second has no product to its right.
Constraints
  • 0 <= precos.length <= 10^5
  • 0 <= precos[i] <= 10^5
StackArrays
Caso 1
precos =
[8,4,6,2,3]
Expected =
[4,2,4,2,3]
Caso 2
precos =
[1,2,3]
Expected =
[1,2,3]
Caso 3
precos =
[5,5]
Expected =
[0,5]

Run checks these 3 cases. Submit runs all 11, including hidden ones.