-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy path1659-maximize-grid-happiness.js
93 lines (79 loc) · 3.32 KB
/
1659-maximize-grid-happiness.js
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/**
* 1659. Maximize Grid Happiness
* https://leetcode.com/problems/maximize-grid-happiness/
* Difficulty: Hard
*
* 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.
*/
/**
* @param {number} m
* @param {number} n
* @param {number} introvertsCount
* @param {number} extrovertsCount
* @return {number}
*/
var getMaxGridHappiness = function(m, n, introvertsCount, extrovertsCount) {
const memo = new Map();
const maxState = Math.pow(3, n);
return calculateHappiness(0, 0, introvertsCount, extrovertsCount, 0);
function calculateHappiness(row, col, introverts, extroverts, prevState) {
if (row === m || (introverts === 0 && extroverts === 0)) return 0;
if (col === n) return calculateHappiness(row + 1, 0, introverts, extroverts, prevState);
const key = `${row},${col},${introverts},${extroverts},${prevState}`;
if (memo.has(key)) return memo.get(key);
const nextState = (prevState * 3) % maxState;
let maxHappiness = calculateHappiness(row, col + 1, introverts, extroverts, nextState);
if (introverts > 0) {
let happiness = 120;
const leftNeighbor = col > 0 ? prevState % 3 : 0;
const upNeighbor = row > 0 ? Math.floor(prevState / Math.pow(3, n - 1)) % 3 : 0;
if (leftNeighbor) happiness -= 30;
if (upNeighbor) happiness -= 30;
let neighborHappiness = 0;
if (leftNeighbor === 1) neighborHappiness -= 30;
if (leftNeighbor === 2) neighborHappiness += 20;
if (upNeighbor === 1) neighborHappiness -= 30;
if (upNeighbor === 2) neighborHappiness += 20;
maxHappiness = Math.max(
maxHappiness,
happiness + neighborHappiness
+ calculateHappiness(row, col + 1, introverts - 1, extroverts, nextState + 1)
);
}
if (extroverts > 0) {
let happiness = 40;
const leftNeighbor = col > 0 ? prevState % 3 : 0;
const upNeighbor = row > 0 ? Math.floor(prevState / Math.pow(3, n - 1)) % 3 : 0;
if (leftNeighbor) happiness += 20;
if (upNeighbor) happiness += 20;
let neighborHappiness = 0;
if (leftNeighbor === 1) neighborHappiness -= 30;
if (leftNeighbor === 2) neighborHappiness += 20;
if (upNeighbor === 1) neighborHappiness -= 30;
if (upNeighbor === 2) neighborHappiness += 20;
maxHappiness = Math.max(
maxHappiness,
happiness + neighborHappiness
+ calculateHappiness(row, col + 1, introverts, extroverts - 1, nextState + 2)
);
}
memo.set(key, maxHappiness);
return maxHappiness;
}
};