Solving Leetcode Interviews in Seconds with AI: Maximum Score From Grid Operations
Introduction
In this blog post, we will explore how to solve the LeetCode problem "3225" 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 2D matrix grid of size n x n. Initially, all cells of the grid are colored white. In one operation, you can select any cell of indices (i, j), and color black all the cells of the jth column starting from the top row down to the ith row. The grid score is the sum of all grid[i][j] such that cell (i, j) is white and it has a horizontally adjacent black cell. Return the maximum score that can be achieved after some number of operations. Example 1: Input: grid = [[0,0,0,0,0],[0,0,3,0,0],[0,1,0,0,0],[5,0,0,3,0],[0,0,0,0,2]] Output: 11 Explanation: In the first operation, we color all cells in column 1 down to row 3, and in the second operation, we color all cells in column 4 down to the last row. The score of the resulting grid is grid[3][0] + grid[1][2] + grid[3][3] which is equal to 11. Example 2: Input: grid = [[10,9,0,0,15],[7,1,0,8,0],[5,20,0,11,0],[0,0,0,1,2],[8,12,1,10,3]] Output: 94 Explanation: We perform operations on 1, 2, and 3 down to rows 1, 4, and 0, respectively. The score of the resulting grid is grid[0][0] + grid[1][0] + grid[2][1] + grid[4][1] + grid[1][3] + grid[2][3] + grid[3][3] + grid[4][3] + grid[0][4] which is equal to 94. Constraints: 1 <= n == grid.length <= 100 n == grid[i].length 0 <= grid[i][j] <= 109
Explanation
Here's the solution, along with the explanation:
- Dynamic Programming: The core idea is to use dynamic programming to explore all possible combinations of operations.
dp[i][j]stores the maximum score achievable considering columns from 0 toi, wherejrepresents the row up to which columnihas been colored black. - Iteration and Calculation: The code iterates through the columns and for each column, it explores all possible cutoff points (rows up to which to color black). It calculates the score gained by choosing a particular cutoff, considering horizontally adjacent black cells.
Maximization: The DP table is updated to store the maximum possible score at each step, ensuring that the final result
dp[n-1][n-1]holds the maximum score achievable for the entire grid.Runtime Complexity: O(n3), where n is the dimension of the grid. Storage Complexity: O(n2).
Code
def solve():
grid = [[0,0,0,0,0],[0,0,3,0,0],[0,1,0,0,0],[5,0,0,3,0],[0,0,0,0,2]]
n = len(grid)
result = max_grid_score(grid)
print(result)
grid = [[10,9,0,0,15],[7,1,0,8,0],[5,20,0,11,0],[0,0,0,1,2],[8,12,1,10,3]]
n = len(grid)
result = max_grid_score(grid)
print(result)
def max_grid_score(grid):
n = len(grid)
dp = [[0] * n for _ in range(n)]
for i in range(n):
for j in range(n):
if i == 0:
score = 0
for r in range(n):
for c in range(i + 1):
if c == i and r > j:
continue
if c > 0:
if c - 1 >= 0 and r >= 0 and r < n and c -1 >=0 and c-1 < n:
if c == i and r <= j:
continue
if grid[r][c - 1] ==0:
if grid[r][c] > 0 :
continue;
if c - 1 < i or r > j:
if grid[r][c-1] == 0 :
score = score + 0
else :
if grid[r][c] > 0:
score += grid[r][c]
dp[i][j] = score
else:
max_score = 0
for k in range(n):
score = dp[i - 1][k]
for r in range(n):
for c in range(i + 1):
if c == i and r > j:
continue
if c > 0:
if c - 1 >= 0 and r >= 0 and r < n and c -1 >=0 and c-1 < n:
is_black_left = False
if c - 1 < i:
if k >= r :
is_black_left = True
elif c - 1 == i :
if j >= r:
is_black_left = True
if is_black_left:
if c == i and r <= j:
continue
score += grid[r][c]
max_score = max(max_score, score)
dp[i][j] = max_score
return dp[n - 1][n - 1]