intermediate · dynamic programming · state machine

Collect the most without taking neighbours

Problem

Houses in a row hold donations of known size. A collector may visit any houses except two neighbours in a row. What is the largest total they can collect?

Constraints

  • 0 ≤ n ≤ 32
  • amounts from 0 to 999

Examples

[2, 7, 9, 3, 1] → 12 — 2 + 9 + 1.

Show the solution

Take / skip DP with two variables

After each house only two numbers matter: the best total ending with this house taken, and the best total with it skipped.

Why it is correct

Taking house i requires house i − 1 skipped, so take(i) = skip(i − 1) + a[i]; skipping allows anything before, so skip(i) = max(take(i − 1), skip(i − 1)). The answer is the better of the two after the last house.

Complexity

Time O(n), extra space O(1). Constant work per house.

For contrast: Try every non-adjacent subset. That costs O(2ⁿ) time and O(n) space.

Edge cases

  • No houses: 0.
  • One house: its amount.
  • Zero amounts.

Common mistakes

  • Greedily taking every other house (alternating can be wrong: [2, 1, 1, 2] → 4 takes the two ends).
  • Overwriting take before computing skip.

Step through it

The solution in four languages

Each is checked against one shared set of cases and a randomized comparison with a brute-force reference.

namespace Bekadan.Dsa.Examples.Problems;
 
public static class HouseRobber
{
    /// <summary>Largest total of non-adjacent amounts. O(n), O(1) space.</summary>
    public static int Best(int[] amounts)
    {
        int take = 0, skip = 0; // best totals with the previous house taken / skipped
        foreach (int amount in amounts)
        {
            int newTake = skip + amount; // taking this house needs the previous one skipped
            skip = Math.Max(skip, take);
            take = newTake;
        }
        return Math.Max(take, skip);
    }
}

Tested on .NET 10 (SDK 10.0.403), C# 14 · examples/csharp/Bekadan.Dsa.Examples/Problems/HouseRobber.cs

from collections.abc import Iterable
 
 
def best(amounts: Iterable[int]) -> int:
    """Largest total of non-adjacent amounts. O(n), O(1) space."""
    take, skip = 0, 0  # best totals with the previous house taken / skipped
    for amount in amounts:
        new_take = skip + amount  # taking this house needs the previous one skipped
        skip = max(skip, take)
        take = new_take
    return max(take, skip)

Tested on CPython 3.14.0 · examples/python/dsa_examples/problems/house_robber.py

package dev.bekadan.dsa.problems;
 
public final class HouseRobber {
    private HouseRobber() {
    }
 
    /** Largest total of non-adjacent amounts. O(n), O(1) space. */
    public static int best(int[] amounts) {
        int take = 0, skip = 0; // best totals with the previous house taken / skipped
        for (int amount : amounts) {
            int newTake = skip + amount; // taking this house needs the previous one skipped
            skip = Math.max(skip, take);
            take = newTake;
        }
        return Math.max(take, skip);
    }
}

Tested on Java 25 (Temurin 25.0.5); also compiles on Java 17 · examples/java/src/main/java/dev/bekadan/dsa/problems/HouseRobber.java

#pragma once
 
#include <algorithm>
#include <span>
 
namespace bekadan::dsa::problems {
 
// Largest total of non-adjacent amounts. O(n), O(1) space.
inline int house_robber(std::span<const int> amounts) {
    int take = 0, skip = 0;  // best totals with the previous house taken / skipped
    for (int amount : amounts) {
        const int new_take = skip + amount;  // taking this house needs the previous one skipped
        skip = std::max(skip, take);
        take = new_take;
    }
    return std::max(take, skip);
}
 
}  // namespace bekadan::dsa::problems

Tested on C++23, GCC 15.3 (libstdc++) · examples/cpp/include/bekadan/dsa/problems/house_robber.hpp