forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution2.ts
49 lines (49 loc) · 1.54 KB
/
Solution2.ts
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
function getMaxGridHappiness(
m: number,
n: number,
introvertsCount: number,
extrovertsCount: number,
): number {
const p = 3 ** (n - 1);
const h: number[][] = [
[0, 0, 0],
[0, -60, -10],
[0, -10, 40],
];
const memo: number[][][][] = Array(m * n)
.fill(0)
.map(() =>
Array(p * 3)
.fill(0)
.map(() =>
Array(introvertsCount + 1)
.fill(0)
.map(() => Array(extrovertsCount + 1).fill(-1)),
),
);
const dfs = (pos: number, pre: number, ic: number, ec: number): number => {
if (pos === m * n || (ic === 0 && ec === 0)) {
return 0;
}
if (memo[pos][pre][ic][ec] !== -1) {
return memo[pos][pre][ic][ec];
}
let ans = 0;
const up = Math.floor(pre / p);
const left = pos % n == 0 ? 0 : pre % 3;
for (let i = 0; i < 3; ++i) {
if ((i === 1 && ic === 0) || (i === 2 && ec === 0)) {
continue;
}
const cur = (pre % p) * 3 + i;
const a = h[up][i] + h[left][i];
const nic = i === 1 ? ic - 1 : ic;
const nec = i === 2 ? ec - 1 : ec;
const b = dfs(pos + 1, cur, nic, nec);
const c = i === 1 ? 120 : i === 2 ? 40 : 0;
ans = Math.max(ans, a + b + c);
}
return (memo[pos][pre][ic][ec] = ans);
};
return dfs(0, 0, introvertsCount, extrovertsCount);
}