LeetCode Best Time to Buy and Sell Stock with Transaction Fee
原题链接在这里:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/
题目:
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 = 2
Output: 8
Explanation: The maximum profit can be achieved by:
- Buying at prices[0] = 1
- Selling at prices[3] = 8
- Buying at prices[4] = 4
- Selling at prices[5] = 9
The total profit is ((8 - 1) - 2) + ((9 - 4) - 2) = 8.
Note:
0 < prices.length <= 50000.0 < prices[i] < 50000.0 <= fee < 50000.
题解:
Let buy[i] denotes maximum profit till index i, ending with buy.
Let sell[i] denotes maximum profit till index i, ending with sell.
buy[i] = max(buy[i-1], sell[i-1]-prices[i]).
sell[i] = max(sell[i-1], buy[i-1]+prices[i]-fee).
Use variable instead of array to save space.
Note: Do NOT forget to update variable after each iteration.
Time Complexity: O(n). n = prices.length.
Space: O(1).
class Solution {
public int maxProfit(int[] prices, int fee) {
if(prices == null || prices.length < 2){
return 0;
}
int b0 = -prices[0];
int b1 = b0;
int s0 = 0;
int s1 = 0;
for(int i = 1; i<prices.length; i++){
b0 = Math.max(b1, s1-prices[i]);
s0 = Math.max(s1, b1+prices[i]-fee);
b1 = b0;
s1 = s0;
}
return s0;
}
}
T[i][k][0] 代表maximum profit that could be gained at the end of the i-th day with at most k transactions. 最后手上剩下0股stock.
递推公式就是
T[i][k][0] = max(T[i-1][k][0], T[i-1][k][1] + prices[i])
T[i][k][1] = max(T[i-1][k][1], T[i-1][k-1][0] - prices[i])
如果需要加上transaction fee就变成
T[i][k][0] = max(T[i-1][k][0], T[i-1][k][1] + prices[i])
T[i][k][1] = max(T[i-1][k][1], T[i-1][k][0] - prices[i] - fee)
买入时交fee.
最后肯定是卖掉手上的股票收益更多,所以返回T[i][k][0].
Time Complexity: O(n). n = prices.length.
Space: O(1).
AC Java:
class Solution {
public int maxProfit(int[] prices, int fee) {
int tIk0 = 0;
int tIk1 = Integer.MIN_VALUE;
for(int price : prices){
int preTransactionIk0 = tIk0;
tIk0 = Math.max(tIk0, tIk1+price);
tIk1 = Math.max(tIk1, tIk0-price-fee);
}
return tIk0;
}
}
买卖股票类题目都可以套用这个思路.
Reference: https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/discuss/108870/
LeetCode Best Time to Buy and Sell Stock with Transaction Fee的更多相关文章
- [LeetCode] Best Time to Buy and Sell Stock with Transaction Fee 买股票的最佳时间含交易费
Your are given an array of integers prices, for which the i-th element is the price of a given stock ...
- Leetcode之动态规划(DP)专题-714. 买卖股票的最佳时机含手续费(Best Time to Buy and Sell Stock with Transaction Fee)
Leetcode之动态规划(DP)专题-714. 买卖股票的最佳时机含手续费(Best Time to Buy and Sell Stock with Transaction Fee) 股票问题: 1 ...
- Week 7 - 714. Best Time to Buy and Sell Stock with Transaction Fee & 718. Maximum Length of Repeated Subarray
714. Best Time to Buy and Sell Stock with Transaction Fee - Medium Your are given an array of intege ...
- 714. Best Time to Buy and Sell Stock with Transaction Fee
问题 给定一个数组,第i个元素表示第i天股票的价格,可执行多次"买一次卖一次",每次执行完(卖出后)需要小费,求最大利润 Input: prices = [1, 3, 2, 8, ...
- [LeetCode] 714. Best Time to Buy and Sell Stock with Transaction Fee 买卖股票的最佳时间有交易费
Your are given an array of integers prices, for which the i-th element is the price of a given stock ...
- 【LeetCode】714. Best Time to Buy and Sell Stock with Transaction Fee 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...
- 【leetcode】714. Best Time to Buy and Sell Stock with Transaction Fee
题目如下: Your are given an array of integers prices, for which the i-th element is the price of a given ...
- [Swift]LeetCode714. 买卖股票的最佳时机含手续费 | Best Time to Buy and Sell Stock with Transaction Fee
Your are given an array of integers prices, for which the i-th element is the price of a given stock ...
- 714. Best Time to Buy and Sell Stock with Transaction Fee有交易费的买卖股票
[抄题]: Your are given an array of integers prices, for which the i-th element is the price of a given ...
随机推荐
- Les13 性能管理
目标 使用Oracle Enterprise Manager监视性能 使用自动内存管理(AMM) 使用内存指导调整内存缓冲区的大小 查看与性能相关的动态视图 排除无效和不可用对象产生的故障 性能监视 ...
- List和数组的相互转化
一.数组转化为list:Arrays.aslist(arr); public static void main(String[] args) { String[] arr={"apple&q ...
- Codeforces Round #400
最近好像总是有点不想打,专题也刷不动,还是坚持这做了一场,虽然打到一半就没打了...(反正通常都只能做出两题) 感觉自己切水题越来越熟练了,然而难题还是不会做.. A题,水,用vector存下来就行了 ...
- IOS加载PDF文件
今天的任务是:在iOS上加载显示pdf文件. 方法一:利用webview -(void)loadDocument:(NSString *)documentName inView:(UIWebView ...
- ie下的bug之button
场景描述: 现在页面设计是都喜欢自定义按钮样式,某日接收到页面发现在ie下有bug,上代码: <div> <button><span><a href=&quo ...
- Linux系统在启动过程中mbr主引导程序被破坏的解决方案
首先,mbr主引导程序被破坏是指系统在启动过程中,磁头找不到/boot分区(windows的启动分区在c盘). 1)下面我们模拟主引导分区被破坏的情况:(在启动分区划分446M的存储大小) 2)重启( ...
- Linux:join命令详解
join 处理两个文件之间的数据,并且将两个文件中有相同的数据的那一行加在一起 语法 join(选项)(file1 file2) 选项 -a<1或2>:除了显示原来的输出内容之外,还显示指 ...
- Laravel 文件夹结构简介
表 1.1:Laravel 文件夹结构简介 文件夹名称 简介 app 应用程序的业务逻辑代码存放文件夹 app/Console 存放自定义 Artisian 命令文件 app/Http/Control ...
- Gcov 详解 + 内核函数覆盖率测试方法详述及产生错误解决办法
http://blog.csdn.net/wangyezi19930928/article/details/42638345 http://www.uml.org.cn/Test/201208311. ...
- String,StringBuffer和StringBuilder比较
String:查看源码得知,String类的声明是:public final,所以可以很清楚的知道,fianl的话是改变不了的,所以,如果我们用String来操作字符串的时候,一旦我们字符串的值改变, ...