Leetcode 123 Best Time to Buy and Sell Stock III 买卖股票的最佳时机 蛮好的

这道题比较有意思,限制了购买次数2次,嗯,蛮好的。

题目如下:

给定一个数组,它的第 i 个元素是一支给定的股票在第 天的价格。

设计一个算法来计算你所能获取的最大利润。你最多可以完成 两笔 交易。

注意: 你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。

示例 1:

输入: [3,3,5,0,0,3,1,4]
输出: 6
解释: 在第 4 天(股票价格 = 0)的时候买入,在第 6 天(股票价格 = 3)的时候卖出,这笔交易所能获得利润 = 3-0 = 3 。
     随后,在第 7 天(股票价格 = 1)的时候买入,在第 8 天 (股票价格 = 4)的时候卖出,这笔交易所能获得利润 = 4-1 = 3 。

哈哈,我的笨方法:

package test;

public class LC123Try1
{
	
	//运行时间比较慢
	public int maxProfit(int[] prices)
	{
		int ret = 0;
		int len = prices.length;
		if (len <= 1)
		{
			return ret;
		}
		int[] dp = new int[len + 1];//用dp记录到第i个元素,购买一次的最大利益,我就是没想到,在记录下第二次的,笨
		for (int i = 1; i < len; i++)
		{
			int max = 0;
			for (int j = 0; j < i; j++)
			{
				if (prices[i] - prices[j] > max)
				{
					max = prices[i] - prices[j];
				}
				if (prices[i] - prices[j] > 0)
				{
					int t = prices[i] - prices[j];
					if ((t + dp[j]) > ret)
					{
						ret = t + dp[j];
					}
				}
			}
			dp[i + 1] = Math.max(dp[i], max);
		}
		return ret;

	}
	public static void main(String[] args)
	{
		LC123Try1 t = new LC123Try1();
		int[] prices = {7,6,4,3,1};
		System.out.println(t.maxProfit(prices));
	}

}

哈哈,接下来,我就在想,我的时间咋比较长,于是就想了下面的错误思路:

当出现一个峰值就把这个峰值当做一个卖出点,认为算作一个交易,找出了两个最大的交易,最后结果却是错误的。

例如:1,2,4,2,5,7,2,4,9

这个例子,如果峰值算出一个卖出点,则1,2,4算作一个交易,转利润3.

2,5,7算作一次交易,转利润5.

2,4,9算作一次交易,转利润7。

找出利润最高的两次交易是5+7=12。

但是没有想到1,2,4,2,5,7算作一次交易,转利润6.

2,4,9算作一次交易,转利润7,则两次交易转6+7=13.

我的思路在这里就算错了。

错误代码如下:

package test;

//wrong 错误的结果
public class LC123Try2
{
	public int maxProfit(int[] prices)
	{
		int ret = 0;
		int len = prices.length;
		if (len <= 1)
		{
			return ret;
		}
		int f = 0;//first
		int s = 0;//second
		int t = 0;//total
		for (int i = 0; i < len - 1; i++)
		{
			if (prices[i + 1] > prices[i])
			{
				t += prices[i + 1] - prices[i];
			}
			else
			{
				if (t >= f)
				{
					s = f;
					f = t;
				}
				else
				{
					if (t > s)
					{
						s = t;
					}
				}
				t = 0;
			}
		}
		if (t >= f)
		{
			s = f;
			f = t;
		}
		else
		{
			if (t > s)
			{
				s = t;
			}
		}
		ret = s + f;

		return ret;
	}

	public static void main(String[] args)
	{
		LC123Try2 t = new LC123Try2();
		int[] prices = { 1,2,3,4,5 };
		System.out.println(t.maxProfit(prices));
	}

}

简单的,两次的,也可以说分儿治之的:

package test;

public class LC123Try3
{
	public int maxProfit(int[] prices)
	{
		int ret = 0;
		int len = prices.length;
		if(len<=1){
			return ret;
		}
		int[] f = new int[len];//first f[i]:从第一个到第i个,的最大利益
		int[] s = new int[len];//second s[i]:从最后一个到第i个最大利益值
		int min = prices[0];
		for (int i = 1; i < len; i++)
		{
			min = Math.min(min, prices[i]);
			f[i] = Math.max(f[i - 1], prices[i] - min);
		}
		int max = prices[len - 1];
		for (int j = len - 2; j >= 0; j--)
		{
			max = Math.max(max, prices[j]);
			s[j] = Math.max(s[j + 1], max - prices[j]);
		}
		ret=Math.max(f[len-1], s[0]);
		for(int k=0;k<len-1;k++){
			ret=Math.max(ret, f[k]+s[k+1]);//从(0-k)加上从(k+1,到len-1)
		}
		
		return ret;
	}
	public static void main(String[] args)
	{
		LC123Try3 t = new LC123Try3();
		int[] prices = { 1,2,4,2,5,7,2,4,9 };
		System.out.println(t.maxProfit(prices));
	}

}
贪心:算法最容易的是想错思路,哈哈

猜你喜欢

转载自blog.csdn.net/ata_123/article/details/81002673