原题

思路:

状态转移

出售股票的状态,最大利润有两种可能。

一,和昨天一样不动;二,昨天持有的股票今天卖掉。

sell[i] = max(sell[i-1],buy[i-1] + prices[i]);

购买股票的状态,最大利润有两种可能。

一,和昨天一样不动;二,两天前出售,今天购买。

bdp[i] = Math.max(bdp[i-1],sell[i-2] - prices[i]);

 class Solution
{
public:
int maxProfit(vector<int> &prices)
{
if (prices.size() == 0)
return 0;
int len = prices.size();
vector<int> buy(len + 1, 0), sell(len + 1, 0);
buy[1] = -prices[0]; for (int i = 2; i <= len; i++)
{
buy[i] = max(buy[i - 1], sell[i - 2] - prices[i - 1]);
sell[i] = max(sell[i - 1], buy[i - 1] + prices[i - 1]);
}
return sell[len];
}
};

[leetcode] 309. Best Time to Buy and Sell Stock with Cooldown(medium)的更多相关文章

  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 (stock problem)

    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

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

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

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

  5. 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 题目的要求是只买卖一次,买的价格越低,卖的价格越高,肯定收益就越大 遍历整个数组,维护一个当前位置之前最低的买入价格,然后每次 ...

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

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...

  7. 121. 122. 123. 188. Best Time to Buy and Sell Stock *HARD* 309. Best Time to Buy and Sell Stock with Cooldown -- 买卖股票

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

  8. 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 a ...

  9. 【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 a ...

随机推荐

  1. linux下mysql定时备份

    1. 在服务器上建立备份文件的存放文件夹 sudo mkdir /usr/local/dbbackup 2. 编写备份脚本 vi dbbackup.sh 在里面编写如下内容 mysqldump -ur ...

  2. DIOCP3 DEMO的编译(去掉VCL前缀)

    总有些朋友问我,关于DEMO编译的一些问题,每次都回答大概都差不多,我想还是写篇说明书给大家,关于DEMO编译的步骤. [环境设定] 1.将DIOCP3\source路径添加到Delphi的搜索路径, ...

  3. QT 文件拖放事件dropEvent和dragEnterEvent

    重载以下两个函数,可以实现将文本文件拖放进文本编辑器 void MainWindow::dragEnterEvent(QDragEnterEvent *event)//拖进事件 { if(event- ...

  4. Android WebView设置背景透明

    Adndroid 2.x的设置 在Android 2.x下,设置webview背景为透明的方法: wvContent.setBackgroundColor(0); Adndroid 4.0 由于硬件加 ...

  5. 网络学习笔记(三):HTTP缓存

      HTTP缓存是一种保存资源副本并在下次请求时直接使用该副本的技术,合理的使用缓存可以有效的提升web性能.   浏览器将js文件.css文件.图片等资源缓存,当下次请求这些资源时,可以不发送网络请 ...

  6. 使用docker运行GitLab

    从docker镜像拉取代码,docker pull gitlab/gitlab-ce:latest. 创建/srv/gitlab目录sudo mkdir /srv/gitlab 启动GitLab CE ...

  7. K8s集群部署(一)------ETCD集群部署

    环境说明 三台主机: k8s-master  10.0.3.225 k8s-node1    10.0.3.226 k8s-node2    10.0.3.227 配置主机名解析 [root@k8s- ...

  8. [apue] 等待子进程的那些事儿

    谈到等待子进程,首先想到的就是SIGCHLD信号与wait函数族,本文试图厘清二者的方方面面,以及组合使用时可能不小心掉进去的坑. 1. 首先谈单独使用SIGCHLD的场景.下面是一段典型的代码片段: ...

  9. 如何使用 Docker 安装 Jenkins

    说在前面 本篇内容非常简单,仅讲述了如何快速在 Docker 上部署一个 Jenkins 实例,不涉及其他. 本文实验环境: 操作系统:Centos 7.5 Docker Version:18.09. ...

  10. volatile的内存语义与应用

    volatile的内存语义 volatile的特性 理解volatile特性的一个好方法是把对volatile变量的单个读/写,堪称是使用同一个锁对这些单个读/写操作做了同步. 锁的happens-b ...