Skip to content

Commit 77f869f

Browse files
authored
Update 1659-maximize-grid-happiness.js
1 parent 82c998e commit 77f869f

File tree

1 file changed

+43
-42
lines changed

1 file changed

+43
-42
lines changed

1659-maximize-grid-happiness.js

+43-42
Original file line numberDiff line numberDiff line change
@@ -5,47 +5,48 @@
55
* @param {number} extrovertsCount
66
* @return {number}
77
*/
8-
const getMaxGridHappiness = function (m, n, introvertsCount, extrovertsCount) {
9-
const grid = Array.from({ length: m }, () => Array(n).fill(0))
10-
return dfs(grid, 0, 0, introvertsCount, extrovertsCount, 0)
11-
12-
function dfs(grid, row, col, int, ex, happy) {
13-
const M = grid.length,
14-
N = grid[0].length
15-
if (row == M - 1 && col == N) return happy
16-
if (col == N) {
17-
col = 0
18-
++row
19-
}
20-
let res = dfs(grid, row, col + 1, int, ex, happy)
21-
if (int > 0) {
22-
grid[row][col] = 1
23-
let h = happy + 120
24-
if (row > 0 && grid[row - 1][col] != 0) {
25-
if (grid[row - 1][col] == 1) h += -30 - 30
26-
else h += 20 - 30
27-
}
28-
if (col > 0 && grid[row][col - 1] != 0) {
29-
if (grid[row][col - 1] == 1) h += -30 - 30
30-
else h += 20 - 30
31-
}
32-
res = Math.max(res, dfs(grid, row, col + 1, int - 1, ex, h))
33-
grid[row][col] = 0
34-
}
35-
if (ex > 0) {
36-
grid[row][col] = 2
37-
let h = happy + 40
38-
if (row > 0 && grid[row - 1][col] != 0) {
39-
if (grid[row - 1][col] == 1) h += 20 - 30
40-
else h += 20 + 20
41-
}
42-
if (col > 0 && grid[row][col - 1] != 0) {
43-
if (grid[row][col - 1] == 1) h += 20 - 30
44-
else h += 20 + 20
45-
}
46-
res = Math.max(res, dfs(grid, row, col + 1, int, ex - 1, h))
47-
grid[row][col] = 0
48-
}
49-
return res
8+
const getMaxGridHappiness = (m, n, introvertsCount, extrovertsCount) => {
9+
const state = '0'.repeat(n)
10+
const memo = new Map()
11+
return helper(state, 0, n, m, introvertsCount, extrovertsCount, memo)
12+
}
13+
function helper(state, idx, n, m, inCount, exCount, memo) {
14+
if ((inCount === 0 && exCount === 0) || idx === m * n) return 0
15+
let key = idx + state + inCount + exCount
16+
if (memo.has(key)) return memo.get(key)
17+
const r = (idx / n) >> 0,
18+
c = idx % n
19+
let best = 0
20+
if (inCount !== 0) {
21+
let score = 120
22+
if (r > 0) score = calc(state.charAt(0) - '0', 1, score)
23+
if (c !== 0) score = calc(state.charAt(state.length - 1) - '0', 1, score)
24+
best =
25+
score +
26+
helper(state.slice(1) + '1', idx + 1, n, m, inCount - 1, exCount, memo)
27+
}
28+
if (exCount !== 0) {
29+
let score = 40
30+
if (r > 0) score = calc(state.charAt(0) - '0', 2, score)
31+
if (c !== 0) score = calc(state.charAt(state.length - 1) - '0', 2, score)
32+
best = Math.max(
33+
best,
34+
score +
35+
helper(state.slice(1) + '2', idx + 1, n, m, inCount, exCount - 1, memo)
36+
)
5037
}
38+
best = Math.max(
39+
best,
40+
helper(state.slice(1) + '0', idx + 1, n, m, inCount, exCount, memo)
41+
)
42+
memo.set(key, best)
43+
return best
44+
}
45+
46+
function calc(p1, p2, score) {
47+
if (p1 === 1 && p2 === 1) return score - 60
48+
else if (p1 === 2 && p2 === 2) return score + 40
49+
else if (p1 === 1 && p2 === 2) return score - 10
50+
else if (p1 === 2 && p2 === 1) return score - 10
51+
return score
5152
}

0 commit comments

Comments
 (0)