MCPcopy Create free account
hub / github.com/careercup/ctci / getMaxMatrix

Method getMaxMatrix

java/Chapter 18/Question18_12/QuestionB.java:6–26  ·  view source on GitHub ↗
(int[][] original)

Source from the content-addressed store, hash-verified

4
5public class QuestionB {
6 public static int getMaxMatrix(int[][] original) {
7 int maxArea = Integer.MIN_VALUE; // Important! Max could be < 0
8 int rowCount = original.length;
9 int columnCount = original[0].length;
10 int[][] matrix = precomputeMatrix(original);
11 for (int row1 = 0; row1 < rowCount; row1++) {
12 for (int row2 = row1; row2 < rowCount; row2++) {
13 for (int col1 = 0; col1 < columnCount; col1++) {
14 for (int col2 = col1; col2 < columnCount; col2++) {
15 int sum = computeSum(matrix, row1, row2, col1, col2);
16 if (sum > maxArea) {
17 System.out.println("New Max of " + sum + ": (rows " + row1 + " to " + row2 + ") and (columns " + col1 + " to " + col2 + ")");
18 maxArea = sum;
19 }
20
21 }
22 }
23 }
24 }
25 return maxArea;
26 }
27
28 private static int[][] precomputeMatrix(int[][] matrix) {
29 int[][] sumMatrix = new int[matrix.length][matrix[0].length];

Callers 1

mainMethod · 0.95

Calls 2

precomputeMatrixMethod · 0.95
computeSumMethod · 0.95

Tested by

no test coverage detected