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).

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.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(m ร— 3^(2n) ร— introvertsCount ร— extrovertsCount)

For each of m rows, we have 3^n current states ร— 3^n previous states, and we track counts of remaining people

n
2n
โœ“ Linear Growth
Space Complexity
O(3^(2n) ร— introvertsCount ร— extrovertsCount)

Memoization table stores results for all state combinations

n
2n
โšก Linearithmic Space

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
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