Logo

dev-resources.site

for different kinds of informations.

Range sum query 2D - Immutable

Published at
1/2/2025
Categories
rangequeries
leetcode
java
dsa
Author
prashantrmishra
Categories
4 categories in total
rangequeries
open
leetcode
open
java
open
dsa
open
Author
15 person written this
prashantrmishra
open
Range sum query 2D - Immutable

Problem

TC: O(n*m) for creating the prefix[][] sum matrix, O(row1+row2) for calculating the sum of the given rectangle
SC: O(n*m) for using the prefix[][] sum matrix

class NumMatrix {
    int prefix[][];
    public NumMatrix(int[][] matrix) {
        prefix = new int[matrix.length][matrix[0].length];
        for(int i=0;i<matrix.length;i++){
            int current = 0;
            for(int j = 0;j<matrix[0].length;j++){
                current+=matrix[i][j];
                prefix[i][j] = current;
            }
        }
    }

    public int sumRegion(int row1, int col1, int row2, int col2) {
        int sum =0;
        for(int i =row1;i<=row2;i++){
            int right = prefix[i][col2];
            int left = (col1>0) ? prefix[i][col1-1] : 0;
            sum+=right-left;
        }
        return sum;
    }
}

/**
 * Your NumMatrix object will be instantiated and called as such:
 * NumMatrix obj = new NumMatrix(matrix);
 * int param_1 = obj.sumRegion(row1,col1,row2,col2);
 */
Enter fullscreen mode Exit fullscreen mode
leetcode Article's
30 articles in total
Favicon
Neetcode Roadmap Part 1
Favicon
2429. Minimize XOR
Favicon
A tΓ©cnica dos dois ponteiros
Favicon
2657. Find the Prefix Common Array of Two Arrays
Favicon
Time Complexity, Big-O for Beginners
Favicon
LeetCode Challenge: 383. Ransom Note - JavaScript Solution πŸš€
Favicon
3223. Minimum Length of String After Operations
Favicon
Leet code
Favicon
2116. Check if a Parentheses String Can Be Valid
Favicon
LeetCode Challenge: 73. Set Matrix Zeroes - JavaScript Solution πŸš€
Favicon
LeetCode Challenge: 290. Word Pattern - JavaScript Solution πŸš€
Favicon
LeetCode Challenge: 205. Isomorphic Strings - JavaScript Solution πŸš€
Favicon
Leetcode: 73 Set Matrix Zeroes
Favicon
LeetCode Challenge: 36.Valid Sudoku - JavaScript Solution πŸš€
Favicon
Count prefix and suffix I and II
Favicon
Leetcode Blind 75
Favicon
Rabin Karp (hashing) String pattern matching
Favicon
Leetcode β€” 2942. Find Words Containing Character
Favicon
Automating Your LeetCode Journey: Building an Enterprise-Grade LeetCode to GitHub Sync System
Favicon
Understanding the XOR Operator: A Powerful Tool in Computing
Favicon
Kadane's Algorithm: Leetcode 53 Maximum subarray
Favicon
1768. Merge Strings Alternately
Favicon
Find all anagrams in the string[Fixed Window pattern]
Favicon
No of ways to split Array
Favicon
Leetcode β€” 3289. The Two Sneaky Numbers of Digitville
Favicon
Range sum query 2D - Immutable
Favicon
Range Sum Query - Immutable
Favicon
Count vowel strings in ranges
Favicon
Yay! Reached 1035+ days Daily Coding Streak on Leetcode!
Favicon
Leetcode 75. Sort Colors

Featured ones: