LeetCode 309. Best Time to Buy and Sell Stock with Cooldown--Java解法-卖股票系列题目

2023-05-16

此文首发于我的Jekyll博客:zhang0peter的个人博客


LeetCode题解文章分类:LeetCode题解文章集合
LeetCode 所有题目总结:LeetCode 所有题目总结


题目地址:Best Time to Buy and Sell Stock with Cooldown - LeetCode


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 as many transactions as you like (ie, buy one and sell one share of the stock multiple times) with the following restrictions:

You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
After you sell your stock, you cannot buy stock on next day. (ie, cooldown 1 day)
Example:

Input: [1,2,3,0,2]
Output: 3 
Explanation: transactions = [buy, sell, cooldown, buy, sell]

这道题目的区别跟之前的买卖股票的题目区别在于卖了之后要停一天。

最容易想到的是贪心,但会发现贪心不能解决问题。

因为后面一个状态跟前面一个状态有关,应该用动态规划解决。

因为之前卖了需要冷却一天,所以需要另外一个变量来存

第n个状态应该与n-1和n-2个状态有关

下面先把解法列出来。

Java 解法如下:

class Solution {
    public int maxProfit(int[] prices) {
        if (prices == null || prices.length <= 1) {
            return 0;
        }
        /**
         there can be two types of profit we need to track
         sellProf[i] - profit earned by selling on ith day
         restProf[i] - profit earned by resting on ith day
         */
        int sellProf = 0;
        int restProf = 0;
        int lastProf = 0;
        for (int i = 1; i < prices.length; i++) {
            lastProf = sellProf;
            //the current sellProf is either by selling on ith day or by resting on ith day
            sellProf = Math.max(sellProf + prices[i] - prices[i - 1], restProf);
            restProf = Math.max(lastProf, restProf);
        }
        return Math.max(sellProf, restProf);
    }
}

下面以[1, 2, 3, 0, 2]为例:

初始值12302
sellProf01213
restProf00122
lastProf00121

这里有2个主要的变量,一个是sellProf,另一个是restProf.

sellProf变量意味着当前卖掉股票的总收入或者昨天休息的总收入。这里有个难点,那就是如果你昨天卖了股票,今天又卖股票,实际意味着前天卖的股票,今天卖了。

restProf变量意味着昨天卖掉股票,今天继续休息的总收入或者昨天休息的总收入。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

