21. Final Prices With a Special Discount in a Shop
Next Smaller Element
Given the array prices
where prices[i]
is the price of the ith
item in a shop. There is a special discount for items in the shop, if you buy the ith
item, then you will receive a discount equivalent to prices[j]
where j
is the minimum index such that j > i
and prices[j] <= prices[i]
, otherwise, you will not receive any discount at all.
Return an array where the ith
element is the final price you will pay for the ith
item of the shop considering the special discount.
Example 1:
Example 2:
Example 3:
Solution: (Stack: Next Smaller Element)
Time Complexity: O(n)
Last updated