Final Prices With a Special Discount

Easy

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

Input:prices = [8,4,6,2,3]
Output:[4,2,4,2,3]
Explanation:

Discounts applied.

Input:prices = [10,1,1,6]
Output:[9,0,1,6]
Explanation:

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).

Input:prices = [7,9,2]
Output:[5,7,2]
Explanation:

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

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!