why don't we add two empty houses at the begnning


  • 0
    S

    public class Solution {
    public int rob(int[] nums) {
    if(nums.length==0) return 0;
    // add two fake houses to avoid index out of bound error
    int[] maxVal = new int[nums.length+2];
    for(int i= 0; i<nums.length; i++) {
    maxVal[i+2] = Math.max(maxVal[i] + nums[i], maxVal[i+1]);
    }

        return maxVal[nums.length+1];
    }
    

    }


Log in to reply
 

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