[leetcode] 1475. Final Prices With a Special Discount in a Shop

Description

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:

Input: prices = [8,4,6,2,3]
Output: [4,2,4,2,3]
Explanation: 
For item 0 with price[0]=8 you will receive a discount equivalent to prices[1]=4, therefore, the final price you will pay is 8 - 4 = 4. 
For item 1 with price[1]=4 you will receive a discount equivalent to prices[3]=2, therefore, the final price you will pay is 4 - 2 = 2. 
For item 2 with price[2]=6 you will receive a discount equivalent to prices[3]=2, therefore, the final price you will pay is 6 - 2 = 4. 
For items 3 and 4 you will not receive any discount at all.

Example 2:

Input: prices = [1,2,3,4,5]
Output: [1,2,3,4,5]
Explanation: In this case, for all items, you will not receive any discount at all.

Example 3:

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

Constraints:

  • 1 <= prices.length <= 500
  • 1 <= prices[i] <= 10^3

分析

题目的意思是:给你一个数组,

代码

class Solution:
    def finalPrices(self, prices: List[int]) -> List[int]:
        n=len(prices)
        res=[]
        for i in range(n):
            flag=True
            for j in range(i+1,n):
                if(prices[j]<=prices[i]):
                    res.append(prices[i]-prices[j])
                    flag=False
                    break
            if(flag):
                res.append(prices[i])
        return res

参考文献

[LeetCode] Easy Python Stack Beats 93% with Comments!

背景描述 2016 年全球生态足迹 您所在国家消耗的资源是否超过一年产生的资源? 数据说明 上下文 生态足迹衡量的是特定人口生产其消耗的自然资源(包括植物性食品和纤维产品、牲畜和鱼产品、木材和其他林产品、城市基础设施的空间)和吸收其废物(尤其是碳排放)所需的生态资产。该足迹跟踪了六类生产性表面积的使用情况:农田、牧场、渔场、建成区(或城市)土地、森林面积和土地上的碳需求。 一个国家的生物承载力代表其生态资产的生产力,包括农田、牧场、林地、渔场和建筑用地。这些区域,尤其是如果不采伐,也可以吸收我们产生的大部分废物,尤其是我们的碳排放。 生态足迹和生物承载力都以全球公顷表示,即具有全球可比性的标准化公顷数与世界平均生产力。 如果一个种群的生态足迹超过该地区的生物承载力,则该区域就会出现生态赤字。它对其陆地和海洋所能提供的商品和服务的需求——水果和蔬菜、肉类、鱼类、木材、服装用棉花和二氧化碳吸收——超过了该地区生态系统可以更新的需求。生态赤字地区通过进口、变现自己的生态资产(如过度捕捞)和/或向大气中排放二氧化碳来满足需求。如果一个地区的生物承载力超过其生态足迹,它就拥有生态保护区。 确认 生态足迹测量是由不列颠哥伦比亚大学的 Mathis Wackernagel 和 William Rees 构思的。生态足迹数据由 Global Footprint Network 提供。 灵感 您的国家是否存在生态赤字,消耗的资源超过了每年的产量?哪些国家的生态赤字或保护区最大?他们的消费量是比普通国家少还是多?2017 年地球超载日,即日历上人类使用一年自然资源的日子,何时发生?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

农民小飞侠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值