Maximize Grid Happiness - Problem

Imagine you're a city planner tasked with arranging residents in an m x n residential grid to maximize overall community happiness! 🏘️

You have two personality types to work with:

  • Introverts: Start with 120 happiness but lose 30 points for each neighbor
  • Extroverts: Start with 40 happiness but gain 20 points for each neighbor

Given introvertsCount introverts and extrovertsCount extroverts, you need to strategically place them (or leave some out) to achieve the maximum possible grid happiness.

Neighbors are those living in directly adjacent cells (north, south, east, west). The total happiness is the sum of all residents' individual happiness scores.

Input & Output

example_1.py — Basic Grid
$ Input: m = 2, n = 3, introvertsCount = 1, extrovertsCount = 2
Output: 240
💡 Note: Place 1 introvert and 2 extroverts optimally. Extroverts gain happiness from neighbors while introvert should be isolated.
example_2.py — Small Grid
$ Input: m = 3, n = 1, introvertsCount = 2, extrovertsCount = 1
Output: 260
💡 Note: In a 3×1 grid, place extrovert in middle (gains from 2 neighbors) and introverts on ends (each loses from 1 neighbor).
example_3.py — Edge Case
$ Input: m = 2, n = 2, introvertsCount = 4, extrovertsCount = 0
Output: 240
💡 Note: With only introverts available, place them to minimize neighbor interactions (diagonal arrangement gives 60×4 = 240).

Constraints

  • 1 ≤ m, n ≤ 5
  • 0 ≤ introvertsCount ≤ min(6, m × n)
  • 0 ≤ extrovertsCount ≤ min(6, m × n)
  • Grid size is small enough for DP with bitmask approach

Visualization

Tap to expand
The Grid Happiness Journey😔 BruteForceO(3^(m×n))Optimize!😊 DP +BitmaskO(m×3^(2n)×I×E)DP State TransitionPrevious RowIXECurrent RowXIXCalculate happiness:• Check vertical neighbors• Check horizontal neighbors• Apply I/E happiness rules• Memoize result🎯 Key InsightRow-by-row processing + memoization transformsexponential complexity into manageable polynomial time!
Understanding the Visualization
1
Understanding the Problem
Introverts lose happiness from neighbors (-30 each), extroverts gain happiness (+20 each). Goal: maximize total happiness.
2
Brute Force Insight
Trying all 3^(m×n) combinations is too slow - we need to identify overlapping subproblems.
3
Key Observation
Happiness only depends on direct neighbors. We can solve row by row, remembering previous row state.
4
DP State Design
State: (current_row, previous_row_configuration, introverts_left, extroverts_left)
5
Optimal Solution
Generate all 3^n row states, use DP with memoization to build solution incrementally.
Key Takeaway
🎯 Key Insight: By processing the grid row by row and using memoization to cache subproblem results, we transform an intractable exponential problem into an efficient dynamic programming solution that leverages the local nature of neighbor interactions.
Asked in
Google 15 Meta 12 Amazon 8 Microsoft 6
27.6K Views
Medium Frequency
~35 min Avg. Time
892 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