Simple Java Solution


  • 0
    Z
    public class Solution {
        public int rob(int[] nums) {
            if(nums == null || nums.length == 0){
                return 0;
            }
            
            int maxOdd = 0;
            int maxEven = 0;
            for (int i = 0; i < nums.length; i++) {
                if(i % 2 == 0){
                    maxOdd = Math.max(maxOdd+nums[i], maxEven);
                }else{
                    maxEven = Math.max(maxEven+nums[i], maxOdd);
                }
            }
            return Math.max(maxOdd, maxEven);
        }
    }
    

Log in to reply
 

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