LeetCode Best Time to Buy and Sell Stock with Cooldown
原题链接在这里:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/
题目:
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:
prices = [1, 2, 3, 0, 2]
maxProfit = 3
transactions = [buy, sell, cooldown, buy, sell]
题解:
Let buy[i] denotes maximum profit till index i, series of transactions ending with buy.
Let sell[i] denotes maimum profit till index i, series of transactions ending with sell.
There is a cooldown before buy again. buy[i] could happend after sell[i-2], not sell[i-1].
Thus, buy[i] = Math.max(buy[i-1], sell[i-2]-prices[i]).
If wanna sell a stock, must buy stock before. Thus sell[i] is calculated with buy[i-1].
sell[i] = Math.max(sell[i-1], buy[i-1]+prices[i]).
只用到了i-1, i-2两个之前的量,所以可以使用常量来代替数组.
b0 as buy[i], b1 as buy[i-1].
s0 as sell[i], s1 as sell[i-1], s2 as sell[i-2].
Time Complexity: O(n). n = prices.length.
Space: O(1).
AC Java:
class Solution {
public int maxProfit(int[] prices) {
if(prices == null || prices.length < 2){
return 0;
}
int b0 = -prices[0];
int b1 = b0;
int s2 = 0;
int s1 = 0;
int s0 = 0;
for(int i = 1; i<prices.length; i++){
b0 = Math.max(b1, s2-prices[i]);
s0 = Math.max(s1, b1+prices[i]);
b1 = b0;
s2 = s1;
s1 = s0;
}
return s0;
}
}
LeetCode Best Time to Buy and Sell Stock with Cooldown的更多相关文章
- [LeetCode] Best Time to Buy and Sell Stock with Cooldown 买股票的最佳时间含冷冻期
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- [LeetCode] Best Time to Buy and Sell Stock IV 买卖股票的最佳时间之四
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- [LeetCode] 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 al ...
- [LeetCode] Best Time to Buy and Sell Stock II 买股票的最佳时间之二
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- [LeetCode] Best Time to Buy and Sell Stock 买卖股票的最佳时间
Say you have an array for which the ith element is the price of a given stock on day i. If you were ...
- LeetCode:Best Time to Buy and Sell Stock I II III
LeetCode:Best Time to Buy and Sell Stock Say you have an array for which the ith element is the pric ...
- [LeetCode] Best Time to Buy and Sell Stock with Transaction Fee 买股票的最佳时间含交易费
Your are given an array of integers prices, for which the i-th element is the price of a given stock ...
- Leetcode之动态规划(DP)专题-309. 最佳买卖股票时机含冷冻期(Best Time to Buy and Sell Stock with Cooldown)
Leetcode之动态规划(DP)专题-309. 最佳买卖股票时机含冷冻期(Best Time to Buy and Sell Stock with Cooldown) 股票问题: 121. 买卖股票 ...
- [LeetCode] Best Time to Buy and Sell Stock 6道合集【DP】
1. Best Time to Buy and Sell Stock 2. Best Time to Buy and Sell Stock II 3. Best Time to Buy and Sel ...
随机推荐
- BZOJ4662 : Snow
首先离散化,即相邻关键点之间的部分可以压成一段. 注意到区间互不包含,因此排序后每个位置的清理影响到的是一段连续区间的清理工的工作长度. 这显然可以用线段树维护,支持区间减去一个数,单点加上$inf$ ...
- SCOI 2013 密码 & 乱搞
题意: Fish 是一条生活在海里的鱼.有一天他很无聊,就到处去寻宝.他找到了位于海底深处的宫殿,但是一扇带有密码锁的大门却阻止了他的前进.通过翻阅古籍,Fish 得知了这个密码的相关信息:1. 该密 ...
- 21045308刘昊阳 《Java程序设计》第九周学习总结
21045308刘昊阳 <Java程序设计>第九周学习总结 教材学习内容总结 第16章 整合数据库 16.1 JDBC入门 16.1.1 JDBC简介 数据库本身是个独立运行的应用程序 撰 ...
- 使用AFNetworking 2.0 请求数据时出现错误 Request failed: unacceptable content-type: text/html 解决方法
使用AFNetworking 2.0 请求数据时出现错误 Request failed: unacceptable content-type: text/html 解决方法 添加一行 manager. ...
- UIColletionView 的属性与常用方法介绍
UICollectionView基础 初始化部分: UICollectionViewFlowLayout *flowLayout= [[UICollectionViewFlowLayout all ...
- Codeforces Round #199 (Div. 2) E. Xenia and Tree
题目链接 2了,差点就A了...这题真心不难,开始想的就是暴力spfa就可以,直接来了一次询问,就来一次的那种,TLE了,想了想,存到栈里会更快,交又TLE了..无奈C又被cha了,我忙着看C去了.. ...
- Crystal Reports 2008(水晶报表) 安装
这篇blog主要是介绍Crystal Reports2008(水晶报表)的安装. 首先我们应该知道Crystal Reports 有什么作用? 从这里Crystal Reports 你可以了解到它的一 ...
- C#_使用SMTP发送邮件
using System.Net; using System.Net.Mail; namespace ClassLibrary { /// <summary> /// 发送邮件 /// & ...
- 通过串口设备vid,pid自动获得该设备所对应的串口号
用C#做串口通讯很方便,因为dotfx2.0已经集成了Serial Port控件,此控件使用上比MSComm控件更简单,当然它也有一个小bug (RecievedBytesThreshold设置有时候 ...
- java mail(发送邮件--163邮箱)
package com.util.mail; /** * 发送邮件需要使用的基本信息 */ import java.util.Properties; public class MailSenderIn ...