clueless coding // TODO: be smarter

LeetCode House Robber II: CPP Solution



TL;DR Code Solution



int robRange(vector<int>& nums, int start, int end) {

    int robbedLast = 0;
    int didntLast = 0;

    for (int i = start; i <= end; i++) {
        int temp = didntLast;
        didntLast = max(didntLast, robbedLast);
        robbedLast = temp + nums[i];
    }

    return max(didntLast, robbedLast);

}

int rob(vector<int>& nums) {

    if (nums.size() < 2)
        return nums.size() ? nums[0] : 0;
    return max(robRange(nums, 0, nums.size() - 2), robRange(nums, 1, nums.size() - 1));




}