class Solution {
public:
int maxProfit(vector<int> &prices) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if (a.size() == 0)
return 0;
vector<int> v1(a.size());
vector<int> v2(a.size());
int minIdx = 0;
v1[0] = 0;
for (int i = 1; i < a.size(); ++i)
{
if (a[i] < a[minIdx])
minIdx = i;
v1[i] = max(v1[i - 1], a[i] - a[minIdx]);
}
int maxIdx = a[a.size() - 1];
v2[a.size() - 1] = 0;
for (int i = a.size() - 2; i >= 0; --i)
{
if (a[i] > a[maxIdx])
maxIdx = i;
v2[i] = max(v2[i + 1], a[maxIdx] - a[i]);
}
int res = 0;
for (int i = 0; i < a.size(); ++i)
{
res = max(res, v1[i] + v2[i]);
}
return res;
}
};
09-03
09-03