Skip to the code
cf.
+25 XP

#499 Stripe Rows Without Three in a Row

medium

A data table has n rows, and each row gets one of k background colors. To keep the table readable, no three consecutive rows may share the same color. Two in a row is fine.

Write countRowColorings(n, k) that returns how many different colorings follow this rule, modulo 1000000007.

Example 1

Input: n = 3, k = 2
Output: 6

All 8 colorings of 3 rows with 2 colors, except the 2 where all rows match.

Example 2

Input: n = 2, k = 3
Output: 9

Any two colors are fine for two rows: 3 × 3.

Constraints

  • 1 <= n <= 10000
  • 1 <= k <= 100000
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).