Ctrl K

Card Towers

Medium
You have cards playing cards and must build a card tower that uses every one of them. The tower is built in rows. Its basic piece is a "tent": two cards leaning against each other. In a row of t tents, one flat card lies across each pair of neighbouring tents, so the row takes 3t - 1 cards. Every tent in an upper row must stand on a flat card of the row below, which means each row has strictly fewer tents than the row beneath it. Tents in a row are placed side by side from the left edge, so a tower is fully described by the number of tents in each row. Return the number of different towers that use exactly cards cards. Two towers are different if some row holds a different number of cards in them.
Example 1
cards=7 → 1

A bottom row of two tents takes 2 · 2 + 1 = 5 cards, and one tent on top takes 2 more. That is 7, and there is no other option.

Example 2
cards=21 → 2

Rows of 4, 3 and 1 tents: 11 + 8 + 2 = 21. Rows of 5, 2 and 1: 14 + 5 + 2 = 21. There are no other ways.

Example 3
cards=4 → 0

One tent takes 2 cards and a row of two takes 5, so exactly 4 cards cannot be used.

Constraints
  • 1 ≤ cards ≤ 500
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsCounting Grid Routes MediumCounting Search Tree Shapes MediumEvery Bracketing Result MediumNumbers Made of Twos, Threes and Fives MediumFewest Square Terms Medium
loading Python…
Input
cards=7
Expected
1

Run checks the first 3 tests in your browser. Submit checks all 92 tests on the server, hidden ones included.