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. pyinstaller模块

    应用场景: 1 把一些python脚本推广到更多机器上运行,但无法保证他们都有安装python解释器,所以,打包可以免去这一障碍 win: 安装: 在cmd中运行pip install pyinsta ...

  2. Codeforces Round #402 (Div. 2) C

    Description Igor found out discounts in a shop and decided to buy n items. Discounts at the store wi ...

  3. Codeforces Round #324 (Div. 2)

    CF的rating设置改了..人太多了,决定开小号打,果然是明智的选择! 水 A - Olesya and Rodion #include <bits/stdc++.h> using na ...

  4. 18.3.2从Class上获取信息(内部类接口等)

    内部类 接口.枚举.注释类型

  5. html2canvas如何将div转成图片并下载,如何将滚动条的内容截取下来

    <!DOCTYPE html> <html lang="en"> <head> <meta name="layout" ...

  6. 498 Diagonal Traverse 对角线遍历

    详见:https://leetcode.com/problems/diagonal-traverse/description/ C++: class Solution { public: vector ...

  7. PHP多图片上传类推荐

    多文件上传是PHP中的一个基础应用,反正PHPer都会遇到的问题,现在就介绍一个功能完善.强大的多文件上传类给大家吧,能用上这个类的地方会很多. <?php class Upload{ var ...

  8. Git团队协作 - 新feature的开发过程

    新feature的开发过程 建议使用SmartGit,以下是命令行操作 git checkout -b dev (对于没有分支的人)新建dev分支 git pull origin dev拉取最新数据 ...

  9. [转]合理使用ArrayMap代替HashMap

    合理使用ArrayMap代替HashMap 2016年07月08日 15:34:44 阅读数:5938 转载请标注: 披萨大叔的博客 http://blog.csdn.net/qq_27258799/ ...

  10. BottomNavigationBar底部导航条用法

    先来张效果图 接下来是实现过程 1.加入依赖 compile 'com.ashokvarma.android:bottom-navigation-bar:1.3.0' 2.xml布局 fragment ...