213. House Robber II

class Solution {
    public int rob(int[] nums) {
        if (nums.length==1)return nums[0];
        return Math.max(helper(nums, 0), helper(nums,1));
    }
    
    private int helper(int[] nums, int start){
        int memo = 0;
        int preMemo = 0;
        for (int i=start; i<nums.length-1+start; i++){
            int curr = Math.max(nums[i]+preMemo, memo);
            preMemo=memo;
            memo=curr;
        }
        return memo;
    }
}

Last updated

Was this helpful?