Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit d5243c0

Browse files
committed
Add solution #1659
1 parent 5578cd0 commit d5243c0

File tree

2 files changed

+95
-1
lines changed

2 files changed

+95
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,456 LeetCode solutions in JavaScript
1+
# 1,457 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1279,6 +1279,7 @@
12791279
1656|[Design an Ordered Stream](./solutions/1656-design-an-ordered-stream.js)|Easy|
12801280
1657|[Determine if Two Strings Are Close](./solutions/1657-determine-if-two-strings-are-close.js)|Medium|
12811281
1658|[Minimum Operations to Reduce X to Zero](./solutions/1658-minimum-operations-to-reduce-x-to-zero.js)|Medium|
1282+
1659|[Maximize Grid Happiness](./solutions/1659-maximize-grid-happiness.js)|Hard|
12821283
1668|[Maximum Repeating Substring](./solutions/1668-maximum-repeating-substring.js)|Easy|
12831284
1669|[Merge In Between Linked Lists](./solutions/1669-merge-in-between-linked-lists.js)|Medium|
12841285
1672|[Richest Customer Wealth](./solutions/1672-richest-customer-wealth.js)|Easy|
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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

Comments
 (0)