1659. Maximize Grid Happiness Hard
1/**
2 * [1659] Maximize Grid Happiness
3 *
4 * You are given four integers, m, n, introvertsCount, and extrovertsCount. You have an m x n grid, and there are two types of people: introverts and extroverts. There are introvertsCount introverts and extrovertsCount extroverts.
5 * You should decide how many people you want to live in the grid and assign each of them one grid cell. Note that you do not have to have all the people living in the grid.
6 * The happiness of each person is calculated as follows:
7 *
8 * Introverts start with 120 happiness and lose 30 happiness for each neighbor (introvert or extrovert).
9 * Extroverts start with 40 happiness and gain 20 happiness for each neighbor (introvert or extrovert).
10 *
11 * Neighbors live in the directly adjacent cells north, east, south, and west of a person's cell.
12 * The grid happiness is the sum of each person's happiness. Return the maximum possible grid happiness.
13 *
14 * Example 1:
15 * <img alt="" src="https://assets.leetcode.com/uploads/2020/11/05/grid_happiness.png" style="width: 261px; height: 121px;" />
16 * Input: m = 2, n = 3, introvertsCount = 1, extrovertsCount = 2
17 * Output: 240
18 * Explanation: Assume the grid is 1-indexed with coordinates (row, column).
19 * We can put the introvert in cell (1,1) and put the extroverts in cells (1,3) and (2,3).
20 * - Introvert at (1,1) happiness: 120 (starting happiness) - (0 * 30) (0 neighbors) = 120
21 * - Extrovert at (1,3) happiness: 40 (starting happiness) + (1 * 20) (1 neighbor) = 60
22 * - Extrovert at (2,3) happiness: 40 (starting happiness) + (1 * 20) (1 neighbor) = 60
23 * The grid happiness is 120 + 60 + 60 = 240.
24 * The above figure shows the grid in this example with each person's happiness. The introvert stays in the light green cell while the extroverts live on the light purple cells.
25 *
26 * Example 2:
27 *
28 * Input: m = 3, n = 1, introvertsCount = 2, extrovertsCount = 1
29 * Output: 260
30 * Explanation: Place the two introverts in (1,1) and (3,1) and the extrovert at (2,1).
31 * - Introvert at (1,1) happiness: 120 (starting happiness) - (1 * 30) (1 neighbor) = 90
32 * - Extrovert at (2,1) happiness: 40 (starting happiness) + (2 * 20) (2 neighbors) = 80
33 * - Introvert at (3,1) happiness: 120 (starting happiness) - (1 * 30) (1 neighbor) = 90
34 * The grid happiness is 90 + 80 + 90 = 260.
35 *
36 * Example 3:
37 *
38 * Input: m = 2, n = 2, introvertsCount = 4, extrovertsCount = 0
39 * Output: 240
40 *
41 *
42 * Constraints:
43 *
44 * 1 <= m, n <= 5
45 * 0 <= introvertsCount, extrovertsCount <= min(m * n, 6)
46 *
47 */
48pub struct Solution {}
49
50// problem: https://leetcode.com/problems/maximize-grid-happiness/
51// discuss: https://leetcode.com/problems/maximize-grid-happiness/discuss/?currentPage=1&orderBy=most_votes&query=
52
53// submission codes start here
54
55impl Solution {
56 pub fn get_max_grid_happiness(m: i32, n: i32, introverts_count: i32, extroverts_count: i32) -> i32 {
57 0
58 }
59}
60
61// submission codes end
62
63#[cfg(test)]
64mod tests {
65 use super::*;
66
67 #[test]
68 fn test_1659() {
69 }
70}
71
Back
© 2025 bowen.ge All Rights Reserved.