# 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

• 本文已收录于以下专栏：

## LeetCode123: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 a...

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

Best Time to Buy and Sell Stock I题意：用一个数组表示股票每天的价格，数组的第i个数表示股票在第i天的价格。 如果只允许进行一次交易，也就是说只允许买一支股票并卖掉，求...

## 【LeetCode】Best Time to Buy and Sell Stock I && II && III

1、Best Time to Buy and Sell Stock  Total Accepted: 7973 Total Submissions: 25534 My Submissions Say ...

## [LeetCode]Buy and Sell Stocks 买卖股票问题

LeetCode上关于买卖股票的问题一共有五道，题号分别为121，122，123，188，309。 此类问题的基本描述即给出一个序列prices[]，prices[i]代表第i天股票的价格。 如果当天...

## leetCode练习（123）

举报原因： 您举报文章：深度学习：神经网络中的前向传播和反向传播算法推导 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)