# Leetcode 123 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.

## 解题思路

max=max<formmer[i]+latter[i]?fommer[i]+latter[i]:max$max = max < formmer[i] + latter[i] ? fommer[i] + latter[i] : max$

ps: 2015年小米春季实习生招聘在线笔试第二题就是这个题，不过当时我没刷这个题，但是还是做出来啦～

2015/8/12更新

## 算法描述

1. 遍历，计算出第x天之前一次交易可以得到的最大收益，存储在formmer[n]中
2. 遍历，计算出第x天之后一次交易可以得到的最大收益，存储在latter[n]中
3. 遍历，计算formmer[i] + latter[i] 得到最大收益max

## 代码 C

/**
* 计算最大收益
* @param prices : 每日价格
* @param pricesDize : 数据规模
* @return : 最大收益
* @note : 至多进行两次买卖
*/
int maxProfit(int* prices, int pricesSize) {
/* 如果价格数据为空或只有一个数据，返回0  */
if (pricesSize <= 1) return 0;

int *maxProfitDuringFormmerDays = (int *)malloc(sizeof(int) * pricesSize),
*maxProfitDuringLatterDays = (int *)malloc(sizeof(int) * pricesSize);

int day, diff, minPrice, maxPrice, maxProfit;

/* 计算某一天极其之前所有时间内的最大利益  */
minPrice = *prices;
maxProfit = 0;
*maxProfitDuringFormmerDays = 0;
for (day = 1; day < pricesSize; ++day) {
diff = *(prices + day) - minPrice;
if (diff < 0) minPrice = *(prices + day);
else if (diff > maxProfit) maxProfit = diff;
*(maxProfitDuringFormmerDays + day) = maxProfit;
}

/* 计算某一天极其之后所有时间内价格的最利益  */
maxPrice = *(prices + pricesSize - 1);
maxProfit = 0;
*(maxProfitDuringLatterDays + pricesSize - 1) = 0;
for (day = pricesSize - 2; day > -1; --day) {
diff = maxPrice - *(prices + day);
if (diff < 0) maxPrice = *(prices + day);
else if (diff > maxProfit) maxProfit = diff;
*(maxProfitDuringLatterDays + day) = maxProfit;
}

/* 计算最大收益 */
int sum;
maxProfit = 0;
for (day = 0; day < pricesSize; ++day) {
sum = *(maxProfitDuringFormmerDays + day) + *(maxProfitDuringLatterDays + day);
if (sum > maxProfit) maxProfit = sum;
}

free(maxProfitDuringFormmerDays);
free(maxProfitDuringLatterDays);

return maxProfit;
}

## 代码 CPP

class Solution {
public:
/**
* 计算最大收益
* @param prices : 每日价格
* @return : 最大收益
* @note : 至多进行两次买卖
*/
int maxProfit(vector<int>& prices) {
int pricesSize = prices.size();

/* 如果价格数据为空或只有一个数据，返回0  */
if (pricesSize <= 1) return 0;

vector<int> maxProfitDuringFormmerDays(pricesSize),
maxProfitDuringLatterDays(pricesSize);

int day, diff, minPrice, maxPrice, maxProfit;

/* 计算某一天极其之前所有时间内的最大利益  */
minPrice = prices[0];
maxProfit = 0;
maxProfitDuringFormmerDays[0] = 0;
for (day = 1; day < pricesSize; ++day) {
diff = prices[day] - minPrice;
if (diff < 0) minPrice = prices[day];
else if (diff > maxProfit) maxProfit = diff;
maxProfitDuringFormmerDays[day] = maxProfit;
}

/* 计算某一天极其之后所有时间内价格的最利益  */
maxPrice = prices[pricesSize - 1];
maxProfit = 0;
maxProfitDuringLatterDays[pricesSize - 1] = 0;
for (day = pricesSize - 2; day > -1; --day) {
diff = maxPrice - prices[day];
if (diff < 0) maxPrice = prices[day];
else if (diff > maxProfit) maxProfit = diff;
maxProfitDuringLatterDays[day] = maxProfit;
}

/* 计算最大收益 */
int sum;
maxProfit = 0;
for (day = 0; day < pricesSize; ++day) {
sum = maxProfitDuringFormmerDays[day] + maxProfitDuringLatterDays[day];
if (sum > maxProfit) maxProfit = sum;
}

return maxProfit;
}

};

## 运行情况

Lauguage Status Time
c Accept 4ms
cpp Accept 12ms

2015/7/8

#### LeetCode 123.买卖股票的最佳时机 III

2018-05-03 16:33:01

#### 【LeetCode】 Best Time to Buy and Sell Stock I II III IV 解题报告

2015-04-06 11:10:34

#### LeetCode123:Best Time to Buy and Sell Stock III

2015-06-16 09:54:29

#### leetcode121、122、123

2017-02-22 19:20:30

#### leetcode123 Best Time to Buy and Sell Stock III

2015-11-06 21:43:37

#### leetcode题解-123. Best Time to Buy and Sell Stock III

2017-06-15 09:52:42

#### 【leetcode】123. Best Time to Buy and Sell Stock III

2015-07-22 19:09:42

#### leetCode练习（123）

2016-11-04 10:07:12

#### LeetCode(123) Best Time to Buy and Sell Stock III (Java)

2015-04-19 13:20:26

#### Best Time to Buy and Sell Stock III -- LeetCode

2014-04-09 05:34:05