我试图找到子数组左上角的索引,其总和最大。我见过找到最大子数组的算法,但这些算法不适合我的需求,因为我需要在使用算法之前设置子数组的维度。
/**
* Finds the rectangle of height h and width w within the band
* row0 <= row < row0 + h with the most "ink" in it, or the largest sum in it
* @param int[][] image - A 2d array of light intensity values of each pixel in an image
* @param h, w - Dimensions of the specified rectangle with height h and width w
* @param row0 - the index of where it should start constructing rectangles? (I'm not sure)
* @return The index of the leftmost column of the rectangle
*/
private int findHorzPosition(int[][] image, int row0, int h, int w) {
int maxSum = 0;
int maxRow = 0;
for(int p = row0; p <= image.length - 1; p++) {
int[][] tempArr = new int[image.length - row0][image[p].length - 1];
for(int q = 0; q <= image[p].length - 1; q++) {
tempArr[p][q] = image[p][q];
for(int i = 0; i <= tempArr.length - 1; i++) {
int rowSum = 0;
for(int j = 0; j <= tempArr[i].length - 1; j++) {
rowSum += image[i][j];
}
if (rowSum > maxSum) {
maxSum = rowSum;
maxRow = i;
}
}
}
}
return maxRow;
}
这是我拥有的,但我似乎无法让它工作。对我能做些什么有什么建议吗?
千万里不及你
蓝山帝景
随时随地看视频慕课网APP
相关分类