【数组】1475. 商品折扣后的最终价格(简单)

python-leetcode-1475. 商品折扣后的最终价格 这个问题要求我们根据给定的条件,在商店中购买商品时计算折扣后的最终价格。具体地,假设我们有一个商品价格数组prices,并且每个商品都有一个折扣价格,这个折扣来源于后面某个价格不大于当前商品价格的商品。我们可以用单调栈来高效地解决这个问题。 阅读详情

【题目】
给你一个数组 prices ,其中 prices[i] 是商店里第 i 件商品的价格。商店里正在进行促销活动,如果你要买第 i 件商品,那么你可以得到与 prices[j] 相等的折扣,其中 j 是满足 j > i 且 prices[j] <= prices[i] 的 最小下标 ,如果没有满足条件的 j ,你将没有任何折扣。请你返回一个数组,数组中第 i 个元素是折扣后你购买商品 i 最终需要支付的价格。
【示例1】
输入:prices = [8,4,6,2,3]
输出:[4,2,4,2,3]
解释:
商品 0 的价格为 price[0]=8 ,你将得到 prices[1]=4 的折扣,所以最终价格为 8 - 4 = 4 。
商品 1 的价格为 price[1]=4 ,你将得到 prices[3]=2 的折扣,所以最终价格为 4 - 2 = 2 。
商品 2 的价格为 price[2]=6 ,你将得到 prices[3]=2 的折扣,所以最终价格为 6 - 2 = 4 。
商品 3 和 4 都没有折扣。
【示例2】
输入:prices = [1,2,3,4,5]
输出:[1,2,3,4,5]
解释:在这个例子中,所有商品都没有折扣。
【示例3】
输入:prices = [10,1,1,6]
输出:[9,0,1,6]
【提示】
1 <= prices.length <= 500
1 <= prices[i] <= 10^3
【代码】

class Solution {
public:
    vector<int> finalPrices(vector<int>& prices) {
        int len=prices.size();
        for(int i=0;i<len-1;i++){
            for(int j=i+1;j<len;j++){
                if(prices[j]<=prices[i]){
                    prices[i]-=prices[j];
                    break;
                }
            }
        }
        return prices;
    }
};
elegant(ELEctron Generation ANd Tracking)入门指南(1) elegant (ELEctron Generation ANd Tracking),美国先进光子源(APS)所用的圆环加速器的模拟程。 阅读详情

相关推荐

Astah 9.0升级步骤(Widnows/Mac OS)

Astah Professional是一款全新的轻量级UML建模工具。软件集思维导图和UML建模于一体,采用100%纯JAVA构建, 兼容性强,不仅能够实现分布式建模、项目合并,还能够实现和其它建模工具的交互。

tequiero777的博客 2828

1475. 商品折扣后的最终价格

1475. 商品折扣后的最终价格(力扣1475)

weixin_44458954的博客 225

Zemax非序列模式实战:手把手教你设计光纤耦合圆光斑整形系统(附Thorlabs透镜选型)

本文提供了一份基于Zemax非序列模式的光纤耦合圆光斑整形系统实战设计指南。文章详细讲解了从圆柱体光源建模、Thorlabs商业透镜库选型调用,到系统搭建、光线追迹与优化的完整流程,旨在帮助工程师高效完成从仿真到落地的光束整形系统设计。

weixin_28335283的博客 633

编程练习:机票打折

输入机票原价(33 到 44 位的正整数,单位:元),再输入机票打折率(小数点后最多一位数字)。编程计算打折后机票的实际价格(单位:元。计算结果要将个位数四舍五入到十位数“元”)。

lxtzs666lxt的博客 2373

【实战指南】Jaspersoft Studio + JasperReports:从零构建企业级PDF报表

本文详细介绍了如何使用Jaspersoft Studio和JasperReports从零构建企业级PDF报表。通过可视化设计、数据连接、模板设计到最终输出的全链路支持,这套组合方案显著降低了报表开发的维护成本。文章还提供了开发环境搭建、中文乱码解决方案、Java集成代码及性能优化技巧,适合需要高效生成复杂PDF报表的开发者。

weixin_42666036的博客 247

信贷审批中的那些坑:从零开始搭建风控策略体系的5个关键步骤

