Maximize Grid Happiness Problem
Description
LeetCode Problem 1659.
You are given four integers, m, n, introvertsCount, and extrovertsCount. You have an m x n grid, and there are two types of people: introverts and extroverts. There are introvertsCount introverts and extrovertsCount extroverts.
You should decide how many people you want to live in the grid and assign each of them one grid cell. Note that you do not have to have all the people living in the grid.
The happiness of each person is calculated as follows:
- Introverts start with
120happiness and lose30happiness for each neighbor (introvert or extrovert). - Extroverts start with
40happiness and gain20happiness for each neighbor (introvert or extrovert).
Neighbors live in the directly adjacent cells north, east, south, and west of a person's cell.
The grid happiness is the sum of each person's happiness. Return the maximum possible grid happiness.
Example 1:

Input: m = 2, n = 3, introvertsCount = 1, extrovertsCount = 2
Output: 240
Explanation: Assume the grid is 1-indexed with coordinates (row, column).
We can put the introvert in cell (1,1) and put the extroverts in cells (1,3) and (2,3).
- Introvert at (1,1) happiness: 120 (starting happiness) - (0 * 30) (0 neighbors) = 120
- Extrovert at (1,3) happiness: 40 (starting happiness) + (1 * 20) (1 neighbor) = 60
- Extrovert at (2,3) happiness: 40 (starting happiness) + (1 * 20) (1 neighbor) = 60
The grid happiness is 120 + 60 + 60 = 240.
The above figure shows the grid in this example with each person's happiness. The introvert stays in the light green cell while the extroverts live on the light purple cells.
Example 2:
Input: m = 3, n = 1, introvertsCount = 2, extrovertsCount = 1
Output: 260
Explanation: Place the two introverts in (1,1) and (3,1) and the extrovert at (2,1).
- Introvert at (1,1) happiness: 120 (starting happiness) - (1 * 30) (1 neighbor) = 90
- Extrovert at (2,1) happiness: 40 (starting happiness) + (2 * 20) (2 neighbors) = 80
- Introvert at (3,1) happiness: 120 (starting happiness) - (1 * 30) (1 neighbor) = 90
The grid happiness is 90 + 80 + 90 = 260.
Example 3:
Input: m = 2, n = 2, introvertsCount = 4, extrovertsCount = 0
Output: 240
Constraints:
1 <= m, n <= 50 <= introvertsCount, extrovertsCount <= min(m * n, 6)
Solution
Approach: Ternary Row States + Memoized Search
Each cell is empty (0), an introvert (1) or an extrovert (2), so one row of n cells is a base-3 number below 3^n. For every row state precompute:
ix[cur]andex[cur]: the number of introverts and extroverts in it;f[cur]: the happiness inside the row, counting each person's base value and the effect between left-right neighbors;g[pre][cur]: the extra happiness between two stacked rowspreandcur, from their vertical neighbors.
For a pair of neighbors the total change is -60 for two introverts, -10 for an introvert and an extrovert (-30 + 20), and +40 for two extroverts.
Let dfs(i, pre, ic, ec) be the best happiness for rows i and below, when the row above is pre and ic introverts and ec extroverts are left. If i = m or nobody is left, it is 0. Otherwise try every row state cur with ix[cur] <= ic and ex[cur] <= ec, and take the best of
f[cur] + g[pre][cur] + dfs(i + 1, cur, ic - ix[cur], ec - ex[cur]).
The answer is dfs(0, 0, introvertsCount, extrovertsCount), with memoization over the four arguments.
Complexity: O(3^(2n) * (m * ic * ec + n)) time and O(3^(2n) + 3^n * m * ic * ec) space, where ic and ec are the two counts.
from functools import lru_cache
class Solution:
def getMaxGridHappiness(
self, m: int, n: int, introvertsCount: int, extrovertsCount: int
) -> int:
@lru_cache(None)
def dfs(i: int, pre: int, ic: int, ec: int) -> int:
if i == m or (ic == 0 and ec == 0):
return 0
ans = 0
for cur in range(mx):
if ix[cur] <= ic and ex[cur] <= ec:
a = f[cur] + g[pre][cur]
b = dfs(i + 1, cur, ic - ix[cur], ec - ex[cur])
ans = max(ans, a + b)
return ans
mx = pow(3, n)
f = [0] * mx
g = [[0] * mx for _ in range(mx)]
h = [[0, 0, 0], [0, -60, -10], [0, -10, 40]]
bits = [[0] * n for _ in range(mx)]
ix = [0] * mx
ex = [0] * mx
for i in range(mx):
mask = i
for j in range(n):
mask, x = divmod(mask, 3)
bits[i][j] = x
if x == 1:
ix[i] += 1
f[i] += 120
elif x == 2:
ex[i] += 1
f[i] += 40
if j:
f[i] += h[x][bits[i][j - 1]]
for i in range(mx):
for j in range(mx):
for k in range(n):
g[i][j] += h[bits[i][k]][bits[j][k]]
return dfs(0, 0, introvertsCount, extrovertsCount)