LeetCode 1475 Final Prices With a Special Discount in a Shop (Python)

Posted by 小明MaxMing on January 17, 2022

题目

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.

解题思路

把每个价格加入栈中,如果当前价格比栈顶元素小,则对栈顶元素进行打折

代码

class Solution:
    def finalPrices(self, prices: List[int]) -> List[int]:
        s = []
        for i, p in enumerate(prices):
            while s and prices[s[-1]] >= p:
                prices[s.pop()] -= p
            s.append(i)
        return prices

视频讲解 YouTube<--欢迎点击订阅

视频讲解 bilibili<--欢迎点击订阅