当前位置: 首页 > news >正文

leetcode 309. Best Time to Buy and Sell Stock with Cooldown

题意

买股票,中间买卖完一次后必须休息一下,求最大收益

题解

建议观看视频 ->->->-> https://www.bilibili.com/video/av31578180

状态转移图
1172239-20190804174650974-15237438.png

buy[i] 代表当前持有股票的最大收益
sell[i] 代表当前卖出股票的最大收益
rest[i] 代表当前休息的最大收益

class Solution {
public:
    // buy[i] -> util day i hold the stock max profit
    // sell[i] -> util day i sell the stock i max profit
    // rest[i] -> util day i rest max profit
    int maxProfit(vector<int>& prices) {
        const int INF = 0x3f3f3f3f;
        int len = prices.size();
        int buy[len+1] = {0};
        int sell[len+1] = {0};
        int rest[len+1] = {0};
        buy[0] = -INF; rest[0] = 0; sell[0] = 0;
        for(int i=1; i<=len; i++) {
            buy[i] = max(buy[i-1], rest[i-1] - prices[i-1]);
            sell[i] = prices[i-1] + buy[i-1];
            rest[i] = max(rest[i-1], sell[i-1]);
        }
        return max(sell[len], rest[len]);
    }
};

滚动数组优化空间

class Solution {
public:
    // buy[i] -> util day i hold the stock max profit
    // sell[i] -> util day i sell the stock i max profit
    // rest[i] -> util day i rest max profit
    int maxProfit(vector<int>& prices) {
        const int INF = 0x3f3f3f3f;
        int len = prices.size();
        int buy[2] = {0};
        int sell[2] = {0};
        int rest[2] = {0};
        buy[0] = -INF; rest[0] = 0; sell[0] = 0;
        for(int i=1; i<=len; i++) {
            buy[i % 2] = max(buy[(i-1) % 2], rest[(i-1) % 2] - prices[i-1]);
            sell[i % 2] = prices[i-1] + buy[(i-1) % 2];
            rest[i % 2] = max(rest[(i-1) % 2], sell[(i-1) % 2]);
        }
        return max(sell[len%2], rest[len%2]);
    }
};

转载于:https://www.cnblogs.com/Draymonder/p/11298982.html

相关文章:

  • 环境变量
  • 手机端和网页端使用同一后台时进行会话控制
  • SpringBoot起步
  • 获取当前python 解释器的路径=.=
  • $.proxy和$.extend
  • Java Web 开发必须掌握的三个技术:Token、Cookie、Session
  • springBoot测试
  • SpringBoot传参方式
  • Springboot项目自动加载设置
  • SpringBoot项目打包
  • Win10修改字体
  • c언어 database
  • Flex 特效组件
  • project bitnami redmine project manager 4.0.4-1
  • JavaWeb过滤器(Filter)
  • 【跃迁之路】【463天】刻意练习系列222(2018.05.14)
  • 0基础学习移动端适配
  • 4月23日世界读书日 网络营销论坛推荐《正在爆发的营销革命》
  • Angular数据绑定机制
  • Git初体验
  • Intervention/image 图片处理扩展包的安装和使用
  • isset在php5.6-和php7.0+的一些差异
  • JDK9: 集成 Jshell 和 Maven 项目.
  • mysql innodb 索引使用指南
  • spark本地环境的搭建到运行第一个spark程序
  • Traffic-Sign Detection and Classification in the Wild 论文笔记
  • Vue小说阅读器(仿追书神器)
  • 关键词挖掘技术哪家强(一)基于node.js技术开发一个关键字查询工具
  • 检测对象或数组
  • 七牛云假注销小指南
  • 前端学习笔记之观察者模式
  • 前端学习笔记之原型——一张图说明`prototype`和`__proto__`的区别
  • 如何优雅地使用 Sublime Text
  • 扫描识别控件Dynamic Web TWAIN v12.2发布,改进SSL证书
  • 深入浅出webpack学习(1)--核心概念
  • 收藏好这篇,别再只说“数据劫持”了
  • 数据可视化之 Sankey 桑基图的实现
  • 新书推荐|Windows黑客编程技术详解
  • 一天一个设计模式之JS实现——适配器模式
  • 资深实践篇 | 基于Kubernetes 1.61的Kubernetes Scheduler 调度详解 ...
  • %3cscript放入php,跟bWAPP学WEB安全(PHP代码)--XSS跨站脚本攻击
  • (11)MSP430F5529 定时器B
  • (C#)一个最简单的链表类
  • (c语言版)滑动窗口 给定一个字符串,只包含字母和数字,按要求找出字符串中的最长(连续)子串的长度
  • (二)fiber的基本认识
  • (附源码)spring boot建达集团公司平台 毕业设计 141538
  • (原創) 未来三学期想要修的课 (日記)
  • (状压dp)uva 10817 Headmaster's Headache
  • .NET 简介:跨平台、开源、高性能的开发平台
  • .net 流——流的类型体系简单介绍
  • .NET轻量级ORM组件Dapper葵花宝典
  • .NET实现之(自动更新)
  • @font-face 用字体画图标
  • [<死锁专题>]
  • [20180129]bash显示path环境变量.txt