Ctrl K

Towers Seen From the Left

Hard
You have to line up n towers of heights 1, 2, …, n: all heights are different and each appears exactly once. An observer looks at the row from the left. A tower is visible if no taller tower stands anywhere to its left; the first tower is always visible. For example, in the row [2, 1, 4, 3, 5] the towers of height 2, 4 and 5 are visible. Count the lineups in which the observer sees exactly visible towers. The answer can be large, so return it modulo 10⁹ + 7.
Example 1
n=4, visible=3 → 6

Six lineups work, for example [1, 2, 4, 3]: 1, 2 and 4 are visible, while 3 is hidden behind 4.

Example 2
n=3, visible=1 → 2

Only one tower is visible, so the tallest one must stand first: [3, 1, 2] and [3, 2, 1].

Example 3
n=6, visible=6 → 1

Every tower is visible only when they stand in increasing order.

Constraints
  • 1 ≤ n ≤ 1000
  • 1 ≤ visible ≤ n
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsTasters and the Spoiled Cask HardRadio Show Without Quick Repeats HardParcel Run Orders HardA Fair Split of Colored Beads HardOther Orders for the Same Search Tree Hard
loading Python…
Input
n=4, visible=3
Expected
6

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