给定一个NxNxN二进制数组(只包含0或1),我们如何获得具有非平凡解的最大长方体,即O(N^3)?
--
这是与Find largest rectangle containing only zeros in an N×N binary matrix相同的问题,但在更高的维度上。另外,在我的例子中,最大的矩形可以“穿过”数组的边缘,即空间就像2D矩阵的环面。
对于二维数组,如果条目为:
00111
00111
11000
00000
00111
“X”所描述的解决方案是
00XXX
00XXX
11000
00000
00XXX
我已经完成了NxN二进制数组的计算,并通过遵循http://tech-queries.blogspot.de/2011/03/maximum-area-rectangle-in-histogram.html中的思想找到了O(N^2)中最大矩形问题的解决方案。但我不知道如何将其应用于3D数组。
--
以3x3x3数组为例,其中解决方案“跨越边缘”:
111
100
011
111
001
111
011
110
011
解决方案应该是:
1XX
100
0XX
1XX
001
1XX
0XX
110
0XX
发布于 2012-03-29 20:09:53
这里只有O(N^4)。
让我们假设你在bool cuboidNN中存储立方体;
bool array2d[N][N];
for(int x_min = 0; x_min < N; x_min++) {
//initializing array2d
for(int y = 0; y < N; y++) {
for(int z = 0; z < N; z++) {
array2d[y][z] = true;
}
}
//computation
for(int x_max = x_min; x_max < N; x_max++) {
// now we want to find largest cube that
// X coordinates are equal to x_min and x_max
// cells at y,z can be used in cube if and only if
// there are only 1's in cuboid[x][y][z] where x_min <= x <= x_max
// so lets compute for each cell in array2d,
// if are only 1's in cuboid[x][y][z] where x_min <= x <= x_max
for(int y = 0; y < N; y++) {
for(int z = 0; z < N; z++) {
array2d[y][z] &= cubiod[x_max][y][z];
}
}
//you already know how to find largest rectangle in 2d in O(N^2)
local_volume = (x_max - x_min + 1) * find_largest_area(array2d);
largest_volume = max(largest_volumne, local_volume);
}
}
您可以使用相同的技巧,在X维度上计算最佳解。只要将问题简化到X-1维即可。复杂度: O(N^(2*X-2))。
https://stackoverflow.com/questions/9923601
复制相似问题