# int32bit / leetcode

Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
..
Failed to load latest commit information.
TLE_solve.cpp
in.txt
solve.cpp

## 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).

## Solution

```int maxProfit(vector<int>& prices) {
int n = prices.size();
if (n <= 1)
return 0;
int profit = 0;
for (int i = 0; i < prices.size(); ++i) {
int p = maxProfit(prices, 0, i) + maxProfit(prices, i, n - 1);
profit = max(profit, p);
}
return profit;
}
int maxProfit(vector<int>& a, int s, int t) {
if (s >= t)
return 0;
int profit = 0;
int minPrice = a[s];
for (int i = s + 1; i <= t; ++i) {
minPrice = min(minPrice, a[i]);
profit = max(profit, a[i] - minPrice);
}
return profit;
}```

```int maxProfit(int *a, int n)
{
int min = a[0];
int max = 0;
for (int i = 1; i < n; ++i) {
min = MIN(min, a[i]);
max = MAX(max, a[i] - min);
}
return max;
}```

```int maxProfit(int *a, int n)
{
int max = a[n - 1];
int profit = 0;
for (int i = n - 2; i >= 0; --i) {
max = MAX(max, a[i]);
profit = MAX(profit, max - a[i]);
}
return profit;
}```

```leftMin[0] = prices[0];
for (int i = 1; i < n; ++i) {
leftMin[i] = min(leftMin[i - 1], prices[i]);
}```

```rightMax[0] = prices[n - 1];
for (int i = n - 2, k = 1; i >=0; --i, k++)
rightMax[k] = max(rightMax[k - 1], prices[i]);```

```left = max(left, prices[i] - leftMin[i]);
right = rightMax[n - i - 1] - prices[i];```

`left = max(left, prices[i] - leftMin[i])`保证了左边一定是最优解。但右边没有使用max，即右边不是最优解，因为当i向后移动时，可能 会消解掉局部最优解，如`0 9 1 6`, 当i = 0，时最优解是9，当i等于1时，此时子数组为`9 1 6`, 最大值为5.

```int maxProfit(vector<int>& prices) {
int n = prices.size();
if (n <= 1)
return 0;
vector<int> leftMin(n, 0);
leftMin[0] = prices[0];
for (int i = 1; i < n; ++i) {
leftMin[i] = min(leftMin[i - 1], prices[i]);
}
vector<int> rightMax(n, 0);
rightMax[0] = prices[n - 1];
for (int i = n - 2, k = 1; i >=0; --i, k++)
rightMax[k] = max(rightMax[k - 1], prices[i]);
int left = 0, right = 0;
int profit = 0;
for (int i = 0; i < n; ++i) {
left = max(left, prices[i] - leftMin[i]);
right = rightMax[n - i - 1] - prices[i];
profit = max(profit, left + right);
}
return profit;
}```

## 扩展

You can’t perform that action at this time.