Problem
Farmer John has a rectangular grass pasture with N rows and M columns for the cows to graze on. Each pasture has a certain tastiness value. However, the gen alpha Bessie has gotten quite picky with what she eats.
Given a 2D array (template below) with size NxM, please write functions for the following:
- getTotalGrass()
- Return total sum of all “tastiness values” from the pastures in the 2D array
- maxSquare()
- Because Bessie sometimes likes enjoying square grass patches, she wants to find the best one.
- Returns the maximum sum of tastiness value of a square in the 2D array. (Square could be 1x1, 2x2, 3x3, etc.)
- maxSubarraySum()
- Sometimes, Bessie enjoys eating grass in a line.
- Return the maximum sum of a continuous subarray in this array if it was “flattened” to be a 1D array. In other words, make the 2D array into a 1D array by combining all rows and find the max subarray sum.
For an example case, see below in the code.
Extra Credit Opportunities
Extra Credit 1 (+0.01): What is the time complexity of your maxSquare code? Explain.
Extra Credit 2 (+0.01): This is achieved if you get the optimal complexity for maxPatch.
Extra Credit 3 (+0.01): What is the time complexity of your maxSubarraySum code? Explain.
Extra Credit 4 (+0.01): This is achieved if you get the optimal complexity for maxSubarraySum.
public class GrassPasture {
private int[][] pastures;
public GrassPasture(int[][] pastures) {
this.pastures = pastures;
}
public int getTotalGrass() {
int total = 0;
for (int[] row : pastures) {
for (int value : row) {
total += value;
}
}
return total;
}
public int maxSquare() {
int n = pastures.length;
int m = pastures[0].length;
int maxSum = Integer.MIN_VALUE;
int[][] prefixSum = new int[n + 1][m + 1];
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
prefixSum[i][j] = pastures[i - 1][j - 1] + prefixSum[i - 1][j] + prefixSum[i][j - 1] - prefixSum[i - 1][j - 1];
}
}
for (int size = 1; size <= Math.min(n, m); size++) {
for (int i = size; i <= n; i++) {
for (int j = size; j <= m; j++) {
int sum = prefixSum[i][j] - prefixSum[i - size][j] - prefixSum[i][j - size] + prefixSum[i - size][j - size];
maxSum = Math.max(maxSum, sum);
}
}
}
return maxSum;
}
public int maxSubarraySum() {
int n = pastures.length;
int m = pastures[0].length;
int[] flattened = new int[n * m];
int index = 0;
for (int[] row : pastures) {
for (int value : row) {
flattened[index++] = value;
}
}
int maxSum = Integer.MIN_VALUE;
int currentSum = 0;
for (int value : flattened) {
currentSum = Math.max(value, currentSum + value);
maxSum = Math.max(maxSum, currentSum);
}
return maxSum;
}
public static void main(String[] args) {
int[][] pastures = {
{-3, 6, -1},
{2, -1, 5},
{-9, 4, -1}
};
GrassPasture gp = new GrassPasture(pastures);
System.out.println("Total Tastiness: " + gp.getTotalGrass());
System.out.println("Max Square Sum: " + gp.maxSquare());
System.out.println("Max Subarray Sum: " + gp.maxSubarraySum());
}
}
GrassPasture.main(null);
Total Tastiness: 2
Max Square Sum: 9
Max Subarray Sum: 11