LeetCode:Best Time to Buy and Sell Stock 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.
分析:题目的意思是整个过程中只能买一只股票然后卖出,也可以不买股票。也就是我们要找到一对最低价和最高价,最低价在最高价前面,以最低价买入股票,以最低价卖出股票。
public int maxProfit(int[] prices) {
int best_diff = 0;
int best_buy = Integer.MAX_VALUE;
for(int i=0;i<prices.length;i++){
best_diff = Math.max(best_diff, prices[i]-best_buy);
best_buy = Math.min(best_buy, prices[i]);
}
return best_diff;
}
LeetCode:Best Time to Buy and Sell Stock 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) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
int len = prices.size();
if(len <= 1)return 0;
int i = 0;
int res = 0;
while(i < len - 1)
{
int buy, sell;
//递减区间
while(i+1 < len && prices[i+1] < prices[i])i++;
buy = i++;
//递增区间
while(i < len && prices[i] >= prices[i-1])i++;
sell = i-1;
res += prices[sell] - prices[buy];
}
return res;
}
};
分析二:把数组中所有差价为正的值加起来就是最大利润了。因为只有递增区间内的差价是正数,并且同一递增区间内所有差价之和 = 区间最大价格 - 区间最小价格
class Solution {
public:
int maxProfit(vector<int> &prices) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
int len = prices.size();
if(len <= 1)return 0;
int res = 0;
for(int i = 0; i < len-1; i++)
if(prices[i+1]-prices[i] > 0)
res += prices[i+1] - prices[i];
return res;
}
};
LeetCode: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).
分析:这一题约束最多只能买卖两次股票,并且手上最多也只能持有一支股票。因为不能连续买入两次股票,所以买卖两次肯定分布在前后两个不同的区间。设p(i) = 区间[0,1,2…i]的最大利润 + 区间[i,i+1,…n-1]的最大利润(式子中两个区间内分别只能有一次买卖,这就是第一道题的问题),那么本题的最大利润 = max{p[0],p[1],p[2],…,p[n-1]}。根据第一题的算法2,我们可以求区间[0,1,2…i]的最大利润;同理可以从后往前扫描数组求区间[i,i+1,…n-1]的最大利润,其递归式如下:
dp[i-1] = max{dp[i], maxprices - prices[i-1]} ,maxprices是区间[i,i+1,…,n-1]内的最高价格。 本文地址
因此两趟扫描数组就可以解决这个问题,代码如下:
class Solution {
public:
int maxProfit(vector<int> &prices) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
const int len = prices.size();
if(len <= 1)return 0;
int maxFromHead[len];
maxFromHead[0] = 0;
int minprice = prices[0], maxprofit = 0;
for(int i = 1; i < len; i++)
{
minprice = min(prices[i-1], minprice);
if(maxprofit < prices[i] - minprice)
maxprofit = prices[i] - minprice;
maxFromHead[i] = maxprofit;
}
int maxprice = prices[len - 1];
int res = maxFromHead[len-1];
maxprofit = 0;
for(int i = len-2; i >=0; i--)
{
maxprice = max(maxprice, prices[i+1]);
if(maxprofit < maxprice - prices[i])
maxprofit = maxprice - prices[i];
if(res < maxFromHead[i] + maxprofit)
res = maxFromHead[i] + maxprofit;
}
return res;
}
};
参考博客园:http://www.cnblogs.com/TenosDoIt/p/3436457.html