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 120 happiness and lose30 happiness for each neighbor (introvert or extrovert).
Extroverts start with 40 happiness and gain20 happiness 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 <= 5
0 <= introvertsCount, extrovertsCount <= min(m * n, 6)
Problem summary: 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 120 happiness and lose 30 happiness for each neighbor (introvert or extrovert). Extroverts start with 40 happiness and gain 20 happiness 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.
Baseline thinking
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Dynamic Programming · Bit Manipulation
Example 1
2
3
1
2
Example 2
3
1
2
1
Example 3
2
2
4
0
Step 02
Core Insight
What unlocks the optimal approach
For each cell, it has 3 options, either it is empty, or contains an introvert, or an extrovert.
You can do DP where you maintain the state of the previous row, the number of remaining introverts and extroverts, the current row and column, and try the 3 options for each cell.
Assume that the previous columns in the current row already belong to the previous row.
Interview move: turn each hint into an invariant you can check after every iteration/recursion step.
Step 03
Algorithm Walkthrough
Iteration Checklist
Define state (indices, window, stack, map, DP cell, or recursion frame).
Apply one transition step and update the invariant.
Record answer candidate when condition is met.
Continue until all input is consumed.
Use the first example testcase as your mental trace to verify each transition.
Step 04
Edge Cases
Minimum Input
Single element / shortest valid input
Validate boundary behavior before entering the main loop or recursion.
Duplicates & Repeats
Repeated values / repeated states
Decide whether duplicates should be merged, skipped, or counted explicitly.
Extreme Constraints
Largest constraint values
Re-check complexity target against constraints to avoid time-limit issues.
Invalid / Corner Shape
Empty collections, zeros, or disconnected structures
Handle special-case structure before the core algorithm path.
Step 05
Full Annotated Code
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1659: Maximize Grid Happiness
class Solution {
private int m;
private int mx;
private int[] f;
private int[][] g;
private int[][] bits;
private int[] ix;
private int[] ex;
private Integer[][][][] memo;
private final int[][] h = {{0, 0, 0}, {0, -60, -10}, {0, -10, 40}};
public int getMaxGridHappiness(int m, int n, int introvertsCount, int extrovertsCount) {
this.m = m;
mx = (int) Math.pow(3, n);
f = new int[mx];
g = new int[mx][mx];
bits = new int[mx][n];
ix = new int[mx];
ex = new int[mx];
memo = new Integer[m][mx][introvertsCount + 1][extrovertsCount + 1];
for (int i = 0; i < mx; ++i) {
int mask = i;
for (int j = 0; j < n; ++j) {
int x = mask % 3;
mask /= 3;
bits[i][j] = x;
if (x == 1) {
ix[i]++;
f[i] += 120;
} else if (x == 2) {
ex[i]++;
f[i] += 40;
}
if (j > 0) {
f[i] += h[x][bits[i][j - 1]];
}
}
}
for (int i = 0; i < mx; ++i) {
for (int j = 0; j < mx; ++j) {
for (int k = 0; k < n; ++k) {
g[i][j] += h[bits[i][k]][bits[j][k]];
}
}
}
return dfs(0, 0, introvertsCount, extrovertsCount);
}
private int dfs(int i, int pre, int ic, int ec) {
if (i == m || (ic == 0 && ec == 0)) {
return 0;
}
if (memo[i][pre][ic][ec] != null) {
return memo[i][pre][ic][ec];
}
int ans = 0;
for (int cur = 0; cur < mx; ++cur) {
if (ix[cur] <= ic && ex[cur] <= ec) {
ans = Math.max(
ans, f[cur] + g[pre][cur] + dfs(i + 1, cur, ic - ix[cur], ec - ex[cur]));
}
}
return memo[i][pre][ic][ec] = ans;
}
}
// Accepted solution for LeetCode #1659: Maximize Grid Happiness
func getMaxGridHappiness(m int, n int, introvertsCount int, extrovertsCount int) int {
mx := int(math.Pow(3, float64(n)))
f := make([]int, mx)
g := make([][]int, mx)
h := [3][3]int{{0, 0, 0}, {0, -60, -10}, {0, -10, 40}}
bits := make([][]int, mx)
ix := make([]int, mx)
ex := make([]int, mx)
memo := make([][][][]int, m)
for i := range g {
g[i] = make([]int, mx)
bits[i] = make([]int, n)
}
for i := range memo {
memo[i] = make([][][]int, mx)
for j := range memo[i] {
memo[i][j] = make([][]int, introvertsCount+1)
for k := range memo[i][j] {
memo[i][j][k] = make([]int, extrovertsCount+1)
for l := range memo[i][j][k] {
memo[i][j][k][l] = -1
}
}
}
}
for i := 0; i < mx; i++ {
mask := i
for j := 0; j < n; j++ {
x := mask % 3
mask /= 3
bits[i][j] = x
if x == 1 {
ix[i]++
f[i] += 120
} else if x == 2 {
ex[i]++
f[i] += 40
}
if j > 0 {
f[i] += h[x][bits[i][j-1]]
}
}
}
for i := 0; i < mx; i++ {
for j := 0; j < mx; j++ {
for k := 0; k < n; k++ {
g[i][j] += h[bits[i][k]][bits[j][k]]
}
}
}
var dfs func(int, int, int, int) int
dfs = func(i, pre, ic, ec int) int {
if i == m || (ic == 0 && ec == 0) {
return 0
}
if memo[i][pre][ic][ec] != -1 {
return memo[i][pre][ic][ec]
}
ans := 0
for cur := 0; cur < mx; cur++ {
if ix[cur] <= ic && ex[cur] <= ec {
ans = max(ans, f[cur]+g[pre][cur]+dfs(i+1, cur, ic-ix[cur], ec-ex[cur]))
}
}
memo[i][pre][ic][ec] = ans
return ans
}
return dfs(0, 0, introvertsCount, extrovertsCount)
}
# Accepted solution for LeetCode #1659: Maximize Grid Happiness
class Solution:
def getMaxGridHappiness(
self, m: int, n: int, introvertsCount: int, extrovertsCount: int
) -> int:
@cache
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)
// Accepted solution for LeetCode #1659: Maximize Grid Happiness
struct Solution;
impl Solution {
fn get_max_grid_happiness(n: i32, m: i32, introverts_count: i32, extroverts_count: i32) -> i32 {
let mut res = 0;
let m = m as usize;
let n = n as usize;
let mut grid: Vec<Vec<i32>> = vec![vec![0; m]; n];
let mut cur = 0;
Self::dfs(
0,
introverts_count,
extroverts_count,
&mut grid,
&mut cur,
&mut res,
n,
m,
);
res
}
fn dfs(
start: usize,
a: i32,
b: i32,
grid: &mut Vec<Vec<i32>>,
cur: &mut i32,
max: &mut i32,
n: usize,
m: usize,
) {
if a == 0 && b == 0 || start == n * m {
*max = (*max).max(*cur);
return;
}
if *cur + a * 120 + b * 120 <= *max {
return;
}
let i = start / m;
let j = start % m;
if a > 0 {
grid[i][j] = 1;
let mut base = 120;
if i > 0 && grid[i - 1][j] == 1 {
base -= 60;
}
if i > 0 && grid[i - 1][j] == 2 {
base -= 10;
}
if j > 0 && grid[i][j - 1] == 1 {
base -= 60;
}
if j > 0 && grid[i][j - 1] == 2 {
base -= 10;
}
*cur += base;
Self::dfs(start + 1, a - 1, b, grid, cur, max, n, m);
*cur -= base;
grid[i][j] = 0;
}
if b > 0 {
grid[i][j] = 2;
let mut base = 40;
if i > 0 && grid[i - 1][j] == 1 {
base -= 10;
}
if i > 0 && grid[i - 1][j] == 2 {
base += 40;
}
if j > 0 && grid[i][j - 1] == 1 {
base -= 10;
}
if j > 0 && grid[i][j - 1] == 2 {
base += 40;
}
*cur += base;
Self::dfs(start + 1, a, b - 1, grid, cur, max, n, m);
*cur -= base;
grid[i][j] = 0;
}
if (i > 0 && grid[i - 1][j] != 0) || (j > 0 && grid[i][j - 1] != 0) {
Self::dfs(start + 1, a, b, grid, cur, max, n, m);
}
}
fn happiness(grid: &[Vec<i32>], n: usize, m: usize) -> i32 {
let mut res = 0;
for i in 0..n {
for j in 0..m {
let mut start = 0;
match grid[i][j] {
1 => {
start = 120;
if i > 0 && grid[i - 1][j] != 0 {
start -= 30;
}
if j > 0 && grid[i][j - 1] != 0 {
start -= 30;
}
if i + 1 < n && grid[i + 1][j] != 0 {
start -= 30;
}
if j + 1 < m && grid[i][j + 1] != 0 {
start -= 30;
}
}
2 => {
start = 40;
if i > 0 && grid[i - 1][j] != 0 {
start += 20;
}
if j > 0 && grid[i][j - 1] != 0 {
start += 20;
}
if i + 1 < n && grid[i + 1][j] != 0 {
start += 20;
}
if j + 1 < m && grid[i][j + 1] != 0 {
start += 20;
}
}
_ => {}
}
res += start;
}
}
res
}
}
#[test]
fn test() {
let n = 2;
let m = 3;
let introverts_count = 1;
let extroverts_count = 2;
let res = 240;
assert_eq!(
Solution::get_max_grid_happiness(n, m, introverts_count, extroverts_count),
res
);
let n = 3;
let m = 1;
let introverts_count = 2;
let extroverts_count = 1;
let res = 260;
assert_eq!(
Solution::get_max_grid_happiness(n, m, introverts_count, extroverts_count),
res
);
let n = 2;
let m = 2;
let introverts_count = 4;
let extroverts_count = 0;
let res = 240;
assert_eq!(
Solution::get_max_grid_happiness(n, m, introverts_count, extroverts_count),
res
);
}
// Accepted solution for LeetCode #1659: Maximize Grid Happiness
function getMaxGridHappiness(
m: number,
n: number,
introvertsCount: number,
extrovertsCount: number,
): number {
const mx = 3 ** n;
const f: number[] = Array(mx).fill(0);
const g: number[][] = Array(mx)
.fill(0)
.map(() => Array(mx).fill(0));
const h: number[][] = [
[0, 0, 0],
[0, -60, -10],
[0, -10, 40],
];
const bits: number[][] = Array(mx)
.fill(0)
.map(() => Array(n).fill(0));
const ix: number[] = Array(mx).fill(0);
const ex: number[] = Array(mx).fill(0);
const memo: number[][][][] = Array(m)
.fill(0)
.map(() =>
Array(mx)
.fill(0)
.map(() =>
Array(introvertsCount + 1)
.fill(0)
.map(() => Array(extrovertsCount + 1).fill(-1)),
),
);
for (let i = 0; i < mx; ++i) {
let mask = i;
for (let j = 0; j < n; ++j) {
const x = mask % 3;
mask = Math.floor(mask / 3);
bits[i][j] = x;
if (x === 1) {
ix[i] += 1;
f[i] += 120;
} else if (x === 2) {
ex[i] += 1;
f[i] += 40;
}
if (j > 0) {
f[i] += h[x][bits[i][j - 1]];
}
}
}
for (let i = 0; i < mx; ++i) {
for (let j = 0; j < mx; ++j) {
for (let k = 0; k < n; ++k) {
g[i][j] += h[bits[i][k]][bits[j][k]];
}
}
}
const dfs = (i: number, pre: number, ic: number, ec: number): number => {
if (i === m || (ic === 0 && ec === 0)) {
return 0;
}
if (memo[i][pre][ic][ec] !== -1) {
return memo[i][pre][ic][ec];
}
let ans = 0;
for (let cur = 0; cur < mx; ++cur) {
if (ix[cur] <= ic && ex[cur] <= ec) {
const a = f[cur] + g[pre][cur];
const b = dfs(i + 1, cur, ic - ix[cur], ec - ex[cur]);
ans = Math.max(ans, a + b);
}
}
return (memo[i][pre][ic][ec] = ans);
};
return dfs(0, 0, introvertsCount, extrovertsCount);
}
Step 06
Interactive Study Demo
Use this to step through a reusable interview workflow for this problem.
Press Step or Run All to begin.
Step 07
Complexity Analysis
Time
O(3^2n × (m × ic × ec + n)
Space
O(3^2n + 3^n × m × ic × ec)
Approach Breakdown
RECURSIVE
O(2ⁿ) time
O(n) space
Pure recursion explores every possible choice at each step. With two choices per state (take or skip), the decision tree has 2ⁿ leaves. The recursion stack uses O(n) space. Many subproblems are recomputed exponentially many times.
DYNAMIC PROGRAMMING
O(n × m) time
O(n × m) space
Each cell in the DP table is computed exactly once from previously solved subproblems. The table dimensions determine both time and space. Look for the state variables — each unique combination of state values is one cell. Often a rolling array can reduce space by one dimension.
Shortcut: Count your DP state dimensions → that’s your time. Can you drop one? That’s your space optimization.
Coach Notes
Common Mistakes
Review these before coding to avoid predictable interview regressions.
State misses one required dimension
Wrong move: An incomplete state merges distinct subproblems and caches incorrect answers.
Usually fails on: Correctness breaks on cases that differ only in hidden state.
Fix: Define state so each unique subproblem maps to one DP cell.