【发布时间】:2016-10-29 17:51:00
【问题描述】:
假设我们有一个二维数组 A (n X n)。 A 的所有元素都是 O 或 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)时间吗?