• 121. Best Time to Buy and Sell Stock:股票一次买入卖出,求最大利润
class Solution(object):def maxProfit(self, prices):if prices == []: return 0minNum,ret = prices[0],0for p in prices:minNum = min(minNum,p)ret = max(ret,p-minNum)return ret
  • 122. Best Time to Buy and Sell Stock II:股票多次买入卖出
    思路:判断第i天和第i-1天的价格差diff,把diff>0部分求和即可
    计算相邻股票价格的差值,计算差值大于0的元素的和,即为最大利润
class Solution(object):def maxProfit(self, prices):if prices == []: return 0ans = 0for i in range(1,len(prices)):diff = prices[i]-prices[i-1]if diff>0:  ans+=diffreturn ans
  • 714. Best Time to Buy and Sell Stock with Transaction Fee:买入卖出次数不限,但是每次需要支付一定的费用,求最大的收益,Medium
    思路:

sell维护买入卖出价格(t[0],t[1]),其中t[1]为0表示未卖出
如果栈顶未卖出,且price<=buy 替换买入
如果price大于max(卖出价格,买入价格+费用)替换卖出
price作为买入价格入栈
当栈元素>1,并且合并栈顶的两组交易可以获得更大收益时,对栈顶的两个交易进行合并
a2-a1-fee+b2-b1-fee< b2-a1-fee –> a2

class Solution(object):def maxProfit(self, prices, fee):""":type prices: List[int]:type fee: int:rtype: int"""sell = [[50000,0]]for price in prices:# 如果栈顶未卖出,且price<=buy 替换买入if sell[-1][1]==0 and price<sell[-1][0]:sell[-1][0] = price# 如果price大于max(卖出价格,买入价格+费用)替换卖出elif price>=max(sell[-1][0]+fee,sell[-1][1]):sell[-1][1] = price# price作为买入价格入栈elif sell[-1][1]:sell.append([price,0])# a2-a1-fee+b2-b1-fee<b2-a1-fee --> a2<b1+feewhile(len(sell)>1 and sell[-2][1]<sell[-1][0]+fee):sell[-1][1] = max(sell.pop()[1],sell[-1][1])return sum(t[1]-t[0]-fee for t in sell if t[1]-t[0]>fee)

思路还是偏复杂了。。在kdd的指点,记两个状态,买p1和卖p2

p1 = max(p1,p2-price)
p2 = max(p2,p1+price-fee)

class Solution(object):def maxProfit(self, prices, fee):""":type prices: List[int]:type fee: int:rtype: int"""size = len(prices)if size<=1: return 0p1,p2 = -prices[0],0for i in range(1,size):p1 = max(p1,p2-prices[i])p2 = max(p2,p1+prices[i]-fee)return p2
  • 309. Best Time to Buy and Sell Stock with Cooldown:多次买入卖出,但是每次卖出后第二天不能买入,Medium

buy[i]表示时间为i前操作为买入的最大收益
sell[i]表示时间为i前操作为卖出的最大收益
rest[i]表示时间为i前操作为冷冻器的最大收益

buy[i] = max(buy[i-1],rest[i-1]-price) 前一天购买或者前一天冷冻当天购买
sell[i] = max(sell[i-1],buy[i-1]+price) 前一天卖出或者前一天购买当前卖出
rest[i] = max(rest[i-1],buy[i-1],sell[i-1])
buy[i]<=rest[i] 又有 rest[i] = sell[i-1] (因为rest是在sell操作之后才有的东西)
所以变成
buy[i] = max(buy[i-1],sell[i-2]-price)
sell[i] = max(sell[i-1],buy[i-1]+price)

class Solution(object):def maxProfit(self, prices):""":type prices: List[int]:rtype: int"""if len(prices)<2:return 0sell, buy, prev_sell, prev_buy = 0, -prices[0], 0, 0for price in prices:prev_buy = buybuy = max(prev_sell - price, prev_buy)prev_sell = sellsell = max(prev_buy + price, prev_sell)return sell
  • 123. Best Time to Buy and Sell Stock III:最多买卖两次,求最大利润,Hard

计算相邻股票价格的差值
遍历i
prev[i]表示从左到右第i位的最大利润
post[i]表示从右到左第i位的最大利润
ans=max(ans,prev[i]+post[i])

class Solution(object):def maxProfit(self,prices):if prices == []: return 0size = len(prices)MIN,MAX = prices[0],prices[size-1]prev,post = [0],[0]for i in range(1,size):x = prices[i]y = prices[size-1-i]MAX,MIN = max(MAX,y),min(MIN,x)prev.append(max(prev[-1],x-MIN))post.append(max(post[-1],MAX-y))post.reverse()ret = 0for i in range(size):ret = max(ret,prev[i]+post[i])return ret
  • 188. Best Time to Buy and Sell Stock IV:至多k次买入卖出

global G[i][j] 到第i天至多进行j次交易的最优利润
local G[i][j] 到第i天至多进行j次交易的最优利润,且在第i天卖出
L[i][j] = max(G[i-1][j-1],G[i-1][j-1]+diff,L[i-1][j]+diff)
说明:
G[i-1][j-1]为第i-1天进行j-1次交易的最优利润,在第i天没有买入卖出行为
G[i-1][j-1]+diff为第i-1天进行j-1次交易,然后第i-1天买入,第i天卖出
L[i-1][j]+diff 为第i-1天进行第j次交易,改为第i天进行第j次交易
G[i][j] = max(G[i-1][j],L[i][j])

