zoukankan      html  css  js  c++  java
  • Leetcode 304.二维区域和检索-矩阵不可变

    二维区域和检索 - 矩阵不可变

    给定一个二维矩阵,计算其子矩形范围内元素的总和,该子矩阵的左上角为 (row1, col1) ,右下角为 (row2, col2)。


    上图子矩阵左上角 (row1, col1) = (2, 1) ,右下角(row2, col2) = (4, 3),该子矩形内元素的总和为 8。

    示例:

    给定 matrix = [

    [3, 0, 1, 4, 2],

    [5, 6, 3, 2, 1],

    [1, 2, 0, 1, 5],

    [4, 1, 0, 1, 7],

    [1, 0, 3, 0, 5]

    ]

     

    sumRegion(2, 1, 4, 3) -> 8

    sumRegion(1, 1, 2, 2) -> 11

    sumRegion(1, 2, 2, 4) -> 12

    说明:

    1. 你可以假设矩阵不可变。
    2. 会多次调用 sumRegion 方法
    3. 你可以假设 row1 ≤ row2 且 col1 ≤ col2。

    Approach #4 (Caching Smarter) [Accepted]

    Algorithm

    We used a cumulative sum array in the 1D version. We notice that the cumulative sum is computed with respect to the origin at index 0. Extending this analogy to the 2D case, we could pre-compute a cumulative region sum with respect to the origin at (0,0)(0, 0)(0,0).


    Sum(OD) is the cumulative region sum with respect to the origin at (0, 0).

    How do we derive Sum(ABCD)Sum(ABCD)Sum(ABCD) using the pre-computed cumulative region sum?


    Sum(OB) is the cumulative region sum on top of the rectangle.


    Sum(OC) is the cumulative region sum to the left of the rectangle.


    Sum(OA) is the cumulative region sum to the top left corner of the rectangle.

    Note that the region Sum(OA)Sum(OA)Sum(OA) is covered twice by both Sum(OB)Sum(OB)Sum(OB) and Sum(OC)Sum(OC)Sum(OC). We could use the principle of inclusion-exclusion to calculate Sum(ABCD)Sum(ABCD)Sum(ABCD) as following:

    Sum(ABCD)=Sum(OD)Sum(OB)Sum(OC)+Sum(OA) Sum(ABCD) = Sum(OD) - Sum(OB) - Sum(OC) + Sum(OA) Sum(ABCD)=Sum(OD)Sum(OB)Sum(OC)+Sum(OA)

     1 class NumMatrix {
     2 
     3     private int[][] dp;
     4 
     5     public NumMatrix(int[][] matrix) {
     6         if (matrix.length == 0 || matrix[0].length == 0) return;
     7         dp = new int[matrix.length + 1][matrix[0].length + 1];
     8         for (int r = 0; r < matrix.length; r++) {
     9             for (int c = 0; c < matrix[0].length; c++) {
    10                 dp[r + 1][c + 1] = dp[r + 1][c] + dp[r][c + 1] + matrix[r][c] - dp[r][c];
    11             }
    12         }
    13     }
    14 
    15     public int sumRegion(int row1, int col1, int row2, int col2) {
    16         return dp[row2 + 1][col2 + 1] - dp[row1][col2 + 1] - dp[row2 + 1][col1] + dp[row1][col1];
    17     }
    18 
    19 
    20 }
    21 
    22 /**
    23  * Your NumMatrix object will be instantiated and called as such:
    24  * NumMatrix obj = new NumMatrix(matrix);
    25  * int param_1 = obj.sumRegion(row1,col1,row2,col2);
    26  */
  • 相关阅读:
    单利设计模式
    Jquery中的ajax应用(第九章PPT)
    求Sn=a+aa+aaa+aaaa+aaaaa的前5项之和,其中a是一个数字
    求出0~999之间的所有“水仙花数”并输出
    输出三角和倒三角
    编写程序数一下 1到 100 的所有整数中出现多少次数字9(对整数每一位的提取方法)
    计算1/1-1/2+1/3-1/4+1/5 …… + 1/99
    将数组A中的内容和数组B中的内容进行交换。(数组一样大)
    求两个数的最大公约数(列举法与辗转相除法)
    将三个数从大到小输出
  • 原文地址:https://www.cnblogs.com/kexinxin/p/10235126.html
Copyright © 2011-2022 走看看