Skip to the code
cf.
+25 XP

#519 Ordered Card Widths for a Row

medium

A grid system has rows total columns wide. Cards come in a few allowed widths, listed in widths, and a row is filled from left to right with cards whose widths add up to exactly total. Any width may be used many times, and the order matters: a 1-wide card followed by a 2-wide card is a different row from a 2 followed by a 1.

Write countRowLayouts(widths, total) that returns how many different rows are possible, modulo 1000000007.

Example 1

Input: widths = [1,2], total = 3
Output: 3

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

Example 2

Input: widths = [3,4], total = 5
Output: 0

No mix of 3 and 4 makes 5.

Constraints

  • 1 <= widths.length <= 50
  • 1 <= widths[i] <= 1000, all different
  • 1 <= total <= 1000
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).