[LeetCODE] Best Time to Buy and Sell Stock I II III IV V

 I:

Say you have an array for which the ith element is the price of a given stock on day i.

If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.

题意:给你一只股票的每天的价格曲线,你能买卖一次,问最大利润。

解:对于第i天他能得到的最大利润max(prices[k])(k>i) - prices[i].

我们从后往前面扫一遍就好了,记录下最大值与最大利润,然后更新。

代码:

class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int len = prices.size();
        if(len==0)return 0;
        int ans = 0,maxv = prices[len-1];
        for(int i = len-2;i >= 0;i--){
        	if(maxv-prices[i]>ans)ans=maxv-prices[i];
        	if(prices[i]>maxv)maxv=prices[i];
	}
		return ans;
    }
};

II:

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). However, you may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).

题意:给你一支股票每天的价格,你想买卖几次都行,但是每次买入的时候必须先把手中的卖了。

解:能买无数次,那么我们只要后一天比前一天的价格高我们就在前一天买入后一天卖出即可。

代码:

class Solution {
public:
    int maxProfit(vector<int>& prices) {
	  	int sz = prices.size();
	  	if(sz <= 1) return 0;
		int ans = 0;
		for(int i = 1; i<sz; ++i){
			int t = prices[i] - prices[i-1];
			if(t>0)ans+=t;
		}        
		return ans;
	}
};


IV(强行先做第四题不知道会不会被打)

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 at most k transactions.

Note:
You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).

题意:给你一支股票的价格趋势,你最多可以买入卖出k次,问最大收益。

按照传统的动态规划,我们会这样想:

我们用dp[i][j]表示 第j天卖出,前j天共卖出i次的收益,那么dp[i][j] = max(dp[i][j-2]+numb[j]-numb[j-2],max(dp[i-1][k])(2<=k<=j-2)+numb[j]-numb[j-1])

即把第j天的买卖和前i次交易连起来,或者在前j-2天里面交易i-1次,然后单独交易第j-1和第j天的.

但是这样的话会有一个问题,如果有几天是连续上升的价格,比如5,6,7,8 这样我们会把5,6当成一次交易,7,8当成另外一次交易,而实际上这两次交易可以合并为一次。

所以我们需要对原来的那个价格数组稍稍做一下处理,时数组仅仅保留价格的极大值与极小值,这样就可以把重叠的交易给避免了。

然后我们再用一个pre[j]数组表示max(dp[i-1][k],)这样就不用每次都去找最大值了。

代码:

class Solution {
public:
    int maxProfit(int k, vector<int>& prices) {
    	const int inf = 0x3f3f3f3f;
        const int maxn = prices.size()+10;
        int n = prices.size();
        int cnt = 1;
        if(n == 0) return 0;
        int dp[maxn],numb[maxn],pre[maxn];
        memset(pre,0,sizeof(pre));
        memset(dp,0,sizeof(dp));
        int t = 1e8;
        numb[0] = prices[0] + 1;			 
        int begin = 0;
        while(begin<n){
        	if(prices[begin]>t)break;
        	t = prices[begin];
        	begin++;
		}				//找到第一个连续递减的最小值 
		numb[1] = prices[begin-1];
        for(int i = begin; i<n; i++){
        	t = prices[i];
        	if((t-numb[cnt])*(numb[cnt]-numb[cnt-1])<0)cnt++;	//判断极值 
			numb[cnt] = t;
		}	 
		int ans = 0;
		if(cnt/2<=k){	//允许交易的次数比实际可交易的次数还多,直接参考II贪心即可 
			for(int i = 2; i <= cnt; i += 2) ans+=(numb[i]-numb[i-1]);
		}		 
		else{
			int maxv;
			for(int i=1;i<=k;i++){
				maxv = -inf;	//因为要先用pre[j-2]数组,所以先把最大值保存一下,下次更新 
				for(int j=2*i;j<=cnt;j+=2){
					dp[j] = max(dp[j-2]+numb[j]-numb[j-2],pre[j-2]+numb[j]-numb[j-1]);
					pre[j-2] = maxv;
					if(dp[j]>maxv) maxv = dp[j];	
					if(maxv>ans) ans=maxv;
				}
			}
		}
		return ans;
    }
};

III

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 at most two transactions.

Note:
You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).

有了第四题,我们这题只要把k变成2就好了。

代码:

class Solution {
public:
    int maxProfit(vector<int>& prices) {
    	int k = 2;
    	const int inf = 0x3f3f3f3f;
        const int maxn = prices.size()+10;
        int n = prices.size();
        int cnt = 1;
        if(n == 0) return 0;
        int dp[maxn],numb[maxn],pre[maxn];
        memset(pre,0,sizeof(pre));
        memset(dp,0,sizeof(dp));
        int t = 1e8;
        numb[0] = prices[0] + 1;			 
        int begin = 0;
        while(begin<n){
        	if(prices[begin]>t)break;
        	t = prices[begin];
        	begin++;
		}				
		numb[1] = prices[begin-1];
        for(int i = begin; i<n; i++){
        	t = prices[i];
        	if((t-numb[cnt])*(numb[cnt]-numb[cnt-1])<0)cnt++;	
			numb[cnt] = t;
		}	 
		int ans = 0;
		if(cnt/2<=k){	
			for(int i = 2; i <= cnt; i += 2) ans+=(numb[i]-numb[i-1]);
		}		 
		else{
			int maxv;
			for(int i=1;i<=k;i++){
				maxv = -inf;	
				for(int j=2*i;j<=cnt;j+=2){
					dp[j] = max(dp[j-2]+numb[j]-numb[j-2],pre[j-2]+numb[j]-numb[j-1]);
					pre[j-2] = maxv;
					if(dp[j]>maxv) maxv = dp[j];	
					if(maxv>ans) ans=maxv;
				}
			}
		}
		return ans;
    }
};





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值