class Solution(object):def maxProfit(self, k, prices):""":type k: int:type prices: List[int]:rtype: int"""if prices == [] or k ==0 : return 0size = len(prices)if k>(size/2): return self.maxProfit1(prices)L = [[0 for i in range(k+1)] for p in prices]G = [[0 for i in range(k+1)] for p in prices]for j in range(1,k+1):for i in range(1,len(prices)):diff = prices[i]-prices[i-1]L[i][j] = max(G[i-1][j-1],G[i-1][j-1]+diff,L[i-1][j]+diff)G[i][j] = max(G[i-1][j],L[i][j])return G[len(prices)-1][k]def maxProfit1(self, prices):if prices == []: return 0ans = 0for i in range(1,len(prices)):diff = prices[i]-prices[i-1]if diff>0:  ans+=diffreturn ans

算法专题训练(1)股票问题相关推荐

  1. leetcode算法专题训练:十四.位操作专题

    文章目录 十四.位操作专题 50.Pow(x,n) 69.x的平方根 136.只出现一次的数字 137.只出现一次的数字2 260.只出现一次的数字3 89.格雷编码 剑指 Offer 64. 求1+ ...

  2. ACM-大一训练第三周(Floyd算法+并查集算法专题训练)

  3. 杭电ACM-LCY算法进阶培训班-专题训练15

    杭电ACM-LCY算法进阶培训班-专题训练(03-07-11-15) 1012 最短路 #pragma GCC optimize(2) #pragma GCC optimize(3,"Ofa ...

  4. 杭电ACM-LCY算法进阶培训班-专题训练(矩阵快速幂)

    杭电ACM-LCY算法进阶培训班-专题训练(矩阵快速幂)[模板] 传送门 杭电ACM-LCY算法进阶培训班-专题训练(矩阵快速幂)[模板] 矩阵快速幂模板 Count Problem Descript ...

  5. 杭电ACM-LCY算法进阶培训班-专题训练(计算几何入门)

    杭电ACM-LCY算法进阶培训班-专题训练(计算几何入门) 传送门 杭电ACM-LCY算法进阶培训班-专题训练(计算几何入门) Shape of HDU Problem Description Inp ...

  6. 杭电ACM-LCY算法进阶培训班-专题训练(KMP)

    杭电ACM-LCY算法进阶培训班-专题训练(KMP) 杭电ACM-LCY算法进阶培训班-专题训练(KMP) 剪花布条 Problem Description Input Output Sample I ...

  7. 2021-06-18杭电ACM-LCY算法进阶培训班-专题训练16

    杭电ACM-LCY算法进阶培训班-专题训练(04-08-12-16) 1009 Intervals #pragma GCC optimize(2) #pragma GCC optimize(3,&qu ...

  8. 杭电ACM-LCY算法进阶培训班-专题训练09

    杭电ACM-LCY算法进阶培训班-专题训练09 1014 剪花布条 #pragma GCC optimize(2) #pragma GCC optimize(3,"Ofast",& ...

  9. 杭电ACM-LCY算法进阶培训班-专题训练(线段树)

    杭电ACM-LCY算法进阶培训班-专题训练(线段树) 传送门 目录 杭电ACM-LCY算法进阶培训班-专题训练(线段树) 张煊的金箍棒(2) Problem Description Input Out ...

  10. 并查集算法总结专题训练

    并查集算法总结&专题训练 1.概述 2.模板 3.例题 1.入门题: 2.与别的算法结合: 3.考思维的题: 4.二维转一维: 5.扩展域并查集&边带权并查集: 4.总结 1.概述 并 ...

最新文章

  1. R语言使用yardstick包的lift_curve函数评估多分类(Multiclass)模型的性能、并使用autoplot函数可视化模型在每个交叉验证(或者重采样)的每一折fold在每个分类上的提升
  2. 40岁后才明白的道理:人一生奋斗余地很有限--转载
  3. 检查Android应用程序是否在后台运行
  4. Android创建文件为unix格式,Recovery模式下/cache/recovery/目录下创建文件,开机后文件被删除...
  5. Boost:boost::asio模块的allocation service分配服务测试程序
  6. 数字图像处理之边缘检测,图像分割
  7. 技术面试问项目难题如何解决的_技术创新 | 降本增效,青海农信社项目小伙刻苦钻研解决联合支架设计难题!...
  8. python写一个crm系统_用Python打造一个CRM系统(四)
  9. 软考信息系统项目管理师_历年真题_2020下半年错题集_上午综合知识题---软考高级之信息系统项目管理师036
  10. c# session总结
  11. Ajax+Java实现大文件切割上传
  12. java程序员集合框架面试题_常见的Java集合框架面试题目及回答
  13. google earth pro 安装后启动时报错:google 地球无法连接到登录服务器(错误代码:c000000c)
  14. 小程序与MySQL数据库的交互_微信小程序数据库交互如何做到?
  15. 消逝的光芒 Dying Light for Mac 跑酷僵尸游戏 动作生存游戏
  16. 使用C++代码创建一个Windows桌面应用程序
  17. php 判断字符串类型及长度
  18. 团队任务3每日立会(2018-10-26)
  19. node 安装(新)
  20. 苹果手机隐私分析数据是什么_苹果公司以用户为中心的隐私保护方法能教给我们什么?

热门文章

  1. 什么是FBO (Frame Buffer Object)
  2. BiliBili视频下载
  3. 终端溯源图构建工具SPADE专题-1 SPADE工具安装
  4. 通过Python获取拉钩招聘网站的公司详细地址
  5. 手把手教你用VMware安装Centos7.9镜像(史上最详细)
  6. php链路追踪molten
  7. Port Forwarding in Windows
  8. 使用MindSpore进行一阶导数计算
  9. MR-GMMapping:基于高斯混合模型的通信高效多机器人映射系统
  10. Cisco Viptela SD-WAN实验