题目描述
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).
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).
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];
}
};