题目

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

题解

经典的 暴力递归 -> 傻缓存 -> DP,重要思路来源于之前做过的 leetcode 309. Best Time to Buy and Sell Stock with Cooldown | 309. 最佳买卖股票时机含冷冻期(动态规划)

当年看答案也摸不清门路的 dp,现在应不是玄学了,哈哈。

将「买入」和「卖出」分开进行考虑:「买入」为负收益,而「卖出」为正收益。在初入股市时,你只有「买入」的权利,只能获得负收益。而当你「买入」之后,你就有了「卖出」的权利,可以获得正收益。显然,我们需要尽可能地降低负收益而提高正收益,因此我们的目标总是将收益值最大化。因此,我们可以使用动态规划的方法,维护在股市中每一天结束后可以获得的「累计最大收益」,并以此进行状态转移,得到最终的答案。

没有草稿,直接上代码:

class Solution {public int maxProfit(int[] prices, int fee) {// Approach 1: Recursive, Brute Force
//        return process1(prices, 0, false,fee);// Approach 2: Recursion with Memoization
//        int[][] dp = new int[2][prices.length + 1];
//        Arrays.fill(dp[0], Integer.MIN_VALUE);
//        Arrays.fill(dp[1], Integer.MIN_VALUE);
//        dp[0][prices.length] = 0;
//        dp[1][prices.length] = 0;
//        return process2(prices, 0, 0, fee, dp);// Approach 3: Dynamic Programmingint[][] dp = new int[2][prices.length + 1];Arrays.fill(dp[0], Integer.MIN_VALUE);Arrays.fill(dp[1], Integer.MIN_VALUE);dp[0][prices.length] = 0;dp[1][prices.length] = 0;for (int i = prices.length - 1; i >= 0; i--) {for (int pending = 0; pending <= 1; pending++) {int p1 = Integer.MIN_VALUE;int p2 = Integer.MIN_VALUE;int p3 = Integer.MIN_VALUE;int p4 = Integer.MIN_VALUE;if (pending == 1) {p1 = dp[1][i + 1]; // 不卖p2 = dp[0][i + 1] + prices[i] - fee; // 卖} else {p3 = dp[1][i + 1] - prices[i]; // 买p4 = dp[0][i + 1]; // 不买}dp[pending][i] = Math.max(Math.max(p1, p2), Math.max(p3, p4));}}return dp[0][0];}// 从i位置开始做决策,获得的最大收益(pending表示当前是否持有股票)
//    public int process1(int[] prices, int i, boolean pending, int fee) {//        if (i == prices.length) return 0;
//
//        int p1 = Integer.MIN_VALUE;
//        int p2 = Integer.MIN_VALUE;
//        int p3 = Integer.MIN_VALUE;
//        int p4 = Integer.MIN_VALUE;
//        if (pending) {// 持有股票
//            p1 = process1(prices, i + 1, true, fee); // 不卖
//            p2 = process1(prices, i + 1, false, fee) + prices[i] - fee; // 卖
//        } else { // 未持有股票
//            p3 = process1(prices, i + 1, true, fee) - prices[i]; // 买
//            p4 = process1(prices, i + 1, false, fee); // 不买
//        }
//        return Math.max(Math.max(p1, p2), Math.max(p3, p4));
//    }//    public int process2(int[] prices, int i, int pending, int fee, int[][] dp) {//        if (dp[pending][i] != Integer.MIN_VALUE) return dp[pending][i];
//
//        int p1 = Integer.MIN_VALUE;
//        int p2 = Integer.MIN_VALUE;
//        int p3 = Integer.MIN_VALUE;
//        int p4 = Integer.MIN_VALUE;
//        if (pending == 1) {// 持有股票
//            p1 = process2(prices, i + 1, 1, fee, dp); // 不卖
//            p2 = process2(prices, i + 1, 0, fee, dp) + prices[i] - fee; // 卖
//        } else { // 未持有股票
//            p3 = process2(prices, i + 1, 1, fee, dp) - prices[i]; // 买
//            p4 = process2(prices, i + 1, 0, fee, dp); // 不买
//        }
//
//        int result = Math.max(Math.max(p1, p2), Math.max(p3, p4));
//        dp[pending][i] = result;
//        return result;
//    }
}

leetcode 714. Best Time to Buy and Sell Stock with Transaction Fee | 714. 买卖股票的佳最时机含手续费(递归->傻缓存->dp)相关推荐

