原题链接在这里: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. Spring事物原理完全解析

    事务是什么?了解事务的原理吗?说下Spring的事务原理,能自己实现Spring事务原理吗?先自我检测下这些知识掌握了吗.那么接下来一起看下与Spring相关的事务 概念 事务具有ACID特性. 是指 ...

  2. iOS 可变字符串NSMutableString的使用

    .创建一个可变字符串 NSMutableString * ms1 = [[NSMutableString alloc]init]; .可以通过类方法来创建 NSMutableString * ms2 ...

  3. 交通部道路运输车辆卫星定位系统部标JTT808、809、796标准大全

    无论是开发GPS设备硬件还是开发应用软件,都要面临一个标准,这个标准就是国家交通部发布的道路运输车辆卫星定位系统部标认证标准,它涵盖了GPS硬件设备参数.功能标准,也包括了设备上传到应用平台的协议标准 ...

  4. 分布式缓存集群方案特性使用场景(Memcache/Redis(Twemproxy/Codis/Redis-cluster))优缺点对比及选型

    分布式缓存集群方案特性使用场景(Memcache/Redis(Twemproxy/Codis/Redis-cluster))优缺点对比及选型   分布式缓存特性: 1) 高性能:当传统数据库面临大规模 ...

  5. App压力测试MonkeyRunner整理

    压力测试结果:CRASH:崩溃,应用程序在使用过程中,非正常退出ANR:Application Not Responding 命令很多,不用死记,用到复制.粘贴就行,达到目的最重要. 简单通俗易懂点讲 ...

  6. http://blog.csdn.net/milton2017/article/details/54406482

    转自:python 把几个DataFrame合并成一个DataFrame——merge,append,join,conca http://blog.csdn.net/zutsoft/article/d ...

  7. web前端看IE11的变化

    一.User-agent的变化 IE11的User-agent Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv 11.0) like Gecko IE10的U ...

  8. 021——VUE中变异方法 push/unshift pop/shift

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. 十八、dbms_repair(用于检测,修复在表和索引上的损坏数据块)

    1.概述 作用:用于检测,修复在表和索引上的损坏数据块. 2.包的组成 1).admin_tables语法:dbms_repair.admin_tables(table_name in varchar ...

  10. U盘做了一个启动盘来安装Ubuntu,装好后,U盘不能进行格式化了,现在说一下网上找的方法

    参考网址:http://wenwen.sogou.com/z/q289778573.htm 说是这种情况需要对U盘进行低级格式化,具体方法如下: 你可以尝试使用diskpart命令 ① 以管理员身份运 ...