best-time-to-buy-and-sell-stock系列(貪心與動態規劃)

一、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 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.


解題思路:這個問題有很多種解法,最簡單的做法是貪心。

    解法1: 首先設第 i 個元素之前最小的股票價格爲minprice,初始化爲 price[0],最大利潤maxprofit 初始化爲0; 然後遍歷整個price數組,maxprofit = price[i] - minprice ,然後更新minprice = min( minprice, price[i] ) ;

    解法2: 爲了引出後面的解法,這裏介紹一種畫蛇添足的方法: 初始化兩個一維數組 local[N],global[N](全0),分別表示前i天最多進行一次交易的局部最優和全局最優。這裏的局部最優指的是,在第i天賣出股票所得到的最大收入,全局最優指的是在第i天或第i天之前賣出股票所得的最大收入。

    設天數編號爲1..N,從 i == 2 開始遍歷數組,local[i] = max(  local[i-1] + price[i]-price[i-1] , price[i] - price[i-1] ) ; global[i] = max(local[i], global [i -1] ); 最終結果存放在global[N] ;


//解法2:
class Solution {
public:
    int maxProfit(vector<int> &prices) {
        int size = prices.size();
        int *local = new int[ size ];
        int *global = new int[ size ];
        for( int i = 0 ;  i< size ; i++){
            local[i]=global[i] = 0;
        }
        for( int i = 1 ;  i< size ; i++){
            local[i] = max(local[i-1] + prices[i] - prices[i-1],
                           prices[i] - prices[i-1]);
            global[i] = max( global[i-1], local[i]);
        }
        return global[size-1];
    }
};
二、best-time-to-buy-and-sell-stock-ii

解題思路:

    解法一:還是簡單的貪心,只需判斷price[i] - price[i-1] > 0 即可,將此差值加入最終盈利中

    解法二:初始化兩個二維數組 local[N][M],global[N][M](全0),local[i][j],和global[i][j] 分別表示前i天最多進行j次交易的局部最優和全局最優。這裏的局部最優指的是,在第i天賣出股票所得到的最大收入,全局最優指的是在第i天或第i天之前賣出股票所得的最大收入。

    設天數編號爲1..N,從 i == 2 開始遍歷數組,local[i][j] = max(  local[i-1][j] + price[i]-price[i-1] , global[i-1][j-1] + price[i] - price[i-1] ) ; global[i][j] = max(local[i][j], global [i][j-1] ); 最終結果存放在global[N][M] ;

//解法二
class Solution {
public:
    int maxProfit(vector<int> &prices) {
        int size = prices.size();
        if( size <= 1 )
            return 0;
        int M = size / 2;
        vector<vector<int> > local ( size , vector<int>( M +1 , 0 ) );
        vector<vector<int> > global( size, vector<int>( M+1 , 0 ) );
        
        for( int i = 1 ;  i< size ; i++){
            for( int j = 1; j <= M; j++){
              	local[i][j] = max(local[i-1][j] + prices[i] - prices[i-1],
                           global[i-1][j-1] + prices[i] - prices[i-1]);
            	global[i][j] = max( global[i-1][j], local[i][j]);  
            }
        }
        return global[size-1][M];
    }
};

三、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 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).


解題思路:
    解法一:可以將prices序列劃分爲兩個子序列,由一中的方法分別求解
    解法二:將二中解法二的次數由M次改爲2次;
class Solution {
public:
    int maxProfit(vector<int> &prices) {
        int size = prices.size();
        if( size <= 1 )
            return 0;
        int M = 2;
        vector<vector<int> > local ( size , vector<int>( M +1 , 0 ) );
        vector<vector<int> > global( size, vector<int>( M+1 , 0 ) );
        
        for( int i = 1 ;  i< size ; i++){
            for( int j = 1; j <= M; j++){
              	local[i][j] = max(local[i-1][j] + prices[i] - prices[i-1],
                           global[i-1][j-1] + prices[i] - prices[i-1]);
            	global[i][j] = max( global[i-1][j], local[i][j]);  
            }
        }
        return global[size-1][M];
    }
};


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).


解題思路:
    將二中解法二的次數由M次改爲k次;
class Solution {
public:
    int maxProfit(vector<int> &prices,int k ) {
        int size = prices.size();
        if( size <= 1 )
            return 0;
        int M = k;
        vector<vector<int> > local ( size , vector<int>( M +1 , 0 ) );
        vector<vector<int> > global( size, vector<int>( M+1 , 0 ) );
        
        for( int i = 1 ;  i< size ; i++){
            for( int j = 1; j <= M; j++){
              	local[i][j] = max(local[i-1][j] + prices[i] - prices[i-1],
                           global[i-1][j-1] + prices[i] - prices[i-1]);
            	global[i][j] = max( global[i-1][j], local[i][j]);  
            }
        }
        return global[size-1][M];
    }
};


發佈了45 篇原創文章 · 獲贊 12 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章