leetcode 309.Best Time to Buy and Sell Stock with Cooldown (time : O(n))

PLSQL 14.0.6 下载使用教程 众所周知,PLSQL 只是一个oracle第三方开发测试工具,它依赖oracle 客户端搭配使用,但是oracle 客户端有2G,相当庞大,不建议安装。所以我们如果下载安装好PLSQL,再下载好轻量级的支持远程链接的oracle客户端,这样搭配使用可以的话就最好了,答案是可以的。 第一步: PLSQL 下载与安装 PLSQL 14.0.6 下载地址 : https://www.allroundautomations.com/registered-plsqldev/ 下载下来,除了安装路径更改一下, 阅读详情

Say you have an array for which the ith element is the price of a given stock on day i.

Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times) with the following restrictions:

You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
After you sell your stock, you cannot buy stock on next day. (ie, cooldown 1 day)
Example:

Input: [1,2,3,0,2]
Output: 3
Explanation: transactions = [buy, sell, cooldown, buy, sell]

还是买卖股票的问题,但是这次不能卖了隔天就买,卖了之后要停一天才能再买入下一个。
但是如果买了,隔天是可以卖的。

思路:
前面123, 188题是用dp保存前面的profit,然后今天选择交易或不交易。

现在如果还用dp保存profit,因为买入的话要cool down一天,所以要参考dp[i - 2],但是不能确定在dp[i - 2]的时候是不是手上有股票,如果有,就不能在它的基础上再买。
所以dp[i] 不一定能在dp[i - 2]的基础上交易

同理,如果dp[i -1]的时候手上持有股票,在dp[i]的时候是可以卖的。
所以dp[i] 有时是可以在dp[i - 1]的基础上交易的

上述思路具有不确定性,所以要转换思路,因为今天能不能交易取决于前面手上是否持有股票,如果有,那么可以直接卖掉,不用 cool down一天。如果没有,就要考虑cool down一天的条件,只能参考两天前的profit。

所以定义两个dp数组,一个是持有股票的dp:hold[n], 一个是手上没有股票的dp:unhold[n]

边界条件:
hold[0] = -prices[0],
因为在0处如果持有股票,就要用prices[0]的价钱买入,profit就是-prices[0]
hold[1] = max(-prices[0], -prices[1])
因为不能同时买入两个股票,必须卖了才能买,而第二天是不能同时卖掉第一个再买入第二个的,不满足cool down,所以只能要么买prices[0], 要么买prices[1], 选其中profit较大的
unhold[0] = 0, 因为不买,没有profit

state变化:
hold[i]: hold表示持有,即买入。如果在i处不买,就保留hold[i - 1],
如果买入,那么前提是i-2处已经卖出,即unhold[i - 2]
profit = unhold[i - 2] - prices[i]
取买和不买的较大profit:max(hold[i - 1], unhold[i - 2] - prices[i])

unhold[i]: 不持有股票,即卖出。i处不卖,则保留unhold[i - 1]
i处卖掉,hold[i - 1] + prices[i], 取较大值

最后要取最大收益,也就是手上是没有股票的状态,unhold[n - 1]
(手上还有股票说明还有一部分收益没变现,肯定不是最大的)

   //1ms
    public int maxProfit(int[] prices) {
        if (prices == null || prices.length == 0) {
            return 0;
        }
        
        int[] hold = new int[prices.length];
        int[] unhold = new int[prices.length];
        
        hold[0] = -prices[0];      
        
        for (int i = 1; i < prices.length; i++) {
            if (i == 1) {
                hold[1] = Math.max(-prices[0], -prices[1]);
            } else {
                hold[i] = Math.max(hold[i - 1], unhold[i - 2] - prices[i]);
            }
            unhold[i] = Math.max(unhold[i - 1], hold[i - 1] + prices[i]);
        }
        
        return unhold[prices.length - 1];
    }