本文深入探讨了信贷审批中的风控策略体系搭建,从数据陷阱识别、规则冲突诊断到反欺诈攻防,提供了5个关键步骤的实战指南。重点解析了如何通过特征工程、决策树优化和模型可解释性技术,构建动态平衡的风控体系,有效降低逾期风险并提升审批效率。

weixin_29253711的博客 416

PTA L1-051 打折 C++实现

打折 去商场淘打折商品时,计算打折以后的价钱是件颇费脑子的事情。例如原价 ¥988,标明打 7 折,则折扣价应该是 ¥988 x 70% = ¥691.60。本题就请你写个程序替客户计算折扣价。 输入格式: 输入在一行中给出商品的原价(不超过1万元的正整数)和折扣(为[1, 9]区间内的整数),其间以空格分隔。 输出格式: 在一行中输出商品的折扣价,保留小数点后 2 位。 输入样例: 988 7 ...

weixin_43646424的博客 2166

[ADS]单端电感和差分电感的感值和Q值计算公式

单端电感和差分电感的感值和Q值计算公式

乾巽的博客 1万+

C++ 11(购物打折)

#includevoid main(){ double num1=0.0; double num2=0.0;    cout    cin>>num1; if(num1>1000000)  num2=a;    else if(num1>500000)  num2=b;    else if(num1>300000)  num2=c;    else if(num1>150000)

我为杰沦 2203

【VSCode Python无法格式化代码】

提示:需要Install Formatter…安装插件autopep8,点击install。出现Format Document。

AIFRZ的博客 1万+

KEPServerEX 6 之 EX 6.11.718.0 发布说明

KEPServerEX 6.11.718.0 发布说明

-雲飛兄-博客 2882

C++练习02 如何算总价

