[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 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
.
还是买卖股票的最佳时间问题,这题每一次买卖时会有交易费。
解法:DP。第i天的利润分成两个,用两个dp数组分别进行计算,buy[i], sell[i]。
初始值:buy[0]=-prices[0], sell[0]=0
公式:
buy[i] = Math.max(buy[i - 1], sell[i - 1] - prices[i])
第i天买,如果第i-1天是买,就不能买了,利润是buy[i-1]。如果i-1天是卖,就可以买,利润是sell[i-1] - prices[i]。
sell[i] = Math.max(sell[i - 1], buy[i - 1] + prices[i])
第i天卖,如果第i-1天是卖,就不能卖了,利润是sell[i-1]。如果i-1天是买,就可以卖,利润是buy[i - 1] + prices[i]。
Most consistent ways of dealing with the series of stock problems
Java: pay the fee when buying the stock
public int maxProfit(int[] prices, int fee) {
if (prices.length <= 1) return 0;
int days = prices.length, buy[] = new int[days], sell[] = new int[days];
buy[0]=-prices[0]-fee;
for (int i = 1; i<days; i++) {
buy[i] = Math.max(buy[i - 1], sell[i - 1] - prices[i] - fee); // keep the same as day i-1, or buy from sell status at day i-1
sell[i] = Math.max(sell[i - 1], buy[i - 1] + prices[i]); // keep the same as day i-1, or sell from buy status at day i-1
}
return sell[days - 1];
}
Java: pay the fee when selling the stock
public int maxProfit(int[] prices, int fee) {
if (prices.length <= 1) return 0;
int days = prices.length, buy[] = new int[days], sell[] = new int[days];
buy[0]=-prices[0];
for (int i = 1; i<days; i++) {
buy[i] = Math.max(buy[i - 1], sell[i - 1] - prices[i]); // keep the same as day i-1, or buy from sell status at day i-1
sell[i] = Math.max(sell[i - 1], buy[i - 1] + prices[i] - fee); // keep the same as day i-1, or sell from buy status at day i-1
}
return sell[days - 1];
}
Python:
class Solution(object):
def maxProfit(self, prices, fee):
"""
:type prices: List[int]
:type fee: int
:rtype: int
"""
cash, hold = 0, -prices[0]
for i in xrange(1, len(prices)):
cash = max(cash, hold+prices[i]-fee)
hold = max(hold, cash-prices[i])
return cash
C++:
class Solution {
public:
int maxProfit(vector<int>& prices, int fee) {
int s0 = 0, s1 = INT_MIN;
for(int p:prices) {
int tmp = s0;
s0 = max(s0, s1+p);
s1 = max(s1, tmp-p-fee);
}
return s0;
}
};
All LeetCode Questions List 题目汇总
[LeetCode] 714. Best Time to Buy and Sell Stock with Transaction Fee 买卖股票的最佳时间有交易费的更多相关文章
- 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 解题报告(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 ...
- 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 ...
- 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 ...
- 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] 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 Best Time to Buy and Sell Stock with Transaction Fee
原题链接在这里:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/descripti ...
随机推荐
- selenium与webdriver驱动与firefox、 chrome匹配版本
一.Chrome python3 selenium3.11.0(直接pip安装即可) chromedriver_win32:2.38 Chrome版本: 65.0.3325.146(正式版本)(32 ...
- 《exception》第九次团队作业:Beta冲刺与验收准备(第一天)
一.项目基本介绍 项目 内容 这个作业属于哪个课程 任课教师博客主页链接 这个作业的要求在哪里 作业链接地址 团队名称 Exception 作业学习目标 1.掌握软件黑盒测试技术:2.学会编制软件项目 ...
- Java动态代理演变之路
1.什么是代理? 代理,英文成文Proxy.意思是你不用去做,别人代替你去处理.比如有人想找明星周董去唱歌,他需要做签约.讨论.唱歌和付款等等过程,但真正周董擅长的事情是唱歌,其他的事情可以交代给他的 ...
- qt事件机制(转)
学习了一段时间的Qt之后,发现Qt的事件机制和其他语言的机制有些不同.Qt除了能够通过信号和槽机制来实现一些Action动作之外,还可以用对象所带的事件,或者用户自定义的事件来实现对象的一些行为处理. ...
- jQuery通用遍历方法each的实现
each介绍 jQuery 的 each 方法,作为一个通用遍历方法,可用于遍历对象和数组. 语法为: jQuery.each(object, [callback]) 回调函数拥有两个参数:第一个为对 ...
- react编写规范之组件组件的内容编写顺序
static 开头的类属性,如 defaultProps.propTypes. 构造函数,constructor. getter/setter(还不了解的同学可以暂时忽略). 组件生命周期. _ 开头 ...
- hdu6172&&hdu6185&&P5487——BM算法
hdu6172 模板的简单应用 先根据题中的表达式求出前几项,再上BM,注意一下n的大小关系. #include <bits/stdc++.h> using namespace std; ...
- Spring之IOC(控制反转)与AOP(面向切面编程)
控制反转——Spring通过一种称作控制反转(IoC)的技术促进了松耦合.当应用了IoC,一个对象依赖的其它对象会通过被动的方式传递进来,而不是这个对象自己创建或者查找依赖对象.可以认为IoC与JND ...
- 【BZOJ4237】 稻草人 CDQ分治+单调栈
## 题目描述 JOI村有一片荒地,上面竖着N个稻草人,村民们每年多次在稻草人们的周围举行祭典. 有一次,JOI村的村长听到了稻草人们的启示,计划在荒地中开垦一片田地.和启示中的一样,田地需要满足以下 ...
- 关于H5判定区域里面滑动到底部,加载更多的总结
1.如何判定H5中滑动到底部,然后加载更多的功能实现. 思路:我们需要设定一个固定高度的盒子,然后我们利用scroll来监听滚动,当scrollTop(滚动的距离) + clientHeight(页面 ...