题目链接

https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/

题目原文

Say you have an array for which the ith element is the price of a given stock on day i.

Design an algorithm to find the maximum profit. You may complete at most two transactions.

题目大意

给定不同交易日的股票价格,只能做两次交易,求最大的收益

解题思路

First assume that we have no money, so buy1 means that we have to borrow money from others, we want to borrow less so that we have to make our balance as max as we can(because this is negative).

sell1 means we decide to sell the stock, after selling it we have price[i] money and we have to give back the money we owed, so we have price[i] - |buy1| = prices[i ] + buy1, we want to make this max.

buy2 means we want to buy another stock, we already have sell1 money, so after buying stock2 we have buy2 = sell1 - price[i] money left, we want more money left, so we make it max

sell2 means we want to sell stock2, we can have price[i] money after selling it, and we have buy2 money left before, so sell2 = buy2 + prices[i], we make this max.

So sell2 is the most money we can have.

代码

class Solution(object):def maxProfit(self, prices):""":type prices: List[int]:rtype: int"""s1, s2 = 0, 0b1, b2 = -2147483648, -2147483648for i in range(len(prices)):b1 = max(b1, -prices[i])s1 = max(s1, b1 + prices[i])b2 = max(b2, s1 - prices[i])s2 = max(s2, b2 + prices[i])return s2

转载于:https://www.cnblogs.com/slurm/p/5345977.html

leetcode @python 123. Best Time to Buy and Sell Stock III相关推荐

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

  2. leetcode 123. Best Time to Buy and Sell Stock III | 123. 买卖股票的最佳时机 III(总结DP 模型套路)

    题目 https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/description/ DP 模型套路 DP 套路之:暴力递 ...

  3. 123 Best time to buy and sell stock iii

    题解: 根据题目要求,最多进行两次买卖股票,而且手中不能有2只股票,就是不能连续两次买入操作. 所以,两次交易必须是分布在2各区间内,也就是动作为:买入卖出,买入卖出. 进而,我们可以划分为2个区间[ ...

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

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

  5. 【LeetCode】309. Best Time to Buy and Sell Stock with Cooldown 最佳买卖股票时机含冷冻期(Medium)(JAVA)

    [LeetCode]309. Best Time to Buy and Sell Stock with Cooldown 最佳买卖股票时机含冷冻期(Medium)(JAVA) 题目地址: https: ...

  6. Leetcode Best Time to Buy and Sell Stock III

    Leetcode Best Time to Buy and Sell Stock III,本算法的关键为找出其动态子结构.可以发现,序列中的最小值可以做为其的一个分割,令左边序列为left,右边的序列 ...

  7. 【LeetCode】309. Best Time to Buy and Sell Stock with Cooldown 解题报告(Python C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...

  8. [LeetCOde][Java] Best Time to Buy and Sell Stock III

    题目: Say you have an array for which the ith element is the price of a given stock on day i. Design a ...

  9. 【Leetcode】122. Best Time to Buy and Sell Stock II买卖股票的最佳时机 II

    Best Time to Buy and Sell Stock II 买卖股票的最佳时机 II买卖股票的最佳时机 II Say you have an array for which the ith ...

  10. Best Time to Buy and Sell Stock III

    解题思路来自:https://blog.csdn.net/u012501459/article/details/46514309 Say you have an array for which the ...

最新文章

  1. 关于IIS和Serv-U在同一台服务器中端口冲突问题
  2. Spring4.X系列之AOP-@AspectJ
  3. Py之matplotlibseaborn :高级图可视化之​​​​​​​Q-Q分位数图probplot、​​​​​​​boxplot箱线图、stripplot分类散点图案例应用及代码实现
  4. Git之深入解析如何通过GPG签署和验证工作
  5. pip 删除安装包_Python中PIP的快速指南
  6. 截取图片生成头像插件
  7. 理论基础 —— 队列 —— 循环队列
  8. 【数据分析就业实战】——缺失值的常见处理方法
  9. 会写helloworld,不等于入门
  10. java聚合_Java™ 教程(聚合归纳操作)
  11. 记录一次线上mysql事务隔离级别引发的思考
  12. 分布式技术核心(上)-ZookeeperDubbo
  13. Android PackageManager详解
  14. 计算机ppt里怎么应用背景图,如何快速生成一个PPT图片背景“遮罩”?-ppt背景图片怎么设置...
  15. 疲劳检测——眨眼检测
  16. 收敛因子和黄金正弦指引机制的蝴蝶优化算法
  17. 7PTA7-6 结伴同行去秋游
  18. 公钥基础设施 (Public-key infrastructure PKI)
  19. Java设计登录界面——GUI
  20. Python —— 列表的while循环遍历 、for循环遍历

热门文章

  1. 5 月编程语言排行榜:Java第一,R 跌出Top20,Python成最大赢家
  2. 我必须得告诉大家的 MySQL 优化原理
  3. 到底该如何理解 Unix/Linux 的文件系统?看这篇就知道了
  4. 刚毕业就年薪百万,谁说知识不是财富
  5. 让你变懒的 Android Studio Live Templates
  6. 线性回归公式推导与代码实现
  7. mysql添加一个字段(
  8. 使用openssl实现ECDSA签名以及验证功能(附完整测试源码)
  9. 获取cookies的简单代码(总结待续)
  10. CentOS 7 (RHEL 7)服务管理命令的变化