LEETCODE | PYTHON | 1475 | 商品折扣后的最终价格

LEETCODE | PYTHON | 1475 | 商品折扣后的最终价格

1. 题目

给你一个数组 prices ,其中 prices[i] 是商店里第 i 件商品的价格。

商店里正在进行促销活动,如果你要买第 i 件商品,那么你可以得到与 prices[j] 相等的折扣,其中 j 是满足 j > i 且 prices[j] <= prices[i] 的 最小下标 ,如果没有满足条件的 j ,你将没有任何折扣。

请你返回一个数组,数组中第 i 个元素是折扣后你购买商品 i 最终需要支付的价格。

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/final-prices-with-a-special-discount-in-a-shop
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

2. 代码

class Solution:
    def finalPrices(self, prices: List[int]) -> List[int]:

        #prices = [10,1,1,6]

        #初始化
        res = []
        l = len(prices)

        #遍历判断
        for i in range(l):

            #确定当前价格
            Price = prices[i]

            #确定折扣
            index = i + 1
            while index < l and prices[index] > Price:
                index = index + 1

            #判断是否存在折扣
            if index < l:
                Discount = prices[index]
            else:
                Discount = 0

            #更新结果
            res.append(Price - Discount)
        
        return res
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值