LeetCode //C - 714. Best Time to Buy and Sell Stock with Transaction Fee

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

You are given an array prices where prices[i] is the price of a given stock on the i t h i^{th} ith day, and an integer fee representing a transaction fee.

Find the maximum profit you can achieve. You may complete as many transactions as you like, but you need to pay the transaction fee for each transaction.

Note:

  • You may not engage in multiple transactions simultaneously (i.e., you must sell the stock before you buy again).
  • The transaction fee is only charged once for each stock purchase and sale.
     
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.

Example 2:

Input: prices = [1,3,7,5,10,3], fee = 3
Output: 6

Constraints:
  • 1 < = p r i c e s . l e n g t h < = 5 ∗ 1 0 4 1 <= prices.length <= 5 * 10^4 1<=prices.length<=5104
  • 1 < = p r i c e s [ i ] < 5 ∗ 1 0 4 1 <= prices[i] < 5 * 10^4 1<=prices[i]<5104
  • 0 < = f e e < 5 ∗ 1 0 4 0 <= fee < 5 * 10^4 0<=fee<5104

From: LeetCode
Link: 714. Best Time to Buy and Sell Stock with Transaction Fee


Solution:

Ideas:

This function iterates over each price in prices and at each step, calculates the maximum profit for holding and not holding a stock, considering the transaction fee. The maximum profit when not holding a stock is either the same as the previous day if no action is taken, or it is the profit from selling the stock held previously plus the current price minus the transaction fee. The maximum profit for holding a stock is either the same as the previous day if no action is taken, or it’s the profit from buying a stock that day, which is the previous day’s profit without holding a stock minus the current price and the transaction fee.

At the end of the iteration, notHold will contain the maximum profit achievable without holding any stock, which is the result we want to return.

Caode:
int maxProfit(int* prices, int pricesSize, int fee) {
   
    // Initialize variables to store the maximum profit when holding and not holding a stock.
    // notHold is initialized to 0 since we start with no stock.
    // hold is initialized to -prices[0] - fee to represent buying the stock on the first day.
    int notHold = 0, hold = -prices[0] - fee;
    
    for (int i = 1; i < pricesSize; i++) {
   
        // Update notHold to be the maximum of itself and hold + prices[i].
        // This represents not holding any stock on day i.
        int prevNotHold = notHold;
        notHold = hold + prices[i] > notHold ? hold + prices[i] : notHold;
        
        // Update hold to be the maximum of itself and prevNotHold - prices[i] - fee.
        // This represents holding a stock on day i.
        hold = prevNotHold - prices[i] - fee > hold ? prevNotHold - prices[i] - fee : hold;
    }
    
    // The maximum profit is when we do not hold any stock at the end.
    return notHold;
}

相关推荐

  1. leetcode704. 二分查找

    2024-02-17 11:06:01       59 阅读
  2. 704. 二分查找

    2024-02-17 11:06:01       52 阅读
  3. Leetcode:704. 二分查找

    2024-02-17 11:06:01       77 阅读
  4. 704.二分查找

    2024-02-17 11:06:01       61 阅读
  5. LeetCode 704 二分查找

    2024-02-17 11:06:01       38 阅读
  6. Leetcode704_二分查找

    2024-02-17 11:06:01       44 阅读
  7. leetcode 704. 二分查找

    2024-02-17 11:06:01       36 阅读
  8. 【Leetcode】741.摘樱桃

    2024-02-17 11:06:01       36 阅读
  9. Leetcode704_二分查找

    2024-02-17 11:06:01       32 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-02-17 11:06:01       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-02-17 11:06:01       106 阅读
  3. 在Django里面运行非项目文件

    2024-02-17 11:06:01       87 阅读
  4. Python语言-面向对象

    2024-02-17 11:06:01       96 阅读

热门阅读

  1. rtt设备io框架面向对象学习-uart设备

    2024-02-17 11:06:01       49 阅读
  2. Linux命令-bzcat命令(解压缩指定的.bz2文件)

    2024-02-17 11:06:01       46 阅读
  3. 索引失效场景

    2024-02-17 11:06:01       45 阅读
  4. 这是 30 年来创办公司的最佳时机。

    2024-02-17 11:06:01       52 阅读
  5. Grafana入门:从0开始打造动态仪表板

    2024-02-17 11:06:01       52 阅读
  6. 2.16C语言学习

    2024-02-17 11:06:01       52 阅读
  7. JDK 8 安装及环境配置

    2024-02-17 11:06:01       54 阅读