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 ...
随机推荐
- offset的坑 使用前要将对象先show
使用jquery $('#obj').offset( {top:300, left: 600}); 如果设置offset之前是隐藏的,那么你设置新的offset之后就不会是指定的位置,而且会越飞越远, ...
- uva11626逆时针排序
给一个凸包,要求逆时针排序,刚开始一直因为极角排序就是逆时针的,所以一直wa,后来发现极角排序距离相同是,排的是随机的,所以要对末尾角度相同的点重新排一次 #include<map> #i ...
- HDU 4828 逆元+catalan数
Grids Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Subm ...
- wii 入门之路--fatt
wii 入门之路--fatt system menu:系统界面,开机进入后,显示很多应用,4.1用IOS60. Channel:理解为界面中的软件应用和游戏. IOS:IOS(Input Output ...
- Python flask网站部署总结
先开一贴,有空来总结下前段时间的网站部署情况.此次部署采用Gunicorn + Nginx + supervisor的组合在VPS环境中部署flask网站应用. Ubuntu环境准备 准备python ...
- 008PHP基础知识——运算符(一)
<?php /** * 运算符(一) */ /*PHP中的运算符: * 1.算术运算符: * 2.递增/递减运算符 * 3.比较运算符 * 4.逻辑运算符 * 5.位运算符 * 6.其他运算符 ...
- [SQL]会引起全表扫描的10种SQL语句
1.模糊查询效率很低: 原因:like本身效率就比较低,应该尽量避免查询条件使用like:对于like ‘%...%’(全模糊)这样的条件,是无法使用索引的,全表扫描自然效率很低:另外,由于匹配算法的 ...
- 【hdu1005】Number Sequence
题目描述 一个数列的定义如下: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7. 给出A和B,你要求出f(n). 输入 输 ...
- 网页宽高clientWidth clientHeight获得数值不对的问题
当网页内容撑不满一屏时,通过以下代码获得整个网页高度会有问题 document.body.clientHeight;document.body.clientWidth; 得到的宽高不对,可能是因为ht ...
- mysql function动态执行不同sql语句
create procedure cps() begin ) default 'user'; set strSql = concat('select * from ',table_user); pre ...