LeetCode 309. Best Time to Buy and Sell Stock with Cooldown--Java解法-卖股票系列题目 的相关文章

  • ViewBinding简单使用

    官方文档 xff1a https developer android google cn topic libraries view binding hl 61 zh cn java 在app module下的build gradle文件中
  • Android广播实现进程间通信,很简单

    应用A发送广播 xff1a span class token keyword public span span class token keyword class span span class token class name MainA
  • 下载JDK8 JVM源码

    性子急的可以直接看快速下载步骤 xff1a 目录 详细步骤快速下载步骤 详细步骤 打开openJDK官网 xff1a https openjdk org 找到左侧的Mercurial xff0c 点击进入新界面 选择jdk8 xff0c 点
  • Git查看分支的创建人

    开发小组人多的时候 xff0c 仓库里会有跟多分支 xff0c 需要看下某个分支具体是谁创建的 命令 xff1a git for each ref format 61 39 committerdate 09 authorname 09 re
  • kotlin的this关键字几种用法

    与java不同的是 xff0c 原先MainActivity this这种写法在kotlin中会报错 如下 正确的写法有许多 xff0c 直接就写this也可以识别到 xff0c 如下 xff1a span class token clas
  • kotlin中匿名内部类的写法

    原本java开发安卓常用的setOnClickListener xff0c 用kotlin写 xff0c 也变得五花八门了 span class token keyword var span view span class token op
  • Spring与SpringMVC的区别和联系是啥?

    Spring Spring是一个开源容器框架 xff0c 可以接管web层 xff0c 业务层 xff0c dao层 xff0c 持久层的组件 xff0c 并且可以配置各种bean 和维护bean与bean之间的关系 其核心就是控制反转 I
  • “在XML文件中给代码加注释”请注意注释的位置

    先科普一下eclipse加注释的快捷键 xff1a eclipse中编辑Java文件时 xff0c 注释和取消注释的快捷键都是 xff1a 34 CTRL 43 34 编辑xml文件时 xff0c 注释 xff1a CTRL 43 SHIF
  • HTTP代理服务器的实现

    接下来都是我对HTTP代理服务器的理解 HTTP代理服务 xff08 proxy server xff09 器就是客户端也是服务端 xff0c 是一个事务处理的中间人 xff0c 就像下图所展示的一样 xff0c 图片来源于 HTTP权威指
  • “无法识别的USB设备”如何解决

    昨天 xff0c 我把USB数据线插入笔记本电脑做真机调试 xff0c 电脑右下角提示显示 无法识别的USB设备 xff0c 我开始百度 xff08 还不会搭梯子用google xff09 xff0c 搜索结果大多说是要更新驱动 xff0c
  • 解决Android studio 模拟器闪烁黑屏问题

    首先 xff0c 必须感谢csdn大神给我的启示 xff0c 但是原文并没有解决我的问题 我在看 第一行代码 的时候 xff0c 跟着郭霖大神的思路 xff0c 想利用cmd命令查看虚拟机中的 db文件中的数据表 因为真机需要root才能查
  • Android studio如何更改应用程序的图标以及名称

    如何在Android studio中更改应用程序的图标和名称是很多初学者遇到的问题之一 xff0c 今天我就来给大家讲一下简单的步骤 1 更改图标 首先选中我们需要更改的工程 xff0c 然后new gt Image Asset 就来到了更
  • Matlab中矩阵的合并、某行某列的删除、矩阵大小的改变(完整的函数调用表)、矩阵元素的访问

    矩阵的合并 矩阵的合并就是把两个或两个以上的矩阵合并成一个新的矩阵 可用于构造矩阵 xff0c 也可用于合并矩阵 c 61 A B 就是在水平方向上合并矩阵A和矩阵B c 61 A B 就是在竖直方向上合并矩阵A和矩阵B 如下 xff1a
  • Matlab里for循环详解

    for循环用来重复指定次数 xff0c 由于for 循环变量 end组成 例1 xff1a span class token keyword for span i span class token operator 61 span span
  • 定时关机

    include 34 stdafx h 34 include lt windows h gt include lt windowsx h gt include lt shellapi h gt include 34 resource h 3
  • Android设置Settings:PreferenceFragment【4】

    Android设置Settings xff1a PreferenceFragment 4 最新的android谷歌官方设计文档指出 xff0c 在后续的Android开发中 xff0c 应尽量使用PreferenceFragment而不是P
  • Centos ansible部署,启动服务失败

    出现错误 xff1a Unable to enable service nginx Failed to execute operation Cannot send after transport endpoint shutdown解决方法
  • 抢红包算法--四种抢红包算法对比(附源码)

    还记着longlong ago 我还在做绿色征途手游版的时候 有天 策划同学要求同事 一定要优化下抢红包算法 本着划水第一 吃瓜并列第一的原则 于是 我听到了一堆数学名词 定理 XXX公式 呼 是我不配了 怪我没有好好学习 再仔细听一听 问
  • 抢红包算法--四种抢红包算法对比

    线上测试服务器中 xff0c 有个同事做的抢红包算法被要求优化 xff0c 大概听了下他们的讨论 xff0c 最后的结果竟然要用什么概率论等等一系列我听过的 没听过的名词去解决 我表示一脸懵 其实解决的问题就是一个 xff1a 抢红包算法不
  • ubuntu 18.04 安装pycharm社区免费版

    参考教程 https blog csdn net qq 15192373 article details 81091278 1 打开官网的下载页面 https www jetbrains com pycharm download secti

随机推荐