Code Logo

Garden Lantern Seating Ways

Published at05 Jan 2026
Hard 2 views
Like15

You have a row of seats, and each seat contributes a brightness value if you choose it. The goal is to count how many selections add up to the target exactly, with one strict rule: you are not allowed to pick two neighboring seats.

This is what makes the problem interesting. A seat can help you reach the target, but taking it also blocks the seat right before and right after it. So the answer is not just about matching the sum. It is about matching the sum while respecting the spacing rule.

For example, if brightness = [2,1,2] and target = 2, there are two valid choices: pick the first seat alone or pick the third seat alone. If brightness = [2,4,3] and target = 5, there is only one valid choice, which is taking the first and third seats together.

So the task is to count every non-adjacent selection whose total brightness is exactly the target.

Example Input & Output

Example 1
Input
brightness = [2,1,2], target = 2
Output
2
Explanation

Either seat 1 alone or seat 3 alone satisfies the requirement.

Example 2
Input
brightness = [2,4,3], target = 5
Output
1
Explanation

Only seats 1 and 3 together meet the brightness target without adjacency.

Example 3
Input
brightness = [1,2,3,4], target = 4
Output
2
Explanation

The sets {1,3} and {4} both sum to 4 while avoiding adjacent selections.

Algorithm Flow

Recommendation Algorithm Flow for Garden Lantern Seating Ways
Recommendation Algorithm Flow for Garden Lantern Seating Ways

Solution Approach

This is a good fit for dynamic programming with memoization. At each seat, you really have two choices: skip it, or take it. If you take it, you must jump over the next seat because adjacent picks are forbidden.

That naturally gives a state like:

dp(index, remaining)

Here, index tells us which seat we are considering, and remaining tells us how much brightness we still need to reach the target.

The transitions are:

Skip the current seat and move to index + 1.

Take the current seat, subtract its value from remaining, and move to index + 2.

If remaining === 0, we found one valid arrangement. If we run out of seats or remaining goes negative, that path contributes nothing.

Memoizing those states keeps us from recomputing the same subproblems again and again. The result is a clean DP that counts all valid ways without brute-forcing every arrangement from scratch.

Best Answers

java
class Solution {
    public int calculate_seating_ways(int n, int k) {
        if (k < 0 || k > (n + 1) / 2) return 0;
        if (k == 0) return 1;
        return (int) nCr(n - k + 1, k);
    }
    private long nCr(int n, int r) {
        if (r > n) return 0;
        if (r == 0 || r == n) return 1;
        if (r > n / 2) r = n - r;
        long res = 1;
        for (int i = 1; i <= r; i++) {
            res = res * (n - i + 1) / i;
        }
        return res;
    }
}