Saturday, November 14, 2015

[leetcode] House Robber

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
public class Solution {
    public int rob(int[] nums) {
        if (nums == null || nums.length == 0) return 0;
        if (nums.length == 1) return nums[0];
        int previousOne = nums[1] > nums[0]?nums[1]:nums[0];
        int previousTwo = nums[0];
        for (int i = 2; i < nums.length; i++){
            int temp = Math.max(previousTwo+nums[i], previousOne);
            previousTwo = previousOne;
            previousOne = temp;
        }
        
        return previousOne;
    }
}

No comments:

Post a Comment