C++ 6ms 100%


  • 0
    S
    class Solution {
    public:
        int maxProfit(vector<int>& a) {
            int n = a.size();
            int save1[n+1], save2[n+1], save3[n+1], save4[n+1], ans;
            save1[n] = save2[n] = save3[n] = save4[n] = ans = 0;
            for(int i = n-1; i >= 0; i--){
                save1[i] = max(save1[i+1], a[i]);
                save2[i] = max(save2[i+1], save1[i+1]-a[i]);
                save3[i] = max(save3[i+1], save2[i+1] + a[i]);
                save4[i] = max(save4[i+1], save3[i+1] - a[i]);
                ans = max(ans, save4[i]);
            }
            return ans;
        }
    };
    

Log in to reply
 

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