leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

0123.cpp (440B)


      1 class Solution {
      2   public:
      3     int maxProfit(const vector<int> &prices) const {
      4         int cost1 = INT_MAX, cost2 = INT_MAX;
      5         int profit1 = 0, profit2 = 0;
      6 
      7         for (const int price : prices) {
      8             cost1 = min(cost1, price);
      9             profit1 = max(profit1, price - cost1);
     10             cost2 = min(cost2, price - profit1);
     11             profit2 = max(profit2, price - cost2);
     12         }
     13 
     14         return profit2;
     15     }
     16 };