MCPcopy Index your code
hub / github.com/careercup/ctci / maxSubMatrix

Method maxSubMatrix

java/Chapter 18/Question18_12/QuestionC.java:13–35  ·  view source on GitHub ↗
(int[][] matrix)

Source from the content-addressed store, hash-verified

11 }
12
13 public static int maxSubMatrix(int[][] matrix) {
14 int rowCount = matrix.length;
15 int colCount = matrix[0].length;
16
17 int[] partialSum = new int[colCount];
18 int maxSum = 0; // Max sum is an empty matrix
19
20 for (int rowStart = 0; rowStart < rowCount; rowStart++) {
21 clearArray(partialSum);
22
23 for (int rowEnd = rowStart; rowEnd < rowCount; rowEnd++) {
24 for (int i = 0; i < colCount; i++) {
25 partialSum[i] += matrix[rowEnd][i];
26 }
27
28 int tempMaxSum = maxSubArray(partialSum, colCount);
29
30 // if you want to track the coordinates, add code here to do that
31 maxSum = Math.max(maxSum, tempMaxSum);
32 }
33 }
34 return maxSum;
35 }
36
37 public static int maxSubArray(int array[], int N) {
38 int maxSum = 0;

Callers 1

mainMethod · 0.95

Calls 3

clearArrayMethod · 0.95
maxSubArrayMethod · 0.95
maxMethod · 0.80

Tested by

no test coverage detected