|
| 1 | +/** |
| 2 | + * 1659. Maximize Grid Happiness |
| 3 | + * https://leetcode.com/problems/maximize-grid-happiness/ |
| 4 | + * Difficulty: Hard |
| 5 | + * |
| 6 | + * You are given four integers, m, n, introvertsCount, and extrovertsCount. You have an m x n grid, |
| 7 | + * and there are two types of people: introverts and extroverts. There are introvertsCount |
| 8 | + * introverts and extrovertsCount extroverts. |
| 9 | + * |
| 10 | + * You should decide how many people you want to live in the grid and assign each of them one grid |
| 11 | + * cell. Note that you do not have to have all the people living in the grid. |
| 12 | + * |
| 13 | + * The happiness of each person is calculated as follows: |
| 14 | + * - Introverts start with 120 happiness and lose 30 happiness for each neighbor (introvert or |
| 15 | + * extrovert). |
| 16 | + * - Extroverts start with 40 happiness and gain 20 happiness for each neighbor (introvert or |
| 17 | + * extrovert). |
| 18 | + * |
| 19 | + * Neighbors live in the directly adjacent cells north, east, south, and west of a person's cell. |
| 20 | + * |
| 21 | + * The grid happiness is the sum of each person's happiness. Return the maximum possible grid |
| 22 | + * happiness. |
| 23 | + */ |
| 24 | + |
| 25 | +/** |
| 26 | + * @param {number} m |
| 27 | + * @param {number} n |
| 28 | + * @param {number} introvertsCount |
| 29 | + * @param {number} extrovertsCount |
| 30 | + * @return {number} |
| 31 | + */ |
| 32 | +var getMaxGridHappiness = function(m, n, introvertsCount, extrovertsCount) { |
| 33 | + const memo = new Map(); |
| 34 | + const maxState = Math.pow(3, n); |
| 35 | + |
| 36 | + return calculateHappiness(0, 0, introvertsCount, extrovertsCount, 0); |
| 37 | + |
| 38 | + function calculateHappiness(row, col, introverts, extroverts, prevState) { |
| 39 | + if (row === m || (introverts === 0 && extroverts === 0)) return 0; |
| 40 | + if (col === n) return calculateHappiness(row + 1, 0, introverts, extroverts, prevState); |
| 41 | + |
| 42 | + const key = `${row},${col},${introverts},${extroverts},${prevState}`; |
| 43 | + if (memo.has(key)) return memo.get(key); |
| 44 | + |
| 45 | + const nextState = (prevState * 3) % maxState; |
| 46 | + let maxHappiness = calculateHappiness(row, col + 1, introverts, extroverts, nextState); |
| 47 | + |
| 48 | + if (introverts > 0) { |
| 49 | + let happiness = 120; |
| 50 | + const leftNeighbor = col > 0 ? prevState % 3 : 0; |
| 51 | + const upNeighbor = row > 0 ? Math.floor(prevState / Math.pow(3, n - 1)) % 3 : 0; |
| 52 | + |
| 53 | + if (leftNeighbor) happiness -= 30; |
| 54 | + if (upNeighbor) happiness -= 30; |
| 55 | + |
| 56 | + let neighborHappiness = 0; |
| 57 | + if (leftNeighbor === 1) neighborHappiness -= 30; |
| 58 | + if (leftNeighbor === 2) neighborHappiness += 20; |
| 59 | + if (upNeighbor === 1) neighborHappiness -= 30; |
| 60 | + if (upNeighbor === 2) neighborHappiness += 20; |
| 61 | + |
| 62 | + maxHappiness = Math.max( |
| 63 | + maxHappiness, |
| 64 | + happiness + neighborHappiness |
| 65 | + + calculateHappiness(row, col + 1, introverts - 1, extroverts, nextState + 1) |
| 66 | + ); |
| 67 | + } |
| 68 | + |
| 69 | + if (extroverts > 0) { |
| 70 | + let happiness = 40; |
| 71 | + const leftNeighbor = col > 0 ? prevState % 3 : 0; |
| 72 | + const upNeighbor = row > 0 ? Math.floor(prevState / Math.pow(3, n - 1)) % 3 : 0; |
| 73 | + |
| 74 | + if (leftNeighbor) happiness += 20; |
| 75 | + if (upNeighbor) happiness += 20; |
| 76 | + |
| 77 | + let neighborHappiness = 0; |
| 78 | + if (leftNeighbor === 1) neighborHappiness -= 30; |
| 79 | + if (leftNeighbor === 2) neighborHappiness += 20; |
| 80 | + if (upNeighbor === 1) neighborHappiness -= 30; |
| 81 | + if (upNeighbor === 2) neighborHappiness += 20; |
| 82 | + |
| 83 | + maxHappiness = Math.max( |
| 84 | + maxHappiness, |
| 85 | + happiness + neighborHappiness |
| 86 | + + calculateHappiness(row, col + 1, introverts, extroverts - 1, nextState + 2) |
| 87 | + ); |
| 88 | + } |
| 89 | + |
| 90 | + memo.set(key, maxHappiness); |
| 91 | + return maxHappiness; |
| 92 | + } |
| 93 | +}; |
0 commit comments