2025-08-03 10:56:55 +0000 UTC

Final Prices With a Special Discount in a Shop

Code

class Solution:
    def finalPrices(self, prices: List[int]) -> List[int]:
        stack = deque()
        for i in range(len(prices)):
            price = prices[i]
            while True:
                if not stack:
                    break
                last_i, last_price = stack[-1]
                if last_price >= price:
                    prices[last_i] -= price
                    stack.pop()
                else:
                    break
            stack.append((i, price))
        return prices