best-time-to-buy-and-sell-stock系列(贪心与动态规划)

4 篇文章 0 订阅
2 篇文章 0 订阅
一、 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];
    }
};


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值