惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
Schneier on Security
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
T
Threat Research - Cisco Blogs
C
Cyber Attacks, Cyber Crime and Cyber Security
C
CXSECURITY Database RSS Feed - CXSecurity.com
A
Arctic Wolf
Security Latest
Security Latest
Simon Willison's Weblog
Simon Willison's Weblog
I
Intezer
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
T
Troy Hunt's Blog
Latest news
Latest news
Help Net Security
Help Net Security
S
Security Affairs
Webroot Blog
Webroot Blog
The Hacker News
The Hacker News
AI
AI
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
T
Tor Project blog
Forbes - Security
Forbes - Security
Google DeepMind News
Google DeepMind News
AWS News Blog
AWS News Blog
Attack and Defense Labs
Attack and Defense Labs
P
Proofpoint News Feed
www.infosecurity-magazine.com
www.infosecurity-magazine.com
H
Help Net Security
L
Lohrmann on Cybersecurity
S
SegmentFault 最新的问题
Google Online Security Blog
Google Online Security Blog
MongoDB | Blog
MongoDB | Blog
Cyberwarzone
Cyberwarzone
The Last Watchdog
The Last Watchdog
S
Securelist
N
News and Events Feed by Topic
S
Secure Thoughts
F
Fortinet All Blogs
博客园_首页
C
Cybersecurity and Infrastructure Security Agency CISA
量子位
M
MIT News - Artificial intelligence
F
Full Disclosure
T
The Blog of Author Tim Ferriss
T
Tailwind CSS Blog
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Microsoft Security Blog
Microsoft Security Blog
I
InfoQ
P
Privacy International News Feed
L
LangChain Blog
Know Your Adversary
Know Your Adversary
C
CERT Recently Published Vulnerability Notes

博客园 - 谢绝围观

LeetCode 910. Smallest Range II EAC 抓取CD为AAC文件 Binary Indexed Tree (Fenwick Tree) Lint Code 1365. Minimum Cycle Section LintCode 896. Prime Product 简明题解 UVa 1471 - Defense Lines Windows下安装配置MinGW GCC调试环境 详解LeetCode 137. Single Number II LeetCode 84. Largest Rectangle in Histogram 修改注册表删除Windows资源管理器 “通过QQ发送” 右键菜单项 LeetCode 312. Burst Balloons LeetCode 287. Find the Duplicate Number LeetCode 10. Regular Expression Matching LeetCode 117. Populating Next Right Pointers in Each Node II 在Windows Azure VM下搭建SSTP VPN - 谢绝围观 利用.NET Code Contracts实现运行时验证 Log4net 配置实例 解决Windows Server 2012 下利用RRAS创建VPN断线的问题 - 谢绝围观 慎用静态类static class
LeetCode 309. Best Time to Buy and Sell Stock with Cooldown
谢绝围观 · 2017-03-04 · via 博客园 - 谢绝围观

题目意思大概是给出连续n天的股价,作出如下限制:

  1. 只能一次性买入卖出,不能分批交易。分多次买入再卖出或者买入一次然后分多次卖出都是不允许的。
  2. 卖出以后有一天的时间不能交易 (one day cooldown time)。

交易次数不限。求可以获得的最大利润。

根据题意,画出DFA如下

每天可以采取的动作有3种:

  1. 什么也不干
  2. 买入
  3. 卖出

初始状态为idle,手头没有任何股票,且可以进行买入交易。

如果当天或之前有买入动作,则状态为hold,持有股票。

如果当天有卖出动作,则状态变为sold。手头不再持有股票,但也不能买入。

至此,我们可以分别计算3种状态下,采取不同的行动以后各个状态的最大利润。

hold:

前一天为idle,当天买入,或者之前有买入动作,当天什么也不做:

hold = max(idle - prices[i], hold);

idle:

idle可以由之前的idle或sold状态变化而来:

sold:

当天卖出,由hold状态变化而来,注意,当天买入当天卖出是允许的。

最终代码如下:

class Solution {
public:
    int maxProfit(vector<int>& prices) {
        if(prices.empty() || prices.size() <= 1){
            return 0;
        }
        
        int idle = 0, hold = -prices[0], sold = 0;
        for(int i = 1; i < prices.size(); i++){
            hold = max(idle - prices[i], hold);
            idle = max(idle, sold);
            sold = hold + prices[i];
        }
        
        return max(sold, idle);
    }
};