(算法分析Week15)Best Time to Buy and Sell Stock with Cooldown[Medium]
来源:互联网 发布:java wait 当前线程 编辑:程序博客网 时间:2024/06/07 02:58
309. Best Time to Buy and Sell Stock with Cooldown[Medium]
题目来源
Description
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 = 3transactions = [buy, sell, cooldown, buy, sell]
Solution
这道题是Best Time to Buy and Sell Stock with Colldown的升级版。给出一个数字,第i个元素表示某只股票在第i天的价格。限制条件是,卖出后第二天不可以马上买入。可以多次交易,但是在买入股票之前必须卖出。
看了看好像应该先做Best Time to Buy and Sell Stock II,但是不要紧,DP问题就是找递推式子。
第i天的交易累积收益只和第i-1天、第i-2天有关。
sell[i]表示在第i天选择卖出的最大累积收益
buy[i]表示在第i天选择买入的最大累积收益
sell[i] = max(buy[i - 1] + prices[i], sell[i - 1] + (prices[i) - prices[i-1]))buy[i] = max(sell[i - 2] - prices[i], buy[i - 1] - (prices[i) - prices[i-1]))
意思就是,
第i天选择卖出的最大收益 = max(第i-1天买入第i天卖出的最大累积收益,第i-1天卖出改为第i天卖出的最大累积收益【sell[i-1]-prices[i-1]+prices[i]】)
第i天选择买入的最大收益 = max(第i-2天卖出第i天买入的最大累积收益, 第i-1天买入改为第i天买入的最大累积收益【buy[i-1]+prices[i-1]-prices[i]】)
返回sell数组中的最大值即为答案。
需要注意的是初始化数组,由于循环从i = 2开始才不会越界所以要处理下标为0和1的时候的数据
buy[0] = -price[0];buy[1] = -price[1];sell[1] = (price[1] - price[0]) > 0 ? (price[1] -price[0]) : 0;
Complexity analysis
O(n)
Code
class Solution {public: int maxProfit(vector<int>& price) { int maxPro = 0; int curPro = 0; int n = price.size(); if (n <= 1) { return 0; } int buy[n] = {0}; int sell[n] = {0}; buy[0] = -price[0]; buy[1] = -price[1]; sell[1] = (price[1] - price[0]) > 0 ? (price[1] - price[0]) : 0; maxPro = sell[1]; if (n == 2) return sell[1]; for(int i = 2; i < n; i++) { sell[i] = max(buy[i-1] + price[i], sell[i-1] + (price[i]-price[i-1])); buy[i] = max(sell[i-2] - price[i], buy[i-1] - (price[i]-price[i-1])); maxPro = max(maxPro, sell[i]); } return maxPro; }};
Result
- (算法分析Week15)Best Time to Buy and Sell Stock with Cooldown[Medium]
- [Medium]Best Time to Buy and Sell Stock with Cooldown
- 309.leetcode Best Time to Buy and Sell Stock with Cooldown(medium)[动态规划]
- Leetcode 309. Best Time to Buy and Sell Stock with Cooldown (Medium) (cpp)
- LeetCode解题报告 309. Best Time to Buy and Sell Stock with Cooldown[medium]
- 309. Best Time to Buy and Sell Stock with Cooldown 类别:动态规划 难度:medium
- 309. Best Time to Buy and Sell Stock with Cooldown -Medium
- 309Best Time to Buy and Sell Stock with Cooldown
- leetcode Best Time to Buy and Sell Stock with Cooldown
- Best Time to Buy and Sell Stock with Cooldown
- Leetcode197: Best Time to Buy and Sell Stock with Cooldown
- LeetCode Best Time to Buy and Sell Stock with Cooldown
- Leetcode: Best Time to Buy and Sell Stock with Cooldown
- Best Time to Buy and Sell Stock with Cooldown
- [LeetCode]Best Time to Buy and Sell Stock with Cooldown
- Leetcode Best Time to Buy and Sell Stock with Cooldown
- leetcode - Best Time to Buy and Sell Stock with Cooldown
- Best Time to Buy and Sell Stock with Cooldown
- java编程思想中的设计模式
- 如何通过API调用来分析恶意软件
- 实时适配iOS真的很难吗?
- Python3爬取网络数据
- 如何使用Xftp连接Centos服务器?
- (算法分析Week15)Best Time to Buy and Sell Stock with Cooldown[Medium]
- 用两个栈实现队列
- java 内部类使用姿势
- 《java8函数式编程》中后续代码要用到的模型
- 多线程基础3-----<future>
- 举例 用continue语句提前结束循环
- CoreText 入门
- Android实现EditText不可粘贴
- 斐波那契数列