从后端角度浅谈抗辐射设计----三模冗余(TMR) 三模冗余设计(TMR),是最常用的一种容错技术,三个模块采取相同的操作,以多数相同的输出作为表决系统的正确输出,即3取2,因为三个模块是相互独立的,而其中两个模块同时出错的概率是极小的,因此便可保证系统输出的可信度。然而这个设计也会存在面积浪费,功耗的增加,而且由于是冗余的设计,在后续进行综合或者pr的时候会被工具优化,因此需要对模块进行don’t touch设置;通过增值表得到,逻辑关系表达式为:Q=(Q1&Q2)|(Q1&Q3)|(Q2&Q3),通过工艺库的stdcell进行搭建,得到如下电路。 阅读详情

相关推荐

第十六届蓝桥杯大赛软件赛(编程类)知识点大纲

难度:[1-3]难度:[1-5]难度:[1-3]难度:[2-5]难度:[1-5]

Future_yzx的博客 4121

【LeetCode】309. Best Time to Buy and Sell Stock with Cooldown 解题报告(Python & C++)

【LeetCode】309. Best Time to Buy and Sell Stock with Cooldown 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode...

负雪明烛 3559

保姆级教程:用SOEM主站搞定汇川SV660N伺服驱动器的EtherCAT通讯(附完整C代码)

本文提供了一份详细的SOEM主站与汇川SV660N伺服驱动器EtherCAT通讯的实战教程,涵盖环境搭建、状态机理解、PDO映射配置、CiA402状态机实战、实时性能优化及诊断调试等关键步骤,并附完整C代码示例,帮助开发者快速实现工业级EtherCAT控制。

weixin_28292761的博客 316

[leetcode] 309. Best Time to Buy and Sell Stock with Cooldown 解题报告

题目链接:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/   Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to fi...

小榕流光的专栏 4741

【scratch音乐课】天空之城:音符与节拍、消息与链表

scratch是一个非常小巧的青少年编程工具,自带程序编写语句,积木式语句非常直观、易用,可创作数学、科学、音乐、绘画等方面作品。本文用scratch“演奏”一首大家非常熟悉的《天空之城》,并向大家介绍一些基本乐理知识,以及scratch中消息、链表的使用。

★·°立芯;︶︵︶ 1万+

扣子Coze智能体万字教程:从入门到精通,一文掌握AI工作流搭建

智能体 = 角色设定 + 提示词 + 变量输入一个好提示词的核心是结构、风格和复用性创建后可以一键发布成网页应用、对话助手整个过程0代码,只靠配置,人人可学,人人可用四、进阶一:理解「工作流」的威力如果你已经用扣子创建了几个简单的智能体,比如写个爆款小红书文案、生成一段短视频脚本,那恭喜你已经入门。但如果你还停留在“一个智能体 = 一段提示词”的层级,你离真正的智能体生产力革命,还差一步:学会搭建“工作流”——让多个大模型自动协同完成任务。

互联网架构师笔记 3560

[leetcode] 309. Best Time to Buy and Sell Stock with Cooldown

