Description
Given array prices where prices[i] is the price of the ith item, for each item find the first item to the right with price <= current price. That becomes the discount. Return final prices.
Examples
prices = [8,4,6,2,3][4,2,4,2,3]Discounts applied.
prices = [10,1,1,6][9,0,1,6]For price 10, the next smaller/equal element is 1, so discount is 1 (final price: 10-1=9). For price 1, the next smaller/equal element is 1, so discount is 1 (final price: 1-1=0). For the third element (price 1), no subsequent element is smaller or equal, so no discount (final price: 1). For price 6, no more elements exist, so no discount (final price: 6).
prices = [7,9,2][5,7,2]For price 7, the next smaller/equal element is 2, so discount is 2 (final price: 7-2=5). For price 9, the next smaller/equal element is 2, so discount is 2 (final price: 9-2=7). For price 2, there are no more elements after it, so no discount applies (final price: 2).
Constraints
- •
1 ≤ n ≤ 500