Skip to main content

Command Palette

Search for a command to run...

Solving Leetcode Interviews in Seconds with AI: Maximum Value Sum by Placing Three Rooks II

Updated
3 min read

Introduction

In this blog post, we will explore how to solve the LeetCode problem "3257" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.

Problem Statement

You are given a m x n 2D array board representing a chessboard, where board[i][j] represents the value of the cell (i, j). Rooks in the same row or column attack each other. You need to place three rooks on the chessboard such that the rooks do not attack each other. Return the maximum sum of the cell values on which the rooks are placed. Example 1: Input: board = [[-3,1,1,1],[-3,1,-3,1],[-3,2,1,1]] Output: 4 Explanation: We can place the rooks in the cells (0, 2), (1, 3), and (2, 1) for a sum of 1 + 1 + 2 = 4. Example 2: Input: board = [[1,2,3],[4,5,6],[7,8,9]] Output: 15 Explanation: We can place the rooks in the cells (0, 0), (1, 1), and (2, 2) for a sum of 1 + 5 + 9 = 15. Example 3: Input: board = [[1,1,1],[1,1,1],[1,1,1]] Output: 3 Explanation: We can place the rooks in the cells (0, 2), (1, 1), and (2, 0) for a sum of 1 + 1 + 1 = 3. Constraints: 3 <= m == board.length <= 500 3 <= n == board[i].length <= 500 -109 <= board[i][j] <= 109

Explanation

Here's the breakdown of the solution:

  • Key Idea: The problem requires selecting three cells on a chessboard such that no two cells share the same row or column, maximizing the sum of cell values. We can solve this using dynamic programming (DP). The state dp[i][mask] represents the maximum sum achievable by placing i rooks on the first i rows of the board such that the columns used by these i rooks are represented by the bitmask mask.

  • DP Transitions: For each row i and each possible column configuration represented by mask, we iterate through all columns j. If column j is not yet occupied (i.e., the j-th bit in mask is not set), we consider placing a rook in cell (i, j). The updated dp value will be dp[i][mask | (1 << j)] = max(dp[i][mask | (1 << j)], dp[i-1][mask] + board[i][j]).

  • Base Case: dp[0][0] = 0, indicating that with 0 rooks placed and no columns used, the sum is 0.

  • Complexity:

    • Runtime: O(m * 2n * n), where m is the number of rows and n is the number of columns.
    • Storage: O(m * 2n)

Code

    def max_rook_sum(board):
    m = len(board)
    n = len(board[0])

    dp = {}

    def solve(row, mask, num_rooks):
        if num_rooks == 3:
            return 0 if row == m else float('-inf')

        if row == m:
            return float('-inf') if num_rooks < 3 else 0

        if (row, mask, num_rooks) in dp:
            return dp[(row, mask, num_rooks)]

        max_sum = float('-inf')
        #Option 1: Don't place a rook in this row
        max_sum = max(max_sum, solve(row + 1, mask, num_rooks))

        #Option 2: Try all columns
        for col in range(n):
            if (mask & (1 << col)) == 0:
                new_mask = mask | (1 << col)
                max_sum = max(max_sum, board[row][col] + solve(row + 1, new_mask, num_rooks + 1))

        dp[(row, mask, num_rooks)] = max_sum
        return max_sum

    return solve(0, 0, 0)

More from this blog

C

Chatmagic blog

2894 posts