zoukankan      html  css  js  c++  java
  • LeetCode 714. 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).

     1 class Solution {
     2     public int maxProfit(int[] prices, int fee) {
     3         if(prices == null || prices.length < 2){
     4             return 0;
     5         }
     6         
     7         int b0 = -prices[0];
     8         int b1 = b0;
     9         int s0 = 0;
    10         int s1 = 0;
    11         for(int i = 1; i<prices.length; i++){
    12             b0 = Math.max(b1, s1-prices[i]);
    13             s0 = Math.max(s1, b1+prices[i]-fee);
    14             
    15             b1 = b0;
    16             s1 = s0;
    17         }
    18         
    19         return s0;
    20     }
    21 }

    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:

     1 class Solution {
     2     public int maxProfit(int[] prices, int fee) {
     3         int tIk0 = 0;
     4         int tIk1 = Integer.MIN_VALUE;
     5         for(int price : prices){
     6             int preTransactionIk0 = tIk0;
     7             tIk0 = Math.max(tIk0, tIk1+price);
     8             tIk1 = Math.max(tIk1, tIk0-price-fee);
     9         }
    10         return tIk0;
    11     }
    12 }

    买卖股票类题目都可以套用这个思路. 

    Reference: https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/discuss/108870/

  • 相关阅读:
    Java(14):面向对象、封装、继承、方法重写、多态、抽象类与接口、内部类
    Java(13):数组、Arrays类、冒泡排序
    Java(12):方法、重载、命令行传参、可变参数、方法调用
    Java(11):switch、dowhile、九九乘法表、打印质数、打印三角形
    Java(10):用户交互Scanner
    Java(9):包
    Java(8):运算符
    Java(7):变量和常量及其规范、作用域
    Mybatis 打印日志
    mysql 更新数据
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/8131710.html
Copyright © 2011-2022 走看看