You receive an integer array prices, in which prices[i] represents the cost of the item at index i in a store.
For every item, scan the items positioned after it and find the earliest one priced at most as much as the current item. When one is found, subtract that item's price as the current item's discount. If no such later item exists, no discount applies.
Produce an integer array answer such that answer[i] gives the amount paid for item i after its discount is applied.
Example 1:
Input: prices = [7, 3, 5, 2, 4]
Output: [4, 1, 3, 2, 4]
Explanation:
Item 0 is reduced by 3, item 1 is reduced by 2, and item 2 is reduced by 2. The final two items have no later item that qualifies for a discount.
input = [7, 3, 5, 2, 4][4,1, 3, 2, 4]
We start with the given prices: [7, 3, 5, 2, 4].
Example 2:
Input: prices = [2, 4, 6, 8]
Output: [2, 4, 6, 8]
Explanation:
Each price is followed only by larger prices, so none of the items receives a discount.
Example 3:
Input: prices = [9, 2, 2, 7]
Output: [7, 0, 2, 7]
1 <= prices.length <= 5001 <= prices[i] <= 10^3input = [7, 3, 5, 2, 4][4,1, 3, 2, 4]
We start with the given prices: [7, 3, 5, 2, 4].