# 1659. Maximize Grid Happiness

## Description

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.

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)

## Solutions

• 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;
}
}

• class Solution {
public:
int getMaxGridHappiness(int m, int n, int introvertsCount, int extrovertsCount) {
int mx = pow(3, n);
int f[mx];
int g[mx][mx];
int bits[mx][n];
int ix[mx];
int ex[mx];
int memo[m][mx][introvertsCount + 1][extrovertsCount + 1];
int h[3][3] = { {0, 0, 0}, {0, -60, -10}, {0, -10, 40} };
memset(f, 0, sizeof(f));
memset(g, 0, sizeof(g));
memset(bits, 0, sizeof(bits));
memset(ix, 0, sizeof(ix));
memset(ex, 0, sizeof(ex));
memset(memo, -1, sizeof(memo));
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) {
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]];
}
}
}
function<int(int, int, int, 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] != -1) {
return memo[i][pre][ic][ec];
}
int ans = 0;
for (int 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]));
}
}
return memo[i][pre][ic][ec] = ans;
};
return dfs(0, 0, introvertsCount, extrovertsCount);
}
};

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


• 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)
}

• 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);
}