Skip to the code
cf.
+10 XP

#565 Flag Rows Without Two Heavy Neighbors

easy

A performance dashboard has n heavy widgets in a row, and each one is either on (1) or off (0). Two neighboring heavy widgets may never both be on, or the page stutters.

Write countSafeRows(n) that returns how many on/off patterns of length n follow this rule, modulo 1000000007.

Example 1

Input: n = 3
Output: 5

000, 001, 010, 100, and 101.

Example 2

Input: n = 1
Output: 2

0 or 1.

Constraints

  • 1 <= n <= 1000000
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).