想了一下,还可以优化一点点。
在每次binary之前,用当前最大的面积/当前check的高,来作为high就可以了。
因为high再大的话,即使得出的指小于target,但是面积不会大于当前最大面积。
//area[i][j] is value sum of [0][0]-[i][j];
public int val_cals(a,i,b,j) {
return area[i][j] - area[a][j] - area[i][b] + area[a][j - 1];
}
//This is the calculation of max_area for each right-bottom point (i,j).
for (int a = i - 1; a >= 0; a--) {
if (val_cals(a,i, j-1,j) > target) break;
int low = 0;
int high = j - 1;
while (low <= high) {
int mid = (low + high) / 2;
if (val_cals(a, i, mid, j) > target) low = mid + 1;
else if (val_cals(a, i, mid, j) < target) high = mid - 1;
else {
global_max = Math.max(global_max, (j - mid) * (i - a));
break;
}
}
}
//need to calculate for n*n points to get the global max.
Probably there is some bug.