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:

prices = [1, 2, 3, 0, 2]

maxProfit = 3

transactions = [buy, sell, cooldown, buy, sell]

stock problem交易次数无限模型加上冷却条件,

T[i][0]=max(T[i-1][0],T[i-1][1]+prices[i]);

T[i][1]=max(T[i-1][1],T[i-2][0]-prices[i]);

const int inf=-999999;
class Solution {
public:
int maxProfit(vector<int>& prices) {
int sell=0,buy=inf,pre=0,last;
for(int i=0;i<prices.size();i++){
last=sell;
sell=max(sell,buy+prices[i]);
buy=max(buy,pre-prices[i]);
pre=last;
}
return sell;
}
};

LeetCode 309. Best Time to Buy and Sell Stock with Cooldown (stock problem)的更多相关文章

  1. [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 al ...

  2. 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 al ...

  3. [leetcode] 309. Best Time to Buy and Sell Stock with Cooldown(medium)

    原题 思路: 状态转移 出售股票的状态,最大利润有两种可能. 一,和昨天一样不动:二,昨天持有的股票今天卖掉. sell[i] = max(sell[i-1],buy[i-1] + prices[i] ...

  4. LeetCode 309 Best Time to Buy and Sell Stock with Cooldown 解决方案

    题目描述 给定一个整数数组,其中第 i 个元素代表了第 i 天的股票价格 .​ 设计一个算法计算出最大利润.在满足以下约束条件下,你可以尽可能地完成更多的交易(多次买卖一支股票): 你不能同时参与多笔 ...

  5. [LeetCode] 121. Best Time to Buy and Sell Stock 买卖股票的最佳时间

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

  6. [LeetCode] 122. Best Time to Buy and Sell Stock II 买卖股票的最佳时间 II

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

  7. [LeetCode] 123. Best Time to Buy and Sell Stock III 买卖股票的最佳时间 III

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

  8. [LeetCode] 188. Best Time to Buy and Sell Stock IV 买卖股票的最佳时间 IV

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

  9. leetcode 121. Best Time to Buy and Sell Stock 、122.Best Time to Buy and Sell Stock II 、309. Best Time to Buy and Sell Stock with Cooldown

    121. Best Time to Buy and Sell Stock 题目的要求是只买卖一次,买的价格越低,卖的价格越高,肯定收益就越大 遍历整个数组,维护一个当前位置之前最低的买入价格,然后每次 ...

随机推荐

  1. websocket实现单聊

    server# @File: ws from flask import Flask, request, render_template from geventwebsocket.handler imp ...

  2. codeforces 615 D. Multipliers (数论 + 小费马定理 + 素数)

    题目链接: codeforces 615 D. Multipliers 题目描述: 给出n个素数,这n个素数的乘积等于s,问p的所有因子相乘等于多少? 解题思路: 需要求出每一个素数的贡献值,设定在这 ...

  3. Ionic开发-如何在ion-content形成上下结构 上面固定下层可滚动

    在一个系统设计中,一般有些需要固定位置,便利操作.现在我要做一个上下两层,需要固定上方,下方拉刷新数据. 页面: <ion-content scroll="false"> ...

  4. 猩球StarBall ,一个方便约球的小程序

    扫描小程序码直接进入小程序 猩球StarBall 是一款为热爱运动的人群提供便利的小程序. 开发技术为Java +Mysql 其中用到的技术框架为SpringBoot,Mybatis,Redis,Qu ...

  5. logging日志过滤和日志文件自动截取

    1.日志过滤 import logging class IgnoreFilter(logging.Filter): def filter(self,record): return "girl ...

  6. sys模块详解

    1.sys.argv argv是「argument variable」参数变量的简写形式,一般在命令行调用的时候由系统传递给程序.这个变量其实是一个List,argv[0] 一般是“被调用的脚本文件名 ...

  7. CF967D Resource Distribution

    思路: 在一堆服务器中,资源最少的那一个是“瓶颈”,由此想到贪心思路. 首先对所有服务器按照资源数量c排序,再从头到尾扫描.对每个位置,根据x1和x2计算出两段连续的服务器集合分别分配给A任务和B任务 ...

  8. SPICE-HTML5 鼠标指针BUG修复

    研究SPICE,找到了他们官方指定的HTML5客户端.下载下来用一下,发现跟网页VNC的水平差不多了.http://www.spice-space.org/page/Html5 服务端直接用QEMU起 ...

  9. WEB前端研发工程师编程能力成长之路(2)(转)

    WEB前端研发工程师编程能力成长之路(2)   四.[入微] 最强解决方案.你能够走在需求的前面,将当前需求里有的.没有直接提出来的.现在暂时没有但将来可能有的等等,及前端编程潜规则等各个方方面面都综 ...

  10. XtraBackUp 热备份工具

    是一款强大的在线热备份工具 备份的过程中,不锁表 使用percona-xtrabackup-24-2.4.7-1.el7.x86_64.rpm yum源安装: 1.安装Percona的库:       ...