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. angular 2 angular quick start Could not find HammerJS

    Angular2 的material中 引用了 hammerjs,遇到Could not find HammerJS错误,正确的步骤如下: 需要在如下位置增加 对material 和 hammerjs ...

  2. string类常用方法3

  3. toLocaleSting()

    之前一直忽略了这一方法,直到前天的笔试题,两种方式实现如下功能... 1234567890→1,234,567,890 当时我的思路是这样的:1.字符串反转,插入逗号,再反转 2.求余数,将字符串一分 ...

  4. Spring Boot学到的内容

    Hello World:了解程序入口(创建启动类) Web程序:写Controller类(@RestController),写Controller方法(@GetMapping),maven依赖spri ...

  5. CAS分析

    CAS:Compare and Swap, 翻译成比较并交换.   CAS 指的是现代 CPU 广泛支持的一种对内存中的共享数据进行操作的一种特殊指令.这个指令会对内存中的共享数据做原子的读写操作.简 ...

  6. laravel学习笔记(三)

    模型传值 路由: Route::get('/posts/{post}','\App\Http\Controllers\PostController@show'); 方法: public functio ...

  7. 洛谷 P1507 NASA的食物计划

    题目背景 NASA(美国航空航天局)因为航天飞机的隔热瓦等其他安 全技术问题一直大伤脑筋,因此在各方压力下终止了航天 飞机的历史,但是此类事情会不会在以后发生,谁也无法 保证,在遇到这类航天问题时,解 ...

  8. Jenkins邮件扩展插件Email Extension Plugin配置使用

    1.在管理插件中搜索并安装邮件扩展插件Email Extension Plugin: 2.在任务中增加构建后操作步骤,选择Editable Email Notification; 3.在高级中Add ...

  9. GPU、CPU的异同

    一.概念 CPU(Center Processing Unit)即中央处理器,GPU(Graphics Processing Unit)即图形处理器. 二.CPU和GPU的相同之处 两者都有总线和外界 ...

  10. Mybatis-Generator逆向生成Po,Mapper,XMLMAPPER(一)

    这个地方的生成需要用到三个文件,generatorConfig.xml.mybatis-generator-core-1.3.1.jar和mysql-connector-java-5.1.29.jar ...