LeetCode #714 Best Time to Buy and Sell Stock with Transaction Fee
来源:互联网 发布:凌哥刷枪软件 编辑:程序博客网 时间:2024/05/21 10:10
题目
Your are given an array of integers prices
, for which the i
-th element is the price of a given stock on day i
; and a non-negative integer fee
representing a transaction fee.
You may complete as many transactions as you like, but you need to pay the transaction fee for each transaction. You may not buy more than 1 share of a stock at a time (ie. you must sell the stock share before you buy again.)
Return the maximum profit you can make.
Example 1:
Input: prices = [1, 3, 2, 8, 4, 9], fee = 2Output: 8Explanation: The maximum profit can be achieved by:Buying at prices[0] = 1Selling at prices[3] = 8Buying at prices[4] = 4Selling at prices[5] = 9The total profit is ((8 - 1) - 2) + ((9 - 4) - 2) = 8.
Note:
0 < prices.length <= 50000.
0 < prices[i] < 50000.
0 <= fee < 50000.
解题思路
采用动态规划的方法,一开始比较容易想到的思路是设置一个收益数组 pro
,令 pro[i]
表示第 i
天的最大收益,然后考虑 pro[i]
与 pro[i - 1, i - 2 ... 0]
的关系。在第 i
天,我们只有三个操作:
- 不买也不卖。此时
pro[i] = pro[i - 1]
。 - 买。此时
pro[i] = pro[i - 1] - prices[i]
。 - 卖。此时
pro[i] = pro[i - 1] + prices[i] - fee
。
这样,似乎可以天真地认为一个动态转移方程就出来了:
然而这个方程根本不可行,因为
上面的思路行不通,根本原因是“买股票”和“卖股票”这两个操作是无法单独地衡量其收益的,这一次的“卖”操作必须减去上一次的“买”操作才能衡量收益,同理,这一次的“买”操作也必须结合上一次的“卖”操作来衡量收益。因此,我们需要维护两种第 i
天的最大收益:一种是第 i
天执行“买”操作的最大收益 buy_pro[i]
,另一种是第 i
天执行“卖”操作的最大收益 sell_pro[i]
,这样,动态转移方程就变成了:
这里要特别注意
buy_pro[i]
表示的是第 i
天执行“买”操作的最大收益,而不是花费,因此有 sell_pro
和 buy_pro
的最后一个元素哪个较大,选择最大的收益返回即可。上述的动态转移方程需要 i
天的操作的收益只与第 i - 1
天的操作收益有关,因此根本不需要维护这样的两个数组,只要使用两个变量 buy_pro
和 sell_pro
即可。
C++代码实现
class Solution {public: int maxProfit(vector<int>& prices, int fee) { // 初始化,第一天只能买,不能卖 int buy_profit = -prices[0], sell_profit = 0; for (int i = 1; i < prices.size(); ++i) { // 如果 buy_profit 被改变, // 即 buy_profit = sell_profit - prices[i], // 则 sell_profit 必然不会改变,反之亦然。 // buy_profit 和 sell_profit 之中只有一个能改变, // 保证了两者之中必有一个是前一天的最大收益值, // 这是不用维护两个数组,降低空间复杂度的关键! buy_profit = max(buy_profit, sell_profit - prices[i]); sell_profit = max(sell_profit, prices[i] + buy_profit - fee); } return max(buy_profit, sell_profit); }};
- Leetcode算法学习日志-714 Best Time to Buy and Sell Stock with Transaction Fee
- leetcode 714 Best Time to Buy and Sell Stock with Transaction Fee
- LeetCode #714 Best Time to Buy and Sell Stock with Transaction Fee
- Best Time to Buy and Sell Stock with Transaction Fee[LeetCode 714]
- LeetCode 714 Best Time to Buy and Sell Stock with Transaction Fee
- Best Time to Buy and Sell Stock with Transaction Fee
- Best Time to Buy and Sell Stock with Transaction Fee
- Best Time to Buy and Sell Stock with Transaction Fee
- LeetCode算法问题10 —— Best Time to Buy and Sell Stock with Transaction Fee
- [LeetCode] Algorithms-714. Best Time to Buy and Sell Stock with Transaction Fee
- [LeetCode] DP之 Best time to buy and sell stock with transaction fee
- leetcode练习 714. Best Time to Buy and Sell Stock with Transaction Fee
- [leetcode]714. Best Time to Buy and Sell Stock with Transaction Fee
- [LeetCode]714. Best Time to Buy and Sell Stock with Transaction Fee
- leetCode-Best Time to Buy and Sell Stock with Transaction Fee
- LeetCode:Best Time to Buy and Sell Stock with Transaction Fee
- LeetCode 714. Best Time to Buy and Sell Stock with Transaction Fee
- LeetCode 题解: 714. Best Time to Buy and Sell Stock with Transaction Fee
- 苹果自动驾驶遭遇挫折,转而开发园区穿梭大巴
- 阿里云开放华北5,继续加注云市场投入
- “化反”原创者阿木卸任乐视一切职务 贾跃亭战队即将解散
- Servlet进阶3
- poj2528 Mayor's posters (离散化 + 并查集)
- LeetCode #714 Best Time to Buy and Sell Stock with Transaction Fee
- 腾讯云+未来北京站 打造信息高速路与政企数字化转型
- Gartner发2017技术成熟度曲线,和愈发明显的技术“小真空期”
- 更迭 5 代,进入19岁,微软要让小冰赚钱养家了
- K12领域最高融资额落定 VIPKID推出少儿中文平台
- 英语在线教育机构争战AI风口,为何雷声大雨点小?
- HDU 1593 find a way to escape(角速度)
- 阿里加码农村市场 全集团资源运作村淘业务
- linux debian 安装git