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. 148 Sort List 链表上的归并排序和快速排序

    在使用O(n log n) 时间复杂度和常数级空间复杂度下,对链表进行排序. 详见:https://leetcode.com/problems/sort-list/description/ Java实 ...

  2. STM32HAL库学习之前言

    HAL库:HAL 的全称是: Hardware Abstraction Layer (硬件抽象层) ,是ST最新推荐的库.包括基本库和扩展库(功能外展):三种编程模型(轮询.中断和 DMA) 灵活的回 ...

  3. .net core跨域设置

    services.AddCors(options => options.AddPolicy("AllowSameDomain", builder => builder. ...

  4. idea安装mybatis插件

    简介 mybatis_plus主要的作用是自动导航,如下图 点击箭头会跳转到对应的dao接口中,同样,dao接口中也有这样的箭头,点击之后会跳转到对应的sql映射语句处. 还有一个功能就是检查mapp ...

  5. oracle 数据导入、导出

    导入导出 --数据导出备份和导入 ------注意 导出和导入 必须是CMD 命令行下操作,而不是SQL编辑器中 --1.导出表 . --exp:导出关键字 ,userid:用户权限 ,file:保存 ...

  6. AJPFX总结java开发常用类(包装,数字处理集合等)(一)

    一:首谈java中的包装类 Java为基本类型提供包装类,这使得任何接受对象的操作也可以用来操作基本类型,直接将简单类型的变量表示为一个类,在执行变量类型的相互转换时,我们会大量使用这些包装类.jav ...

  7. Ubuntu16下查看CPU、内存和磁盘相关信息

    1.内存 查看内存#free -m total used free shared buff/cache available Mem: Swap: 2.CPU 查看逻辑cpu个数: #cat /proc ...

  8. webuploader上传工具

    http://fex.baidu.com/webuploader/getting-started.html#显示用户选择 Html部分 首先准备dom结构,包含存放文件信息的容器.选择按钮和上传按钮三 ...

  9. JDO

    JDO 编辑 本词条缺少名片图,补充相关内容使词条更完整,还能快速升级,赶紧来编辑吧! JDO(Java Data Object )是Java对象持久化的新的规范,也是一个用于存取某种数据仓库中的对象 ...

  10. C++_pthread read-write lock_读写锁_visual studio 2015下配置

    pthread下载地址:https://sourceware.org/pthreads-win32/ 1. 项目->属性->VC++目录 包含目录里添加:pthread所在路径\pthre ...