假设有一个二维数组A(n X n),其中所有元素都为0或1。另外有一个整数K。我们的任务是找到A中所有可能的“矩形”,其包含总和为K的元素数量。
To give an example , if A =
0 0 1 0
1 0 0 1
1 1 1 1
1 0 0 1 and k=3 ,
0 0 1 0
1 0 0 1 holds the property ,
1 1 1 holds the property ,
1 1 1 holds the property ,
0 0
1 0
1 1 holds the property ,
1 1
1 0 holds the property ,
1 1
0 1 holds the property ,
1
1
1 holds the property
1
1
1 holds the property
除非我漏掉了什么,否则这个例子的答案应该是8。
换句话说,我们需要检查矩阵A中所有可能的矩形,以查看它们元素的总和是否为K。有没有一种比O(n^2 * k^2)更快的方法?
O(n^4)
的时间吗? - Vincent van der Weele