-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path123.cpp
More file actions
24 lines (23 loc) · 748 Bytes
/
123.cpp
File metadata and controls
24 lines (23 loc) · 748 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <vector>
#include <algorithm>
using std::vector;
using namespace std;
class Problem123{
int maxProfit (vector<int>& prices){
vector<int> answer;
int highest = INT_MAX, int lowest = INT_MIN;
int profit1 = 0, int profit2 = 0;
for (int i = 0; i < prices.size(); ++i){
lowest = min(lowest, prices[i]);
profit1 = max(profit1, prices[i] - lowest);
answer.push_back(profit1);
}
for (int j = prices.size()-1; j >= 0; j--){
highest = max(highest, prices[j]);
profit2 = max(profit2, highest - prices[j]);
answer[j] += profit2;
}
return *max_element(answer.begin(), answer.end());
}
};