Simple Java Solution


  • 1
    S
    public class Solution {
        public int rob(int[] nums) {
            int n = nums.length;
            if(n==1) return nums[0];
            return Math.max(helper(nums, 0, n-1), helper(nums, 1, n));
        }
        
        private int helper(int nums[], int inclusive, int exclusive){
            if(inclusive>=exclusive) return 0;
            int first = 0, second = nums[inclusive];
            for(int i=inclusive+1; i<exclusive; i++){
                int temp = second;
                second = Math.max(first+nums[i], temp);
                first = temp;
            }
            return second;
        }
    }

Log in to reply
 

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