8ms simple C++ solution


  • 0
    Y
    class Solution {
    public:
        int maxProfit(vector<int>& prices) {
            int buy_one(INT_MIN),sell_one(0),buy_two(INT_MIN),sell_two(0);
            for(const int price : prices){
                buy_one = std::max(buy_one,0-price);
                sell_one = std::max(sell_one,price + buy_one);
                buy_two = std::max(buy_two,-price + sell_one);
                sell_two = std::max(sell_two,price + buy_two);
            }
            return std::max(sell_one,sell_two);
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.