Maximize Grid Happiness

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:

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:


Solution:

class Solution {
    Integer[][][][][] dp;
    
    public int getMaxGridHappiness(int m, int n, int introvertsCount, int extrovertsCount) {
        dp = new Integer[m * n][7][7][1 << n][1 << n];
        return dfs(0, m, n, introvertsCount, extrovertsCount, 0, 0);
    }
    
    private int calc(int m, int n, int i, int j, int inMask, int outMask, int delta) {
        int res = 0, up = (1 << (n - 1));
        // left has an introvert person
        if (j > 0 && (inMask & 1) > 0) {
            res += delta - 30;
        }
        // up has an introvert person
        if (i > 0 && (inMask & up) > 0) {
            res += delta - 30;
        }
        // left has an extrovert
        if (j > 0 && (outMask & 1) > 0) {
            res += delta + 20;
        }
        // up has an extrovert
        if (i > 0 && (outMask & up) > 0) {
            res += delta + 20;
        }
        return res;
    }
    
    private int dfs(int p, int m, int n, int in, int ex, int inMask, int outMask) {
        int i = p / n, j = p % n;
        if (i >= m || j >= n) return 0;
        if (dp[p][in][ex][inMask][outMask] != null) return dp[p][in][ex][inMask][outMask];
        int nInMask = (inMask << 1) & ((1 << n) - 1), nOutMask = (outMask << 1) & ((1 << n) - 1);
        // do not place anyone
        int res = dfs(p + 1, m, n, in, ex, nInMask, nOutMask);
        // place introvert at p
        if (in > 0) {
            int diff = 120 + calc(m, n, i, j, inMask, outMask, -30);
            res = Math.max(res, diff + dfs(p + 1, m, n, in - 1, ex, nInMask + 1, nOutMask));
        }
        // place extrovert at p
        if (ex > 0) {
            int diff = 40 + calc(m, n, i, j, inMask, outMask, 20);
            res = Math.max(res, diff + dfs(p + 1, m, n, in, ex - 1, nInMask, nOutMask + 1));
        }
        return dp[p][in][ex][inMask][outMask] = res;
    }
}