原题链接在这里: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的更多相关文章

  1. [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 ...

  2. 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 ...

  3. 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 ...

  4. 714. Best Time to Buy and Sell Stock with Transaction Fee

    问题 给定一个数组,第i个元素表示第i天股票的价格,可执行多次"买一次卖一次",每次执行完(卖出后)需要小费,求最大利润 Input: prices = [1, 3, 2, 8, ...

  5. [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 ...

  6. 【LeetCode】714. Best Time to Buy and Sell Stock with Transaction Fee 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...

  7. 【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 ...

  8. [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 ...

  9. 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 ...

随机推荐

  1. poi 取excel单元格内容时,需要判断单元格的类型,才能正确取出

    以下内容非原创,原文链接http://blog.sina.com.cn/s/blog_4b5bc01101015iuq.html ate String getCellValue(HSSFCell ce ...

  2. git 的日常使用命令

    全视图了解:看完下面内容,再回头看,会有不一样的风景! 1.明白git的四个区 Workspace(工作区):平时我们写代码的地方. Index(暂存区):写完代码后让它变成的待提交的状态. Repo ...

  3. LeetCode 454. 4Sum II

    454. 4Sum II Add to List Description Submission Solutions Total Accepted: 8398 Total Submissions: 18 ...

  4. 12.详解Condition的await和signal等待通知机制

    1.Condition简介 任何一个java对象都天然继承于Object类,在线程间实现通信的往往会应用到Object的几个方法,比如wait(),wait(long timeout),wait(lo ...

  5. 十八 Python分布式爬虫打造搜索引擎Scrapy精讲—Scrapy启动文件的配置—xpath表达式

    我们自定义一个main.py来作为启动文件 main.py #!/usr/bin/env python # -*- coding:utf8 -*- from scrapy.cmdline import ...

  6. canvas之图形的变化(平移,缩放,旋转)

    1.保存与恢复canvas状态 ctx.save();暂时将当前的状态保存到堆中 ctx.restore();该方法用于将上一个保存的状态从堆中再次取出,恢复该状态的所有设置. <!DOCTYP ...

  7. bzoj1997

    题解: 在圆上面的点能不能不交叉 和那一题差不多 http://www.cnblogs.com/xuanyiming/p/8110597.html 代码: #include<bits/stdc+ ...

  8. MoreEffectiveC++Item35(异常)(条款9-15)

    条款9 使用析构函数防止内存泄漏 条款10 在构造函数中防止内存泄漏 条款11 禁止异常信息传递到析构函数外 条款12 理解"抛出一个异常''与"传递一个参数"或调用一个 ...

  9. 【Shell脚本】逐行处理文本文件

    经常会对文体文件进行逐行处理,在Shell里面如何获取每行数据,然后处理该行数据,最后读取下一行数据,循环处理.有多种解决方法如下: 1.通过read命令完成. read命令接收标准输入,或其他文件描 ...

  10. d3.js入门之DOM操作

    上篇成功在vue项目中把d3跑起来了,接下来对d3的基本操作做个汇总: 一.d3元素选择器 d3.select(".skill"):选择第一个类名为skill的元素并返回这个元素对 ...