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));
	}

}

 

超时的答案:

class Solution {
    public int maxProfit(int[] prices) {
        int maxret=0;
            int len=prices.length;
            int[] firstsell=new int[len];
            firstsell[0]=0;
            int minval=prices[0];
            //卖出一次
            for(int i=1;i<len;i++){                
                firstsell[i]=Math.max(prices[i]-minval,firstsell[i-1]);
                if(minval>prices[i]){
                    minval=prices[i];
                }
                if(firstsell[i]>maxret){
                    maxret=firstsell[i];                    
                }
            }
            //卖出两次
            for(int i=3;i<len;i++){
                for(int j=1;j<i-1;j++){
                    if((firstsell[j]+prices[i]-prices[j+1])>maxret) {
                        maxret=firstsell[j]+prices[i]-prices[j+1];
                    }
                }
                
            }
            
            return maxret;
    }
}

本质是动态规划:

这一天可以进行的动作:不卖、第一次买、第一次卖、第二次买、第二次卖

class Solution {
    public int maxProfit(int[] prices) {
         if(prices.length==1){
            return 0;
        }
        int buy1=prices[0];//buy就是成本 第一次买入
        int sell1=0;//第一次卖出
        int buy2=prices[0];
        int sell2=0;//sell 就是收益
        int len=prices.length;
        for(int i=1;i<len;i++){
            buy1=Math.min(buy1,prices[i]);
            sell1=Math.max(sell1,prices[i]-buy1);
            buy2=Math.min(buy2,prices[i]-sell1);//第二次卖出的成本
            sell2=Math.max(sell2,prices[i]-buy2);
        }
        return sell2;
    }
}

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值