Simplest Java, O(1) space


  • 0
    S
    public int rob(int[] nums) {
            if(nums == null) {
                return 0;
            }
            
            int max = 0;
            int pre = 0;
    
            for(int i = nums.length-1; i >= 0; i--) {
                int preMax = max;
                max = Math.max(max, pre+nums[i]);
                pre = preMax;
            }
            
            return max;
        }

Log in to reply
 

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