大家在生活中都会去超市买东西,那必然会涉及到金额的问题,用最基本的C++算法去计算商品的金额问题。 可能会用到制表,在C++中&quot;\t&quot;为制表符,在列购物清单时可以使用制表符进行间隔。 在使用制表符时需要注意:由于中文和英文字符的长度不同,如果想要中文和英文对齐,英文字符的制表符要比中文的多一个。 例题:输入商品的数量和购买商品的单价,输出“金额是:” 如显示器提示: 请输入商品的单价:(输入...

weixin_44275777的博客 4976

YOLOv8交通监控全方案:精准车辆计数+违章(闯红灯/压线)识别,拿来就落地!

作为深耕计算机视觉落地的开发者,我见过太多交通监控的“伪智能”——传统系统要么车辆计数不准(漏计/重复计数),要么违章识别滞后(靠人工回看录像),甚至连“闯红灯、压线”这类常见违章都无法实时判定,完全满足不了实际交通管理需求。

专注于Python爬虫开发,分享爬虫技巧、项目实战与反爬经验,使用Scrapy、BeautifulSoup等工具,解决数据抓取难题。 647

Leetcode.1475 商品折扣后的最终价格 简单

然后判断是否栈空,栈空就说明后面没有比此数组元素还要小的元素,就是原价,栈不为空就打折,此时打折的价格就是减去栈顶元素。前面的解法使用了双重循环遍历,时间复杂度较大,单调栈的核心思维就是寻找右边更小的元素时不需要遍历数组,如果栈不为空且栈顶元素比此位置数组元素大,就出栈,重复操作,直到栈为空或者栈顶元素小于此数组元素。空间复杂度为O(1),返回值不计入空间复杂度。时间复杂度为O(n),一个循环遍历数组。时间复杂度为O(n²),n是数组长度。空间复杂度为O(n),栈占用空间。从数组右端开始遍历,...

qq_51599283的博客 301

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

【Python】【难度:简单】Leetcode 5420. 商品折扣后的最终价格【完】

给你一个数组prices,其中prices[i]是商店里第i件商品的价格。 商店里正在进行促销活动,如果你要买第i件商品,那么你可以得到与 prices[j] 相等的折扣,其中j是满足j > i且prices[j] <= prices[i]的最小下标,如果没有满足条件的j,你将没有任何折扣。 请你返回一个数组,数组中第i个元素是折扣后你购买商品 i最终需要支付的价格。 示例 1: 输入:prices = [8,4,6,2,3] 输出:[4,...

thomashhs12的博客 641

android 蓝牙通讯demo

android 下蓝牙通讯demo,实现了发现蓝牙,使能蓝牙,通过蓝牙发送消息。

LeetCode: 1475. 商品折扣后的最终价格

给你一个数组 prices,其中 prices[i] 是商店里第 i 件商品的价格。 商店里正在进行促销活动,如果你要买第 i 件商品,那么你可以得到与 prices[j] 相等的折扣,其中 j 是满足 j > i 且 prices[j]

WZHao000的博客 2137

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

LeetCode-1475. 商品折扣后的最终价格【栈 数组 单调栈】

商店里正在进行促销活动,如果你要买第 i 件商品,那么你可以得到与 prices[j] 相等的折扣,其中 j 是满足 j > i 且 prices[j]

zik的博客 1068

【LeetCode每日一题】——1475.商品折扣后的最终价格

给你一个数组 prices ,其中 prices[i] 是商店里第 i 件商品的价格。 商店里正在进行促销活动,如果你要买第 i 件商品,那么你可以得到与 prices[j] 相等的折扣,其中 j 是满足 j > i 且 prices[j]

IronmanJay 1159

【leetcode刷题】73.商品折扣后的最终价格——Java版

单调栈——⭐️

公号:一条coding 1万+

leetcode商品折扣后的最终价格(1475)

给你一个数组 prices ,其中 prices[i] 是商店里第 i 件商品的价格。 商店里正在进行促销活动,如果你要买第 i 件商品,那么你可以得到与 prices[j] 相等的折扣,其中 j 是满足 j > i 且 prices[j] <= prices[i] 的 最小下标 , 如果没有满足条件的 j ,你将没有任何折扣。 请你返回一个数组,数组中第 i 个元素是折扣后你购买商品 i 最终需要支付的价格。 输入:prices = [8,4,6,2,3] 输出:[4,2,4,2,3] .

you are sherlocked by me! 906

leetcode 1475. 商品折扣后的最终价格

1475. 商品折扣后的最终价格 难度简单7 给你一个数组prices,其中prices[i]是商店里第i件商品的价格。 商店里正在进行促销活动,如果你要买第i件商品,那么你可以得到与prices[j]相等的折扣,其中j是满足j > i且prices[j] <= prices[i]的最小下标,如果没有满足条件的j,你将没有任何折扣。 请你返回一个数组,数组中第i个元素是折扣后你购买商品i最终需要支付的价格。 示例 1: 输入:p...

fang156239305的博客 342

1475.商品折扣后的最终价格(Python)

1475.商品折扣后的最终价格 给你一个数组prices ,其中prices[i]是商店里第i件商品的价格。 商店里正在进行促销活动,如果你要买第i件商品,那么你可以得到与prices[j]相等的折扣, 其中j是满足j > i且prices[j] <= prices[i]的最小下标 ,如果没有满足条件的j ,你将没有任何折扣。 请你返回一个数组,数组中第i个元素是折扣后你购买商品i最终需要支付的价格。 示例1: 输入:prices = [8, 4, 6, 2, 3] 输出:[4, 2, 4, 2

playboygogogo的博客 1178

力扣1475.商品折扣后的最终价格

同理,对于第2件商品i = 2, price[2] = 6, 此时判断(prices[1] = 4) >= (price[2] = 6),显然不成立,这说明对于第1件商品来说,这个6太大了。当前商品i的价格price[i],放在栈顶的那件商品的价格price[k]是否满足price[k] >= price[i],如果满足,就说明:当前这件 i,刚好是前面那件 k 右边第一个。商品 2 的价格为 price[2]=6 ,你将得到 prices[3]=2 的折扣,所以最终价格为 6 - 2 = 4。

m0_71641460的博客 533

LeetCode 每日一题——1475. 商品折扣后的最终价格

LeetCode 每日一题——1475. 商品折扣后的最终价格

SK_Jaco的博客 441
上一篇: 【数组】面试题 01.01. 判定字符是否唯一(简单)
下一篇: 【数组】1385. 两个数组间的距离值(简单)
菜菜2022
博客等级 码龄9年 67粉丝 1033原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值