Count Number of Ways to Place Houses - Problem

Imagine a charming residential street with n plots on each side (total of 2n plots). You're a city planner tasked with determining how many different ways houses can be placed while following a strict zoning rule: no two houses can be adjacent on the same side of the street.

Here's the interesting part: houses on opposite sides of the street can be directly across from each other - the adjacency rule only applies to houses on the same side.

Goal: Count the total number of valid house placement configurations.

Input: An integer n representing the number of plots on each side

Output: Number of ways to place houses modulo 109 + 7

Example: For n=2, you have 4 plots total (2 on each side). Some valid configurations include: no houses, houses only on opposite corners, houses on one side with a gap, etc.

Input & Output

example_1.py โ€” Basic Case
$ Input: n = 1
โ€บ Output: 4
๐Ÿ’ก Note: With 1 plot per side (2 plots total), we can have: (1) no houses, (2) house on left only, (3) house on right only, (4) houses on both sides (they're not adjacent since they're on different sides)
example_2.py โ€” Medium Case
$ Input: n = 2
โ€บ Output: 9
๐Ÿ’ก Note: For 2 plots per side, each side has 3 valid arrangements: [], [H,_], [_,H] where H=house, _=empty. Since sides are independent: 3 ร— 3 = 9 total arrangements
example_3.py โ€” Larger Case
$ Input: n = 3
โ€บ Output: 25
๐Ÿ’ก Note: Each side of 3 plots has 5 valid arrangements: [], [H,_,_], [_,H,_], [_,_,H], [H,_,H]. Total arrangements: 5 ร— 5 = 25

Visualization

Tap to expand
House Placement on Street (n=3)Left Side (5 valid arrangements)Empty, Empty, Empty๐Ÿ House, Empty, Empty๐Ÿ Empty, House, Empty๐Ÿ Empty, Empty, House๐Ÿ ๐Ÿ House, Empty, HouseSTREETRight Side (5 valid arrangements)Same arrangements as left sideTotal Ways: 5 ร— 5 = 25Key Insight: Both sides are independent!
Understanding the Visualization
1
Identify Independence
Recognize that left and right sides of street are completely independent
2
Solve One Side
Use DP to count arrangements for n plots with no adjacent houses
3
Apply Multiplication
Square the one-side result since both sides can be arranged independently
4
Handle Large Numbers
Apply modular arithmetic to prevent integer overflow
Key Takeaway
๐ŸŽฏ Key Insight: The problem elegantly reduces to finding arrangements for one side (classic DP), then squaring the result due to independence of both sides.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Single pass through n positions to build DP table

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Only need to track previous state, can optimize to constant space

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค n โ‰ค 1000
  • Answer returned modulo 109 + 7
  • No two houses can be adjacent on the same side of the street
Asked in
Google 45 Amazon 38 Meta 32 Microsoft 28
38.0K Views
Medium Frequency
~15 min Avg. Time
1.5K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen