Painting the Fenceproblem

Pattern: 1D DP

medium

Paint n posts with k colours so that no more than two neighbouring posts share a colour. Track ways where the last two posts are the same and where they differ: a 'same' only follows a 'different', and a 'different' can follow either with k - 1 colours.

No verified practice link yet. The guide below states the problem in full, so you can still solve it here.

The problem

Return the number of ways to paint n fence posts with k colours so that at most two neighbouring posts have the same colour. The answer is taken modulo 10⁹ + 7. n = 3, k = 2 gives 6. The catalogue has no practice link for this one.

Think first

  • What does the rule forbid?
  • What do you need to know about the end of a painted fence?
  • How many colours for a 'different' post?

Hints

    Brute force, and why it fails

    Try every colouring: kⁿ.

    The key insight

    The only history that matters is whether the last two posts match.

    Dry run

    same0
    diff2

    n = 3, k = 2. One post: diff = 2, same = 0.

    Step 1 of 3

    The plan

    same = 0; diff = k
    repeat n - 1 times: same, diff = diff, (same + diff) × (k - 1)
    return same + diff
    

    Complexity

    Time
    O(n)
    Space
    O(1)

    Two counters.

    Edge cases

    • n = 1: k.
    • k = 1 and n = 3: 0.
    • n = 2: k².
    I've built my logic, show the code

    Java

    class Solution {
        public int countWays(int n, int k) {
            final long MOD = 1_000_000_007L;
            long same = 0, diff = k;                // ways ending with the last two posts the same / different
            for (int i = 2; i <= n; i++) {
                long newSame = diff;                // repeat the last colour only after a change
                long newDiff = (same + diff) % MOD * (k - 1) % MOD;   // any other colour
                same = newSame;
                diff = newDiff;
            }
            return (int) ((same + diff) % MOD);
        }
    }

    C++

    class Solution {
    public:
        int countWays(int n, int k) {
            const long long MOD = 1000000007LL;
            long long same = 0, diff = k;           // ways ending with the last two posts the same / different
            for (int i = 2; i <= n; i++) {
                long long newSame = diff;           // repeat the last colour only after a change
                long long newDiff = (same + diff) % MOD * (k - 1) % MOD;   // any other colour
                same = newSame;
                diff = newDiff;
            }
            return static_cast<int>((same + diff) % MOD);
        }
    };

    Python

    class Solution:
        def countWays(self, n: int, k: int) -> int:
            MOD = 1_000_000_007
            same, diff = 0, k                       # ways ending with the last two posts the same / different
            for _ in range(2, n + 1):
                same, diff = diff, (same + diff) * (k - 1) % MOD   # repeat only after a change; or any other colour
            return (same + diff) % MOD

    Common mistakes

    • Allowing two 'same' steps in a row.
    • Forgetting the modulo.

    Similar problems