专栏首页机器学习入门LWC 55:714. Best Time to Buy and Sell Stock with Transaction Fee

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

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

传送门:714. Best Time to Buy and Sell Stock with Transaction Fee

Problem:

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.

思路: 动态规划,对于每一个可能的买入点都有三种状态:买,卖,什么都不做,枚举出这些状态的组合,求最大profit即可。

目标:

  • 前i天的持有股票数最大,记为:hold[i]
  • 前i天的股票利益最大,记为:unHold[i]

状态转移:

对于第i + 1天,三种状态,买,卖,什么都不做。

买: hold[i] = max{unHold[i] - prices[i + 1] - fee, hold[i - 1]; 卖:unHold[i] = max{hold[i] + prices[i + 1], unHold[i - 1];

代码如下:

     public int maxProfit(int[] prices, int fee) {
         int n = prices.length;
         int[] hold   = new int[n + 1];
         int[] unHold = new int[n + 1];

         hold[0] = Integer.MIN_VALUE;

         for (int i = 1; i <= n; ++i) {
             hold[i]   = Math.max(hold[i - 1], unHold[i - 1] - prices[i - 1] - fee);
             unHold[i] = Math.max(unHold[i - 1], hold[i - 1] + prices[i - 1]);
         }

         return unHold[n];
     }

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • 挑战程序竞赛系列(5):2.1广度优先搜索

    版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.n...

    用户1147447
  • ACM Computer Factory

    思路: 最大流,不要把机器当结点了,而是机器的每个输入和输出当结点,因为输出的结点可以进一步当作另外一台机器的输入。所以每台机器拆分成两个结点,i 和 i ...

    用户1147447
  • 4.7字符串匹配(3)

    用户1147447
  • 学习|Android中JetPack中的几个组件简单使用

    Android JetPack是一整套的库,工具和指南。可帮助开发者更轻松地编写优质应用。这些组件可帮助您遵循最佳做法、让您摆脱编写样板代码的工作并简化复杂任务...

    Vaccae
  • ElasticSearch搜索引擎在SpringBoot中的实践

    首先当然需要安装好elastic search环境,最好再安装上可视化插件 elasticsearch-head来便于我们直观地查看数据。

    CodeSheep
  • ElasticSearch搜索引擎在SpringBoot中的实践

    实验环境 ES版本:5.3.0 spring bt版本:1.5.9 首先当然需要安装好elastic search环境,最好再安装上可视化插件 elastics...

    猿天地
  • 车(ju)一步吃卒

    题目: 在一个 8 x 8 的棋盘上,有一个白色车(rook)。也可能有空方块,白色的象(bishop)和黑色的卒(pawn)。它们分别以字符 “R”,“.”...

    OPice
  • C# 泛型的简单理解(安全、集合、方法、约束、继承)

    泛型允许你在编译时实现类型安全。它们允许你创建一个数据结构而不限于一特定的数据类型。然而,当使用该数据结构时,编译器保证它使用的类型与类型安全是相一致的。泛型提...

    aehyok
  • Leetcode: Valid Sudoku

    题目: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.

    卡尔曼和玻尔兹曼谁曼
  • 关于 “栈” 的那点破事

    用生活中最实际的例子来说,类似于砌砖,加入我们要砌一堵墙,我们肯定是从下往上砌,先拿的砖头就砌在最下边,后拿的砖头就砌到最上边。但是如果监工觉得这堵墙不合适,我...

    村雨遥

扫码关注云+社区

领取腾讯云代金券