Description Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete as many transactions as you like (i...

Learning from the mistakes 517

UG NX二次开发(C++)-库缺少需要的入口点的原因与解决方案

在UG NX二次开发过程中,有时会遇到形形色色的bug,比如有个读者其采用UG NX二次开发(C++)时,遇到“库缺少需要的入口点”这个问题,看了一下他发给我的源代码,感觉有必要讲一下UG NX二次开发(C++)遇到此类问题的可能情况与解决方法。自然,这是博主的经验之谈,有不足之处还请在评论区讨论。

GimiGimmy 2218

[刷题]leetcode #309 - Best Time to Buy and Sell Stock with Cooldown

题目 代码 class Solution { public int maxProfit(int[] prices) { if (prices.length <= 1) return 0; int l = prices.length; int a=0, b = -prices[0], c = 0; for (int i = 1; i < l; ++i){ int temp = a;

qq_27637285的博客 312

【测开求职】面试题:数据结构 吐血整理

本篇文章是笔者为准备研究生复试以及校招而写,因此主要针对问答,注重理解,而不在于细节计算。涵盖考研范围内的所有知识内容,读者可以先查看上面的链接走一遍数据结构的整体框架结构。其中,重点难点以及易混淆的知识点,笔者用其他近30篇文章进行了详述,在知识点处贴好了链接,有需要的可以查阅。

vector的博客 7081

YOLO26模型应用过程中的报错处理及疑问解答,涉及环境搭建、模型训练、模块改进、论文写作

本文记录了YOLO26模型的这一系列过程中可能发生的问题,以及大家非常关心的问题,这里统一做了一个记录和解答。

Limiiiing的博客 247

【LeetCode】解题309:Best Time to Buy and Sell Stock with Cooldown(动态规划)

LeetCode解题 309:Best Time to Buy and Sell Stock with Cooldown (动态规划)Problem 309: Best Time to Buy and Sell Stock with Cooldown [Medium]解题思路Solution (Java)修改过程 Problem 309: Best Time to Buy and Sell Sto...

Fayedily的博客 324

大数据知识面试题-Hadoop(2022版)

序列号 内容 链接 1 大数据知识面试题-通用(2022版) 待续… 2 大数据知识面试题-Hadoop(2022版) 待续… 3 大数据知识面试题-MapReduce和yarn(2022版) 待续… 4 大数据知识面试题-Zookeepr (2022版) 待续… 5 大数据知识面试题-Hive (2022版) 待续… 6 大数据知识面试题-Flume(2022版) 待续… 7 大数据知识面试题-Hbase(2022版) 待续… 8 大数据知识面试题-sqoop(....

拉格朗日的学习笔记 1万+

leetcode 309. Best Time to Buy and Sell Stock with Cooldown

这道题与的区别就是卖出股票后的一天不能买股票。仍然用动态规划解决。这类题目的关键是要分析每一天有几种状态,用dp数组变量记录这些状态。

weixin_41554427的博客 1166

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

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

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

【LeetCode】309. Best Time to Buy and Sell Stock with Cooldown

309. Best Time to Buy and Sell Stock with Cooldown Description: Say you have an array for which the  element is the price of a given stock on day i. Design an algorithm to find the maximum profit. ...

Microstrong 1216

深度学习工具包 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】【medium】309. Best Time to Buy and Sell Stock with Cooldown​​​​​​​

309. Best Time to Buy and Sell Stock with Cooldown Say you have an array for which theithelement is the price of a given stock on dayi. Design an algorithm to find the maximum profit. You may com...

lemonade13的博客 147

贝叶斯统计 [韦来生编]

贝叶斯(Bayes)统计是近几十年来迅速发展起来的数理统计的一个重要分支。贝叶斯方法与经典统计方法的主要不同之处是进行统计推断时除了利用样本信息外,还要利用参数的先验信息,因此可以提高统计推断或统计决策的效果。它在经济、金融、生物、医学、自然科学和社会科学等许多领域具有广泛的应用。贝叶斯方法的研究已渗透到了统计学的几乎所有领域。作者在给中国科学技术大学概率论与数理统计专业本科生开设的“数理统计”课程中有一章专门讲授“贝叶斯方法和统计决策理论”,近十年来也给中国科学技术大学概率统计专业研究生开设过几次“贝叶斯分析”课。本书是在对过去讲稿的内容作了适当的增减和调整的基础上完成的。  本书共分六章。第1章是绪论,介绍了贝叶斯统计的若干基本概念,同时对必要的数理统计的基础知识有重点地作了回顾。第二章介绍了确定先验分布的若干可供选择的方法。第三章介绍了几类常见统计模型参数的后验分布的主要结果和计算方法。第四章和第五章分别介绍了“贝叶斯统计推断”和“贝叶斯统计决策”的内容。第六章介绍了贝叶斯统计计算的若干方法,包括蒙特卡洛方法和马尔可夫链蒙特卡洛(MCMC)方法以及相关统计软件的简介。本书的每一章附右大量的例题和习题,书末附有常用的几个表格和部分习题解答供读者查用。  大约可在54学时内讲授本书的第1章至第六章的主要内容,教师可根据学时需要选讲本书第1章至第六章的部分内容。书中标“*”号的章节可略去不讲,留给读者作为阅读材料。第六章例题中的R代码和部分例题的数据文件等可从作者个人主页下载。  作者在编写本书的过程中参考了J.O.Berger教授的《统计决策论及贝叶斯分析》和茆诗松教授编写的《贝叶斯统计》等书中的一些内容,以及《贝叶斯分析》一书中由张伟平博士编写的有关贝叶斯统计计算的一些内容。在此表示衷心感谢。  本书编写准备过程中,中国科学技术大学统计与金融系的几个研究生帮助完成了本书部分中文的录入和排版,作者对他们的辛勤工作表示真诚的感谢。高等教育出版社为本书的出版给予了大力支持。在此一并致谢。

309. Best Time to Buy and Sell Stock with Cooldown

https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/ 1. 思路 https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/discuss/75928/Share-my-DP-solution-(B...

scut_salmon的博客 250

Day 51 | 309. Best Time to Buy and Sell Stock with Cooldown | 714. with Transaction Fee

Day 51 | 309. Best Time to Buy and Sell Stock with Cooldown | 714. with Transaction Fee

KKKKKKyle的博客 145

算法: 冷却时间买卖股票的最佳时机309. Best Time to Buy and Sell Stock with Cooldown

309. Best Time to Buy and Sell Stock with Cooldown You are given an array prices where prices[i] is the price of a given stock on the ith day. Find the maximum profit you can achieve. You may complete as many transactions as you like (i.e., buy one and sel

AI架构师易筋 573

【leetcode笔记】Python实现 309. Best Time to Buy and Sell Stock with Cooldown

题目描述 Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy ...

Jack_Kuo的博客 587

LeetCode 309. Best Time to Buy and Sell Stock with Cooldown

cool down time: at least one day //we use dp, and the following explaination is crystal clear // https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/discuss/75931/Easiest-JAVA-solution-with-explanations class Solution { public i

Tech in Pieces 210

LeetCode: 309. Best Time to Buy and Sell Stock with Cooldown

LeetCode: 309. Best Time to Buy and Sell Stock with Cooldown 题目描述 Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit...

杨领well的专栏 193

LeetCode-309. Best Time to Buy and Sell Stock with Cooldown [C++][Java]

Find the maximum profit you can achieve. You may complete as many transactions as you like (i.e., buy one and sell one share of the stock multiple times) with the following restrictions: After you sell your stock, you cannot buy stock on the next day

贫道绝缘子的博客 1149

leetcode -- Best Time to Buy and Sell Stock with Cooldown -- 不理解

https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/

xyqzki的专栏 4302

Leetcode 309. Best Time to Buy and Sell Stock with Cooldown

这道题用的是dp的思想,用buy, sell, cooldown记录当前这些状态下最多的profit。 对于day i 而言: sell[i] = buy[i-1] + prices[i] buy[i] = max(muy[i-1], cooldown[i-1] - prices[i]) cooldown[i] = max(cooldown[i-1]...

I_ren的博客 375

leetcode 309 Best Time to Buy and Sell Stock with Cooldown 详细解答

解法1 参照leetcode 122, 解法几乎一样 但这里需要三个数组, sell: 每次卖出股票后自身的收益 buy: 每次买入股票后自身的收益 cooldown: 冷冻股票后自身的收益 根据题意很明显:cooldown[i] = sell[i-1] 因为题目说在卖出之后需要一个冷冻期 sell[i] = max( sell[i-1], buy[i-1] + prices[i] ) buy[i] = max( buy[i-1], cooldown[i-1] - prices[i] ) 代码如下: .

qq_22210659的博客 234
上一篇: leetcode 304. Range Sum Query 2D - Immutable
下一篇: leetcode 312. Burst Balloons
蓝羽飞鸟
博客等级 码龄8年 1万+粉丝 727原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

打赏作者

蓝羽飞鸟

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

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

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

打赏作者

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

抵扣说明:

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

余额充值