leetcodeSolution to some Leetcode problems written in C++ |
git clone git://git.dimitrijedobrota.com/leetcode.git |
Log | Files | Refs | README | LICENSE |
0123.cpp (440B)
0 class Solution {
1 public:
2 int maxProfit(const vector<int> &prices) const {
3 int cost1 = INT_MAX, cost2 = INT_MAX;
4 int profit1 = 0, profit2 = 0;
6 for (const int price : prices) {
7 cost1 = min(cost1, price);
8 profit1 = max(profit1, price - cost1);
9 cost2 = min(cost2, price - profit1);
10 profit2 = max(profit2, price - cost2);
11 }
13 return profit2;
14 }
15 };