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 ...
随机推荐
- angularjs定时任务的设置与清除
人们似乎常常将AngularJS中 的$timeOut() $interval()函数看做是一个内置的.无须在意的函数.但是,如果你忘记了$timeOut()$interval()的回调函数将会造成 ...
- windows service 2008 内存吃尽解决方案
最近用windows2008,发现系统运行一段时间后,内存吃紧,赶紧打开资源查看器,发现当前运行的程序占有内存都很小,后经查资料,原来是被windows2008的文件缓存吃尽了,这是windows20 ...
- 19.并发容器之BlockingQueue
1. BlockingQueue简介 在实际编程中,会经常使用到JDK中Collection集合框架中的各种容器类如实现List,Map,Queue接口的容器类,但是这些容器类基本上不是线程安全的,除 ...
- 你真的会使用Chrome开发者工具吗?
Chrome开发工具是一个Web开发者的利器,使用它你可以实现: 管理界面元素 断点调试JavaScript代码 优化你的代码 这里将列出一些非常实用的使用技巧,帮助你更加高效的工作 1. 快速编辑H ...
- hdu 4679 Terrorist’s destroy 树的直径+dp
题意:给你一棵树,每条边都有值W,然后问你去掉一条边,令val = w*max(两颗新树的直径),求val最小值~ 做法,先求树的直径,然后算出直径上每个点的最长枝条长度.这样对于每一条边,假如是枝条 ...
- restframework api(基础2)
一 socket简介 1 最简单的socket #################server import socket ip_port = ('127.0.0.1',9997) sk = sock ...
- 2: 使用Prism初始化程序 Initializing Applications Using the Prism Library 5.0 for WPF(英汉对照版)
This topic addresses what needs to happen to get a Prism for WPF application up and running. A Prism ...
- vue开发环境搭建Mac版
一.前言 要做一个移动端app,面对webapp最流行的三个技术React,angular,vue,三选一,如何选,可参考blog移动app技术选型,react,angular, vue, 下面是对 ...
- 一张图带你了解OKhttp框架
- 今天开始写些随笔,就从Jplayer开始吧
今天才开始用Jplayer,可能有点落伍了,但是看到网上千篇一律的使用说明,开始决定把自己的使用心得分享一下,废话不多说,开始吧. Step1: 官网上有具体的搭建顺序,URL:http://www. ...