Solving Leetcode Interviews in Seconds with AI: Increment Submatrices by One
Introduction
In this blog post, we will explore how to solve the LeetCode problem "2536" 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 positive integer n, indicating that we initially have an n x n 0-indexed integer matrix mat filled with zeroes. You are also given a 2D integer array query. For each query[i] = [row1i, col1i, row2i, col2i], you should do the following operation: Add 1 to every element in the submatrix with the top left corner (row1i, col1i) and the bottom right corner (row2i, col2i). That is, add 1 to mat[x][y] for all row1i <= x <= row2i and col1i <= y <= col2i. Return the matrix mat after performing every query. Example 1: Input: n = 3, queries = [[1,1,2,2],[0,0,1,1]] Output: [[1,1,0],[1,2,1],[0,1,1]] Explanation: The diagram above shows the initial matrix, the matrix after the first query, and the matrix after the second query. - In the first query, we add 1 to every element in the submatrix with the top left corner (1, 1) and bottom right corner (2, 2). - In the second query, we add 1 to every element in the submatrix with the top left corner (0, 0) and bottom right corner (1, 1). Example 2: Input: n = 2, queries = [[0,0,1,1]] Output: [[1,1],[1,1]] Explanation: The diagram above shows the initial matrix and the matrix after the first query. - In the first query we add 1 to every element in the matrix. Constraints: 1 <= n <= 500 1 <= queries.length <= 104 0 <= row1i <= row2i < n 0 <= col1i <= col2i < n
Explanation
Here's the breakdown of the solution:
- Differential Array (Prefix Sum) Technique: Instead of directly updating each element in the submatrix for every query, we use a differential array (also known as a difference matrix). We increment the top-left element, decrement the top-right + 1 element (if it exists), decrement the bottom-left + 1 element (if it exists), and increment the bottom-right + 1 element (if it exists). This allows us to represent the updates efficiently.
Compute Prefix Sums: After processing all the queries, we calculate the prefix sum of the differential array row-wise and then column-wise. This effectively propagates the updates to the correct submatrix elements, resulting in the final matrix.
Runtime & Storage Complexity:
- Runtime: O(n2 + q), where n is the size of the matrix and q is the number of queries.
- Storage: O(n2)
Code
def rangeAddQueries(n: int, queries: list[list[int]]) -> list[list[int]]:
"""
Applies range addition queries to an n x n matrix using the differential array technique.
Args:
n: The size of the matrix.
queries: A list of queries, where each query is a list [row1, col1, row2, col2].
Returns:
The resulting matrix after applying all the queries.
"""
mat = [[0] * n for _ in range(n)]
for row1, col1, row2, col2 in queries:
mat[row1][col1] += 1
if row2 + 1 < n:
mat[row2 + 1][col1] -= 1
if col2 + 1 < n:
mat[row1][col2 + 1] -= 1
if row2 + 1 < n and col2 + 1 < n:
mat[row2 + 1][col2 + 1] += 1
# Calculate prefix sum row-wise
for i in range(n):
for j in range(1, n):
mat[i][j] += mat[i][j - 1]
# Calculate prefix sum column-wise
for j in range(n):
for i in range(1, n):
mat[i][j] += mat[i - 1][j]
return mat