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)
Solution: DP
dp(x, y, in, ex, mask) := max score at (x, y) with in and ex left and the state of previous row is mask.
Mask is ternary, mask(i) = {0, 1, 2} means {empty, in, ex}, there are at most 3^n = 3^5 = 243 different states.
Total states / Space complexity: O(m*n*3^n*in*ex) = 5*5*3^5*6*6
Space complexity: O(m*n*3^n*in*ex)
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
class Solution { public: int getMaxGridHappiness(int m, int n, int introvertsCount, int extrovertsCount) { constexpr int kMaxStates = 243; int dp[6][6][7][7][kMaxStates]; memset(dp, -1, sizeof(dp)); auto get = [](int val, int i) { return val / static_cast<int>(pow(3, i)) % 3; }; auto update = [](int val, int s) { return (val * 3 + s) % kMaxStates; }; vector<int> init{0, 120, 40}; vector<int> gain{0, -30, 20}; function<int(int,int,int,int,int)> dfs = [&](int x, int y, int in, int ex, int mask) { if (in == 0 && ex == 0) return 0; // Nothing left. if (x == n) { x = 0; ++y; } if (y == m) return 0; // Done. int& ans = dp[x][y][in][ex][mask]; if (ans != -1) return ans; ans = dfs(x + 1, y, in, ex, update(mask, 0)); // Skip current cell. vector<int> counts{0, in, ex}; int up = get(mask, n - 1); int left = get(mask, 0); for (int i = 1; i <= 2; ++i) { if (counts[i] == 0) continue; int s = init[i]; if (y - 1 >= 0 && up) s += gain[i] + gain[up]; if (x - 1 >= 0 && left) s += gain[i] + gain[left]; ans = max(ans, s + dfs(x + 1, y, in - (i==1), ex - (i==2), update(mask, i))); } return ans; }; return dfs(0, 0, introvertsCount, extrovertsCount, 0); } }; |
Python3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# Author: Huahua class Solution: def getMaxGridHappiness(self, m: int, n: int, I: int, E: int) -> int: init, gain = [None, 120, 40], [None, -30, 20] get = lambda val, i: (val // (3 ** i)) % 3 update = lambda val, s: (val * 3 + s) % (3 ** n) @lru_cache(None) def dp(x: int, y: int, i: int, e: int, mask: int) -> int: if x == n: x, y = 0, y + 1 if i + e == 0 or y == m: return 0 ans = dp(x + 1, y, i, e, update(mask, 0)) up, left = get(mask, n - 1), get(mask, 0) for cur, count in enumerate([i, e], 1): if count == 0: continue s = init[cur] if x - 1 >= 0 and left: s += gain[cur] + gain[left] if y - 1 >= 0 and up: s += gain[cur] + gain[up] ans = max(ans, s + dp(x + 1, y, i - (cur == 1), e - (cur == 2), update(mask, cur))) return ans return dp(0, 0, I, E, 0) |
请尊重作者的劳动成果,转载请注明出处!花花保留对文章/视频的所有权利。
如果您喜欢这篇文章/视频,欢迎您捐赠花花。
If you like my articles / videos, donations are welcome.
Be First to Comment