213. House Robber II

Description

Here

Intuition

We can easily derive a sub function without considering the cycle problem

private int rob(int[] num, int lo, int hi) {
    int include = 0, exclude = 0;
    for (int j = lo; j <= hi; j++) {
        int i = include, e = exclude;
        include = e + num[j];
        exclude = Math.max(e, i);
    }
    return Math.max(include, exclude);
}

Therefore, we can return Math.max(rob(nums, 0, nums.length - 2), rob(num, 1, nums.length - 1))

Solution

Solution

results matching ""

    No results matching ""