Skip to the code
cf.
+10 XP

#484 Tile a Banner Strip

easy

A marketing banner is a strip n units wide. A designer fills it from left to right with tiles that are 1, 2, or 3 units wide, with no gaps and no overlap. The order matters: a 1-unit tile followed by a 2-unit tile is a different layout from a 2 followed by a 1.

Write countTilings(n) that returns the number of different layouts that fill the strip exactly. An empty strip (n = 0) has exactly one layout: no tiles.

Example 1

Input: n = 3
Output: 4

1+1+1, 1+2, 2+1, and 3.

Example 2

Input: n = 4
Output: 7

Layouts ending with a 1 (4 of them), a 2 (2 of them), or a 3 (1 of them).

Constraints

  • 0 <= n <= 35
  • The answer fits in a normal JavaScript number.
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).