309. Best Time to Buy and Sell Stock with Cooldown

本文介绍了一种股票交易算法,旨在通过多次买卖操作实现最大利润。该算法遵循特定限制条件,如冷却期规定等,并通过动态规划优化空间复杂度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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]

摘自:https://discuss.leetcode.com/topic/30431/easiest-java-solution-with-explanations/2


1. Define States

To represent the decision at index i:

  • buy[i]: Max profit till index i. The series of transaction is ending with a buy.
  • sell[i]: Max profit till index i. The series of transaction is ending with a sell.

To clarify:

  • Till index i, the buy / sell action must happen and must be the last action. It may not happen at index i. It may happen at i - 1, i - 2, ... 0.
  • In the end n - 1, return sell[n - 1]. Apparently we cannot finally end up with a buy. In that case, we would rather take a rest at n - 1.
  • For special case no transaction at all, classify it as sell[i], so that in the end, we can still return sell[n - 1]. Thanks @alex153 @kennethliaoke @anshu2.

2. Define Recursion

  • buy[i]: To make a decision whether to buy at i, we either take a rest, by just using the old decision at i - 1, or sell at/before i - 2, then buy at i, We cannot sell at i - 1, then buy at i, because of cooldown.
  • sell[i]: To make a decision whether to sell at i, we either take a rest, by just using the old decision at i - 1, or buy at/before i - 1, then sell at i.

A great explanation. I think the definitions of buy[i] and sell[i] can be refined to these:

  • buy[i] : Maximum profit which end with buying on day i or end
    with buying on a day before i and takes rest until the day i since then.

  • sell[i] : Maximum profit which end with selling on day i or end
    with selling on a day before i and takes rest until the day i since then.



So we get the following formula:

buy[i] = Math.max(buy[i - 1], sell[i - 2] - prices[i]);   
sell[i] = Math.max(sell[i - 1], buy[i - 1] + prices[i]);


3. Optimize to O(1) Space

DP solution only depending on i - 1 and i - 2 can be optimized using O(1) space.

  • Let b2, b1, b0 represent buy[i - 2], buy[i - 1], buy[i]
  • Let s2, s1, s0 represent sell[i - 2], sell[i - 1], sell[i]

Then arrays turn into Fibonacci like recursion:

b0 = Math.max(b1, s2 - prices[i]);
s0 = Math.max(s1, b1 + prices[i]);


4. Write Code in 5 Minutes

First we define the initial states at i = 0:

  • We can buy. The max profit at i = 0 ending with a buy is -prices[0].
  • We cannot sell. The max profit at i = 0 ending with a sell is 0.


Here is my solution. Hope it helps!

public int maxProfit(int[] prices) {
    if(prices == null || prices.length <= 1) return 0;
  
    int b0 = -prices[0], b1 = b0;
    int s0 = 0, s1 = 0, s2 = 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;
}

-------------------------------------------------------------------------------------

自己想的O(N^2)的方法。。。

对于某天来说,如果是休息,最大利润和前一天相同,否则的话要做决策,不卖的话跟前一天一样。卖的话依次看看i-1天,i-2天……的情况,

如果i-k天做了卖出操作,那么i-k+1天就要休息,最大利润就是今天价格减去[i-k+2 ,i-1]的最小值,如果i-k-1天做了卖出操作,最大利润就是今天价格减去[i-k+1 ,i-1]的最小值,如果i-k和i-k-1天都没有做卖出操作,最大利润是今天价格减去[i-k,i-1]的最小值。怎么知道哪一天是卖了的呢?另外开辟一个数组记录,dp的过程中如果第i天利润最大不是和i-1天相同,说明是卖出了的,标记这一天就行了。


public int maxProfit(int[] prices)
	{
		int len=prices.length;
		if(len<2)
			return 0;
		
		int[] dp=new int[len];
		int[] aux=new int[len];
		SegmentTree2_1 sgt=new SegmentTree2_1(len);
		sgt.build(1, len, 1, prices);
		
		
		dp[0]=0;
		dp[1]=prices[1]>prices[0]?prices[1]-prices[0]:0;
		
		for(int i=2;i<len;i++)
		{
			int max=dp[i-1];
			for(int j=i-2;j>=0;j--)
			{
				
				int k;
				if(aux[j]==1)
					k=j+2;
				else if(j-1>=0&&aux[j-1]==1)
					k=j+1;
				else {
					k=j;
				}
				
				int stepmin=(int) sgt.query(k+1, i, 1, len, 1);
			
				max=Math.max(max, dp[j]+prices[i]-stepmin);
			}
			if(max!=dp[i-1])
				aux[i]=1;
			
			dp[i]=max;
		}
		
		return dp[len-1];
	}
	
	
}