  1. 【DP + 卖股票】LeetCode 714. Best Time to Buy and Sell Stock with Transaction Fee

    LeetCode 714. Best Time to Buy and Sell Stock with Transaction Fee Solution1: 参考网址:http://www.cnblog ...

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

  3. leetcode 309. Best Time to Buy and Sell Stock with Cooldown | 309. 最佳买卖股票时机含冷冻期(动态规划)

    题目 https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/ 题解 我知道要dp,但是不知道要怎么dp ...

  4. 动态规划——买卖股票的最优时机含手续费(Leetcode 714)

    题目选自​​​​​​Leetcode 714. 买卖股票的最佳时机含手续费 本题和 122. 买卖股票的最佳时机 II 是非常类似的题,唯一的区别就在于本题有「手续费」而第 122 题没有. 问题描述 ...

  5. LeetCode 123. Best Time to Buy and Sell Stock III--Python解法--动态规划--数学题

    此文首发于我的个人博客:zhang0peter的个人博客 LeetCode题解文章分类:LeetCode题解文章集合 LeetCode 所有题目总结:LeetCode 所有题目总结 题目地址:Best ...

  6. 【DP + 卖股票】LeetCode 309. Best Time to Buy and Sell Stock with Cooldown

    LeetCode 309. Best Time to Buy and Sell Stock with Cooldown Solution1: 比较有难度的一道动态规划题了! 参考网址:http://z ...

  7. 【DP + 卖股票】LeetCode 188. Best Time to Buy and Sell Stock IV

    LeetCode 188. Best Time to Buy and Sell Stock IV Solution1:我的答案 参考链接:http://www.cnblogs.com/grandyan ...

  8. 【DP + 卖股票】LeetCode 123. Best Time to Buy and Sell Stock III

    LeetCode 123. Best Time to Buy and Sell Stock III Solution1: 不得不让人感叹算法之精妙啊!!! 参考网址:[1]http://www.cnb ...

  9. 【贪心 和 DP + 卖股票】LeetCode 122. Best Time to Buy and Sell Stock II

    LeetCode 122. Best Time to Buy and Sell Stock II Solution1:我的答案 贪心和DP傻傻分不清! class Solution { public: ...

最新文章

  1. 你属于程序员中的哪种人?
  2. 解决TextView排版混乱或者自动换行的问题
  3. Tips-Windows 10【多桌面视窗】操作
  4. C#全局键盘监听(Hook)
  5. 教师编学科知识计算机,教师考试信息技术学科知识考什么_谈信息技术学科教师应该怎样教学...
  6. 工业机器人打磨抛光编程员工资_让我们一起来谈谈,工业机器人行业的真实工资是多少?...
  7. python中用def实现自动排序_漫画排序算法Python实现
  8. 搞懂Java的反射机制
  9. Telephone Wire(POJ-3612)
  10. 开课吧课堂之如何创建多级类层次
  11. 浙江大学2017年数学分析考研试题
  12. 请领导过目文件怎么说_职场话题:当领导说“你定吧”,你会怎么做?
  13. 国美易卡利用数据挖掘技术,国美易卡精准分析决策
  14. python迷宫小游戏代码_python迷宫游戏,迷宫生成,解决与可视化
  15. linux网速卡怎么办_手机信号差怎么办?
  16. 复旦大学计算机科学与技术,复旦大学计算机科学技术学院
  17. 艰涩难懂,不存在的,消息队列其实很简单
  18. Sparse R-CNN: End-to-End Object Detection with Learnable Proposals
  19. S-function入门及案例详解(2)——S-function基本案例介绍
  20. 【YOLO系列】YOLOv3

热门文章

  1. 牛客多校8 - Enigmatic Partition(二阶差分)
  2. CodeForces - 1326E Bombs(线段树+思维)
  3. 洛谷 - P2756 飞行员配对方案问题(二分图最大匹配+路径打印)
  4. plpythonu_postgresql plpythonu例子
  5. centos7修改ip地址命令_CentOS7 安装 httpd 服务
  6. 交叉编译openwrt php,构建 openwrt 交叉编译工具链
  7. Wireshark 抓包分析 RTSP/RTP/RTCP 基本工作过程
  8. 一文读懂浏览器存储与缓存机制
  9. Error:java: 无效的标记 -version 编译错误的解决办法
  10. 2020 JVM生态报告