|
1 | 1 | package g1601_1700.s1659_maximize_grid_happiness
|
2 | 2 |
|
3 | 3 | // #Hard #Dynamic_Programming #Bit_Manipulation #Bitmask #Memoization
|
4 |
| -// #2023_06_15_Time_181_ms_(100.00%)_Space_36.9_MB_(100.00%) |
| 4 | +// #2025_04_04_Time_44_ms_(100.00%)_Space_56.67_MB_(100.00%) |
5 | 5 |
|
6 |
| -class Solution { |
7 |
| - private var m = 0 |
8 |
| - private var n = 0 |
9 |
| - private lateinit var dp: Array<Array<Array<Array<IntArray>>>> |
10 |
| - private val notPlace = 0 |
11 |
| - private val intro = 1 |
12 |
| - private val extro = 2 |
13 |
| - private var mod = 0 |
| 6 | +import kotlin.math.max |
14 | 7 |
|
15 |
| - fun getMaxGridHappiness(m: Int, n: Int, introvertsCount: Int, extrovertsCount: Int): Int { |
16 |
| - this.m = m |
17 |
| - this.n = n |
18 |
| - val numOfState = Math.pow(3.0, n.toDouble()).toInt() |
19 |
| - dp = Array(m) { |
20 |
| - Array(n) { |
21 |
| - Array(introvertsCount + 1) { |
22 |
| - Array(extrovertsCount + 1) { IntArray(numOfState) } |
23 |
| - } |
24 |
| - } |
| 8 | +@Suppress("kotlin:S107") |
| 9 | +class Solution { |
| 10 | + private fun maxHappiness( |
| 11 | + index: Int, |
| 12 | + m: Int, |
| 13 | + n: Int, |
| 14 | + introverts: Int, |
| 15 | + extroverts: Int, |
| 16 | + board: Int, |
| 17 | + dp: Array<Array<Array<IntArray>>>, |
| 18 | + tmask: Int, |
| 19 | + ): Int { |
| 20 | + if (index >= m * n) { |
| 21 | + return 0 |
| 22 | + } |
| 23 | + if (dp[index][introverts][extroverts][board] != 0) { |
| 24 | + return dp[index][introverts][extroverts][board] |
| 25 | + } |
| 26 | + var introScore = -1 |
| 27 | + var extroScore = -1 |
| 28 | + if (introverts > 0) { |
| 29 | + val newBoard = ((board shl 2) or INTROVERT) and tmask |
| 30 | + introScore = |
| 31 | + ( |
| 32 | + 120 + |
| 33 | + adjust(board, INTROVERT, n, index) + |
| 34 | + maxHappiness( |
| 35 | + index + 1, |
| 36 | + m, |
| 37 | + n, |
| 38 | + introverts - 1, |
| 39 | + extroverts, |
| 40 | + newBoard, |
| 41 | + dp, |
| 42 | + tmask, |
| 43 | + ) |
| 44 | + ) |
| 45 | + } |
| 46 | + if (extroverts > 0) { |
| 47 | + val newBoard = ((board shl 2) or EXTROVERT) and tmask |
| 48 | + extroScore = |
| 49 | + ( |
| 50 | + 40 + |
| 51 | + adjust(board, EXTROVERT, n, index) + |
| 52 | + maxHappiness( |
| 53 | + index + 1, |
| 54 | + m, |
| 55 | + n, |
| 56 | + introverts, |
| 57 | + extroverts - 1, |
| 58 | + newBoard, |
| 59 | + dp, |
| 60 | + tmask, |
| 61 | + ) |
| 62 | + ) |
25 | 63 | }
|
26 |
| - mod = numOfState / 3 |
27 |
| - return dfs(0, 0, introvertsCount, extrovertsCount, 0) |
| 64 | + val newBoard = ((board shl 2) or NONE) and tmask |
| 65 | + val skip = maxHappiness(index + 1, m, n, introverts, extroverts, newBoard, dp, tmask) |
| 66 | + dp[index][introverts][extroverts][board] = |
| 67 | + max(skip, max(introScore, extroScore)) |
| 68 | + return dp[index][introverts][extroverts][board] |
28 | 69 | }
|
29 | 70 |
|
30 |
| - private fun dfs(x: Int, y: Int, ic: Int, ec: Int, state: Int): Int { |
31 |
| - if (x == m) { |
32 |
| - return 0 |
33 |
| - } else if (y == n) { |
34 |
| - return dfs(x + 1, 0, ic, ec, state) |
35 |
| - } |
36 |
| - if (dp[x][y][ic][ec][state] != 0) { |
37 |
| - return dp[x][y][ic][ec][state] |
| 71 | + private fun adjust(board: Int, thisIs: Int, col: Int, index: Int): Int { |
| 72 | + val shiftBy = 2 * (col - 1) |
| 73 | + var left = board and 0x03 |
| 74 | + if (index % col == 0) { |
| 75 | + left = NONE |
38 | 76 | }
|
39 |
| - // 1 - not place |
40 |
| - var max = dfs(x, y + 1, ic, ec, state % mod * 3) |
41 |
| - val up = state / mod |
42 |
| - val left = state % 3 |
43 |
| - // 2 - place intro |
44 |
| - if (ic > 0) { |
45 |
| - var temp = 120 |
46 |
| - if (x > 0 && up != notPlace) { |
47 |
| - temp -= 30 |
48 |
| - temp += if (up == intro) -30 else 20 |
| 77 | + val up = (board shr shiftBy) and 0x03 |
| 78 | + val combination = intArrayOf(left, up) |
| 79 | + var adjustment = 0 |
| 80 | + for (neighbor in combination) { |
| 81 | + if (neighbor == NONE) { |
| 82 | + continue |
49 | 83 | }
|
50 |
| - if (y > 0 && left != notPlace) { |
51 |
| - temp -= 30 |
52 |
| - temp += if (left == intro) -30 else 20 |
| 84 | + if (neighbor == INTROVERT && thisIs == INTROVERT) { |
| 85 | + adjustment -= 60 |
| 86 | + } else if (neighbor == INTROVERT && thisIs == EXTROVERT) { |
| 87 | + adjustment -= 10 |
| 88 | + } else if (neighbor == EXTROVERT && thisIs == INTROVERT) { |
| 89 | + adjustment -= 10 |
| 90 | + } else if (neighbor == EXTROVERT && thisIs == EXTROVERT) { |
| 91 | + adjustment += 40 |
53 | 92 | }
|
54 |
| - var nextState = state |
55 |
| - nextState %= mod |
56 |
| - nextState *= 3 |
57 |
| - nextState += intro |
58 |
| - max = Math.max(max, temp + dfs(x, y + 1, ic - 1, ec, nextState)) |
59 | 93 | }
|
60 |
| - // 3 - place extro |
61 |
| - if (ec > 0) { |
62 |
| - var temp = 40 |
63 |
| - if (x > 0 && up != notPlace) { |
64 |
| - temp += 20 |
65 |
| - temp += if (up == intro) -30 else 20 |
66 |
| - } |
67 |
| - if (y > 0 && left != notPlace) { |
68 |
| - temp += 20 |
69 |
| - temp += if (left == intro) -30 else 20 |
| 94 | + return adjustment |
| 95 | + } |
| 96 | + |
| 97 | + fun getMaxGridHappiness(m: Int, n: Int, introvertsCount: Int, extrovertsCount: Int): Int { |
| 98 | + val dp = Array<Array<Array<IntArray>>>(m * n) { |
| 99 | + Array<Array<IntArray>>(introvertsCount + 1) { |
| 100 | + Array<IntArray>(extrovertsCount + 1) { IntArray((1 shl (2 * n))) } |
70 | 101 | }
|
71 |
| - var nextState = state |
72 |
| - nextState %= mod |
73 |
| - nextState *= 3 |
74 |
| - nextState += extro |
75 |
| - max = Math.max(max, temp + dfs(x, y + 1, ic, ec - 1, nextState)) |
76 | 102 | }
|
77 |
| - dp[x][y][ic][ec][state] = max |
78 |
| - return max |
| 103 | + val tmask = (1 shl (2 * n)) - 1 |
| 104 | + return maxHappiness(0, m, n, introvertsCount, extrovertsCount, 0, dp, tmask) |
| 105 | + } |
| 106 | + |
| 107 | + companion object { |
| 108 | + private const val NONE = 0 |
| 109 | + private const val INTROVERT = 1 |
| 110 | + private const val EXTROVERT = 2 |
79 | 111 | }
|
80 | 112 | }
|
0 commit comments