class SegmentTree2_1
{
	private long[] ele;
	int cnt=0;

	public SegmentTree2_1(int maxn)
	{
		ele = new long[maxn << 2];
	}



	public void build(int l, int r, int rt,int[] arr)
	{
		if(arr.length<1)
			return ;
		
		if (l == r)
		{
			ele[rt] = arr[cnt++];
			return;
		}
		
		int m = (l + r) >> 1;
		build(l, m, rt << 1,arr);
		build(m + 1, r, rt << 1 | 1,arr);
		PushUP(rt);
	}

	private void PushUP(int rt)
	{
		ele[rt] = Math.min(ele[rt << 1] , ele[rt << 1 | 1]);
	}


	public long query(int L, int R, int l, int r, int rt)
	{
		if (L <= l && r <= R)
			return ele[rt];
		int m = (l + r) >> 1;
		long ret = Integer.MAX_VALUE;
		if (L <= m)
			ret = Math.min(ret,query(L, R, l, m, rt << 1));
		if (R > m)
			ret = Math.min(ret,query(L, R, m + 1, r, rt << 1 | 1));
		return ret;
	}

}


内容概要:该PPT详细介绍了企业架构设计的方法论,涵盖业务架构、数据架构、应用架构和技术架构四大核心模块。首先分析了企业架构现状,包括业务、数据、应用和技术四大架构的内容和关系,明确了企业架构设计的重要性。接着,阐述了新版企业架构总体框架(CSG-EAF 2.0)的形成过程,强调其融合了传统架构设计(TOGAF)和领域驱动设计(DDD)的优势,以适应数字化转型需求。业务架构部分通过梳理企业级和专业级价值流,细化业务能力、流程和对象,确保业务战略的有效落地。数据架构部分则遵循五大原则,确保数据的准确、一致和高效使用。应用架构方面,提出了分层解耦和服务化的设计原则,以提高灵活性和响应速度。最后,技术架构部分围绕技术框架、组件、平台和部署节点进行了详细设计,确保技术架构的稳定性和扩展性。 适合人群:适用于具有一定企业架构设计经验的IT架构师、项目经理和业务分析师,特别是那些希望深入了解如何将企业架构设计与数字化转型相结合的专业人士。 使用场景及目标:①帮助企业和组织梳理业务流程,优化业务能力,实现战略目标;②指导数据管理和应用开发,确保数据的一致性和应用的高效性;③为技术选型和系统部署提供科学依据,确保技术架构的稳定性和扩展性。 阅读建议:此资源内容详尽,涵盖企业架构设计的各个方面。建议读者在学习过程中,结合实际案例进行理解和实践,重点关注各架构模块之间的关联和协同,以便更好地应用于实际工作中。
资 源 简 介 独立分量分析(Independent Component Analysis,简称ICA)是近二十年来逐渐发展起来的一种盲信号分离方法。它是一种统计方法,其目的是从由传感器收集到的混合信号中分离相互独立的源信号,使得这些分离出来的源信号之间尽可能独立。它在语音识别、电信和医学信号处理等信号处理方面有着广泛的应用,目前已成为盲信号处理,人工神经网络等研究领域中的一个研究热点。本文简要的阐述了ICA的发展、应用和现状,详细地论述了ICA的原理及实现过程,系统地介绍了目前几种主要ICA算法以及它们之间的内在联系, 详 情 说 明 独立分量分析(Independent Component Analysis,简称ICA)是近二十年来逐渐发展起来的一种盲信号分离方法。它是一种统计方法,其目的是从由传感器收集到的混合信号中分离相互独立的源信号,使得这些分离出来的源信号之间尽可能独立。它在语音识别、电信和医学信号处理等信号处理方面有着广泛的应用,目前已成为盲信号处理,人工神经网络等研究领域中的一个研究热点。 本文简要的阐述了ICA的发展、应用和现状,详细地论述了ICA的原理及实现过程,系统地介绍了目前几种主要ICA算法以及它们之间的内在联系,在此基础上重点分析了一种快速ICA实现算法一FastICA。物质的非线性荧光谱信号可以看成是由多个相互独立的源信号组合成的混合信号,而这些独立的源信号可以看成是光谱的特征信号。为了更好的了解光谱信号的特征,本文利用独立分量分析的思想和方法,提出了利用FastICA算法提取光谱信号的特征的方案,并进行了详细的仿真实验。 此外,我们还进行了进一步的研究,探索了其他可能的ICA应用领域,如音乐信号处理、图像处理以及金融数据分析等。通过在这些领域中的实验和应用,我们发现ICA在提取信号特征、降噪和信号分离等方面具有广泛的潜力和应用前景。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值