Similar Problems

Similar Problems not available

Maximize Grid Happiness - Leetcode Solution

Companies:

LeetCode:  Maximize Grid Happiness Leetcode Solution

Difficulty: Hard

Topics: dynamic-programming bit-manipulation  

The Maximize Grid Happiness problem on LeetCode is a dynamic programming problem that requires you to calculate the maximum happiness that could be achieved by allocating different types of people to the cells of a grid, considering the effects of the nearby cells.

To solve this problem, you need to follow the steps given below:

Step 1: Define the variables and inputs The first step is to define the variables and inputs of the problem. The inputs are the following:

  • n: an integer that represents the size of the grid
  • m: an integer that represents the number of people types (1, 0, or -1)
  • k: an integer that represents the number of cells that can be occupied by people
  • P: an integer that represents the personal happiness that each person brings
  • Q: an integer that represents the happiness that two adjacent cells with 1 bring
  • R: an integer that represents the unhappiness that two adjacent cells with -1 bring

Step 2: Think in terms of subproblems The next step is to think in terms of subproblems. In this case, we can define dp[i][j][mask][cnt] as the maximum happiness that can be obtained for the first i rows of the grid, considering the occupancy of the first j cells, and with a mask that represents whether each cell is occupied or not (1 for occupied, 0 for empty), and cnt represents the number of occupied cells.

Step 3: Define the base case The base case is when you have not occupied any cells yet (cnt=0). In this case, the maximum happiness is 0.

Step 4: Define the transition function The transition function calculates the maximum happiness of the next cell that could be occupied. There are three cases to consider:

  • If the cell is empty, you can put any type of person on it. Thus, you need to consider the three possibilities of putting 1, 0, or -1 on the cell and choose the one that gives the maximum happiness.
  • If the cell is already occupied, you need to consider its effect on the nearby cells and calculate the happiness that could be obtained by replacing the person with another type or leaving the cell empty.
  • If the occupied cells reach the maximum limit (k), you cannot put any more people on the grid. Thus, the maximum happiness is the current value of dp.

Step 5: Return the maximum value Once you have calculated the maximum value of the last cell, return it as the result of the problem.

Here is the code in Python that solves the Maximize Grid Happiness problem on LeetCode based on these steps:

class Solution:
    def getMaxGridHappiness(self, m: int, n: int, introvertsCount: int, extrovertsCount: int) -> int:
        # Define variables and inputs
        P = [-60, 0, 40]
        Q = [[0, -10, 0], [-10, 0, 10], [0, 10, 0]]
        R = [[0, 5, 0], [5, 0, 0], [0, 0, 0]]
        
        # Define subproblems
        dp = [[[[None] * (extrovertsCount + 1) for _ in range(introvertsCount + 1)] for _ in range(1 << (2 * n))] for _ in range(n + 1)]

        # Define base case
        def getHappiness(leftEC, leftIC, mask, row):
            if row == n or leftEC + leftIC == 0:
                return 0
            if dp[row][leftEC][leftIC][mask] is not None:
                return dp[row][leftEC][leftIC][mask]

            # Calculate the happiness of the next cell that could be occupied
            ans = 0
            for person in range(3):
                cur = P[person]
                if person == 1:
                    if mask & (1 << (2 * n - 2)):
                        cur = 0
                    elif mask & 1 << (2 * n - 1):
                        cur = -10
                elif person == 2:
                    if mask & (1 << (2 * n - 1)):
                        cur = 0
                    elif mask & (1 << (2 * n - 2)):
                        cur = -10
                if row > 0:
                    last = mask >> (2 * n - 2)
                    if last == 1:
                        cur += Q[person][0]
                    elif last == 2:
                        cur += R[person][0]
                if leftEC > 0:
                    cur += Q[person][1]
                    ans = max(ans, cur + getHappiness(leftEC - 1, leftIC, (mask << 2) & ((1 << (2 * n)) - 1), row))
                if leftIC > 0:
                    cur += R[person][1]
                    ans = max(ans, cur + getHappiness(leftEC, leftIC - 1, ((mask << 2) & ((1 << (2 * n)) - 1)) | (1 << (2 * n - 2)), row))
                if n - row > 1:
                    last = mask & 3
                    if last == 1:
                        cur += Q[person][2]
                    elif last == 2:
                        cur += R[person][2]
                ans = max(ans, cur + getHappiness(leftEC, leftIC, (mask << 2) & ((1 << (2 * n)) - 1), row + 1))
            dp[row][leftEC][leftIC][mask] = ans
            return ans
        
        # Calculate the maximum value
        return getHappiness(extrovertsCount, introvertsCount, 0, 0)

The time complexity of this solution is O(n * 3^k * 2^(2n)), where n is the number of rows in the grid, k is the number of cells that can be occupied, and 3^k2^(2*n) is the maximum number of subproblems that could be calculated. The space complexity is O(n * (introvertsCount + 1) * (extrovertsCount + 1) * 2^(2 * n)), where introvertsCount and extrovertsCount are the maximum possible number of occupants, and 2^(2 * n) is the maximum number of masks that could be used.

Maximize Grid Happiness Solution Code

1