C++ DP with two pairs of rob, noRob for cur, pre, respectively


  • 0
    D
    class Solution {
    public:
        int rob(vector<int> &num) {
            // DP 
            // pre robbed, max(preNo, preYes)
            // pre not roobed current+preNo 
            int preNoRob = 0, preYesRob =0;
            
            for(int i=1; i <= num.size(); i++){
                int curNoRob, curYesRob;
                curNoRob = max(preNoRob, preYesRob);
                curYesRob = preNoRob + num[i-1];
                preNoRob = curNoRob;
                preYesRob = curYesRob;
            }
            return max(preNoRob, preYesRob);
        }
    };

Log in to reply
 

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