简体   繁体   English

如何在计算矩阵和时提高性能

[英]How to improve performance while calculating matrix sum

I was given a task during an interview, where I was given a matrix, now I need to generate another matrix out of it using below formula:我在面试中接到了一项任务,在那里我得到了一个矩阵,现在我需要使用以下公式从中生成另一个矩阵:

Given matrix A[R][C], generate B[R][C]给定矩阵 A[R][C],生成 B[R][C]

val = 0;
for (i = 0; i ≤ xPosition; i += 1) {
    for (j = 0; j ≤ yPosition; j += 1) {
        val = val + a(i, j);
    }
}

B(xPosition,yPosition) = val;

I have come up with below code:我想出了以下代码:

public List<List<Integer>> generate(List<List<Integer>> A) {
        List<List<Integer>> top = new ArrayList<>();

        for (int i = 0; i < A.size(); i++) {
            List<Integer> inner = new ArrayList<>();
            for (int j = 0; j < A.get(0).size(); j++) {
                inner.add(generateValue(A, i, j));
            }
            top.add(inner);
        }
        return top;
    }

    int generateValue(List<List<Integer>> A, int xPosition, int yPosition) {
        int val = 0;
        for (int i = 0; i <= xPosition; i++) {
            for (int j = 0; j <= yPosition; j++) {
                int value = A.get(i).get(j);
                val += value;
            }
        }
        return val;
    }

Sample input :样本输入:

1 2 3
4 5 6

Output :输出 :

1 3 6
5 12 21

How to improve the performance of this logic?如何提高这个逻辑的性能?

mathematicaly for your solution in array b,each element is related to it's previous one.对于数组 b 中的解决方案,每个元素都与它的前一个元素相关。

to improve your code / optimise it you need to see this relation.要改进您的代码/优化它,您需要看到这种关系。 so for every B[i][j] is related to it's previous element and value from the array A.所以对于每个 B[i][j] 都与它的前一个元素和数组 A 中的值有关。

below is solution mathematically,下面是数学上的解决方案,

b[i][j] = b[i-1][j] + a[i][0]+a[i][1] + a[i][2]+...+a[i][y-1] 

for so if you able to implement this, your code will pass all test cases因此,如果您能够实现这一点,您的代码将通过所有测试用例

i am not a java dev, but if you want code, i can write it for you in python我不是 Java 开发人员,但如果你想要代码,我可以用 python 为你编写

The key is to think of this as a dynamic programming problem, assuming that we have already calculated B[x][y] for all 0 <= x < i , 0 <= y < j when we go to calculate B[i][j] .关键是把这看作是一个动态规划问题,假设我们已经计算了B[x][y]对于所有0 <= x < i , 0 <= y < j当我们去计算B[i][j] .

B[i][j] contains the sum of all elements in the submatrix of A that starts at 0, 0 and ends at i, j . B[i][j]包含A的子矩阵中从0, 0开始到i, j结束的所有元素的总和。 Thus B[i-1][j] will contain the sum of the all the elements in this submatrix except the ones in the i th row.因此B[i-1][j]将包含该子矩阵中i行中的元素之外的所有元素的总和。 Similarly, B[i][j-1] will contain the sum of the all the elements in this submatrix except the ones in the j th column.类似地, B[i][j-1]将包含此子矩阵中j列中的元素之外的所有元素的总和。 Adding these two together, we get the sum of all the elements in the submatrix except for element A[i][j] .将这两个加在一起,我们得到子矩阵中除元素A[i][j]之外的所有元素的总和。 However, while doing this we count all the elements from 0, 0 to i-1, j-1 twice, and we have to subtract their sum (which is B[i-1][j-1] ) once so that we only sum them up once in total.但是,在执行此操作时,我们将从0, 0i-1, j-1所有元素计数两次,并且我们必须减去它们的总和(即B[i-1][j-1] )一次,以便我们总共只总结一次。 Then we add the missing element, A[i][j] .然后我们添加缺失的元素A[i][j] Hence因此

B[i][j] = B[i-1][j] + B[i][j-1] - B[i-1][j-1] + A[i][j]

This recursion can now be implemented as a O(RC) dynamic programming algorithm.这种递归现在可以实现为O(RC)动态规划算法。

To help understand this further, consider the following figure representing the submatrix for which we have to find the sum of the elements.为了帮助进一步理解这一点,请考虑下图,该图表示我们必须为其找到元素总和的子矩阵。 The figure is a matrix C[i][j] , where the x, y th element is the number of times we have summed A[x][y] .该图是一个矩阵C[i][j] ,其中第x, y个元素是我们对A[x][y]求和的次数。 In terms of C[i][j] , our end goal ( B[i][j] ) isC[i][j] ,我们的最终目标( B[i][j] )是

1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1

B[i-1][j] corresponds to the matrix C[i-1][j] , which is B[i-1][j]对应于矩阵C[i-1][j] ,即

1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
0 0 0 0 0

B[i][j-1] corresponds to the matrix C[i][j-1] , which is B[i][j-1]对应于矩阵C[i][j-1] ,即

1 1 1 1 0
1 1 1 1 0
1 1 1 1 0
1 1 1 1 0

B[i-1][j] + B[i][j-1] corresponds to the matrix C[i-1][j] + C[i][j-1] , which is B[i-1][j] + B[i][j-1]对应矩阵C[i-1][j] + C[i][j-1] ,即

2 2 2 2 1
2 2 2 2 1
2 2 2 2 1
1 1 1 1 0

B[i-1][j] + B[i][j-1] - B[i-1][j-1] corresponds to the matrix C[i-1][j] + C[i][j-1] - C[i-1][j-1] , which is B[i-1][j] + B[i][j-1] - B[i-1][j-1]对应矩阵C[i-1][j] + C[i][j-1] - C[i-1][j-1] ,即

1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 0

Now B[i-1][j] + B[i][j-1] - B[i-1][j-1] + A[i][j] corresponds to现在B[i-1][j] + B[i][j-1] - B[i-1][j-1] + A[i][j]对应

1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1

which is the same as B[i][j] .B[i][j]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM