Concise C++ DP solution


  • 0
    X

    h is for hold stock, s is for sold stock. This can be a general use for all stock problems

    class Solution {
    public:
        int maxProfit(vector<int>& prices, int fee) {
                int h = INT_MIN, s = 0;
                for (int p : prices) {
                        h = max(h, s - p - fee);
                        s = max(s, h + p);
                }
                return s;
        }
    };
    

Log in to reply
 

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