Skip to content

Latest commit

 

History

History
120 lines (88 loc) · 8.04 KB

File metadata and controls

120 lines (88 loc) · 8.04 KB

Range Sum Query 2D - Immutable - Leetcode 304

Disclaimer: This is a personal summary and interpretation based on a YouTube video. It is not official material and not endorsed by the original creator. All rights remain with the respective creators.

This document summarizes the key takeaways from the video. I highly recommend watching the full video for visual context and coding demonstrations.

Before You Get Started

  • I summarize key points to help you learn and review quickly.
  • Simply click on Ask AI links to dive into any topic you want.

AI-Powered buttons

Teach Me: 5 Years Old | Beginner | Intermediate | Advanced | (reset auto redirect)

Learn Differently: Analogy | Storytelling | Cheatsheet | Mindmap | Flashcards | Practical Projects | Code Examples | Common Mistakes

Check Understanding: Generate Quiz | Interview Me | Refactor Challenge | Assessment Rubric | Next Steps

Problem Introduction

Rust handles memory safety by using ownership and borrowing rules, but here we're focusing on a 2D matrix sum query. You get a 2D matrix and need to compute the sum of any submatrix defined by its top-left (row1, col1) and bottom-right (row2, col2) coordinates. Multiple queries make efficiency key.

Key takeaway: The goal is constant-time queries after preprocessing, building on prefix sum ideas to avoid repeated nested loops for each query.

Ask AI: 2D Range Sum Query

Naive Approach

To compute a submatrix sum, loop through rows from row1 to row2, and for each row, loop through columns from col1 to col2, adding up values. This works but gets slow with many queries, as each could take O(rows * cols) time in the worst case.

Key example: For a submatrix from (1,2) to (2,4), nest loops to sum elements row by row.

Ask AI: Naive Submatrix Sum

1D Prefix Sums Review

In 1D, precompute a prefix array where prefix[i] is the sum from index 0 to i-1. Then, sum from left to right is prefix[right+1] - prefix[left]. This takes O(n) preprocessing and O(1) per query, efficient for many subarray sums.

Key example: For array [3,3,1,4,2], prefix becomes [0,3,6,7,11,13]. Sum from index 1 to 3: 6 +1 +4 =11, or prefix[4] - prefix[1] =11-3=8? Wait, adjust indices properly.

Ask AI: 1D Prefix Sums

Extending Prefix Sums to 2D

In 2D, create a prefix matrix where prefix[r][c] holds the sum from (0,0) to (r-1,c-1). To get a submatrix sum, use inclusion-exclusion: prefix[row2+1][col2+1] - prefix[row1][col2+1] - prefix[row2+1][col1] + prefix[row1][col1]. This subtracts overlapping regions correctly.

Key takeaway: Precomputing the 2D prefix takes O(m*n) time, enabling O(1) queries by treating each bottom-right as a potential submatrix end.

Ask AI: 2D Prefix Sums

Handling Edge Cases with Padding

To avoid out-of-bounds checks for queries at the matrix edges, make the prefix matrix (rows+1) x (cols+1) initialized to zeros. This way, subtracting from row-1 or col-1 hits zeros naturally, simplifying code without if-statements.

Key example: For a 3x4 matrix, prefix is 4x5 with top row and left column as zeros.

Ask AI: Prefix Matrix Padding

Building the Prefix Sum Matrix

In the constructor, get matrix dimensions m (rows) and n (cols). Create sum_mat as [[0] * (n+1) for _ in range(m+1)]. Then, for r in range(m), for c in range(n): sum_mat[r+1][c+1] = matrix[r][c] + sum_mat[r+1][c] + sum_mat[r][c+1] - sum_mat[r][c]. This accumulates row-wise and column-wise sums.

self.sum_mat = [[0] * (cols + 1) for _ in range(rows + 1)]
for r in range(rows):
    for c in range(cols):
        self.sum_mat[r + 1][c + 1] = (
            matrix[r][c] +
            self.sum_mat[r + 1][c] +
            self.sum_mat[r][c + 1] -
            self.sum_mat[r][c]
        )

Ask AI: Building 2D Prefix Matrix

Querying the Sum Region

In sumRegion, add 1 to all inputs for offset: r1 +=1, c1 +=1, r2 +=1, c2 +=1. Then return sum_mat[r2][c2] - sum_mat[r1-1][c2] - sum_mat[r2][c1-1] + sum_mat[r1-1][c1-1]. This gives the exact submatrix sum in O(1) time.

def sumRegion(self, row1: int, col1: int, row2: int, col2: int) -> int:
    r1, c1, r2, c2 = row1 + 1, col1 + 1, row2 + 1, col2 + 1
    return (
        self.sum_mat[r2][c2] -
        self.sum_mat[r1 - 1][c2] -
        self.sum_mat[r2][c1 - 1] +
        self.sum_mat[r1 - 1][c1 - 1]
    )

Ask AI: 2D Sum Query Implementation


About the summarizer

I'm Ali Sol, a Backend Developer. Learn more: