Clean DP implementation


  • 0
    V
        public int rob(int[] nums) {
            if (nums.length == 1) return nums[0];
            return Math.max(rob(nums, 1, nums.length - 1), rob(nums, 0, nums.length - 2));
        }
        
        public int rob(int[] nums, int from, int to) {
            int prev = 0; //previous house
            int gprev = 0; //grand previous house
            for (int i = from; i <= to; i++) {
                int cur = Math.max(gprev + nums[i], prev);
                gprev = prev;
                prev = cur;
            }
            return prev;
        }
    

Log in to reply
 

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