|
| 1 | +/* |
| 2 | +https://youtu.be/yCQN096CwWM |
| 3 | +https://github.com/mission-peace/interview/blob/master/src/com/interview/dynamic/SubRectangularMatrixWithMaximumSum.java |
| 4 | +
|
| 5 | +We Use Kadane's Algorithm to solve this problem |
| 6 | +*/ |
| 7 | + |
| 8 | +class MaxSumRectangularSubMatrix{ |
| 9 | + class Result{ |
| 10 | + int maxSum; |
| 11 | + int leftBound; |
| 12 | + int rightBound; |
| 13 | + int lowBound; |
| 14 | + int upBound; |
| 15 | + } |
| 16 | + |
| 17 | + class KadaneResult{ |
| 18 | + int sum; |
| 19 | + int start; //will indicate the start row of the max sum sub matrix |
| 20 | + int end; //will indicate the end row of the max sum sub matrix |
| 21 | + } |
| 22 | + |
| 23 | + |
| 24 | + public Result maxSum(int A[][]){ |
| 25 | + int rows = A.length; |
| 26 | + int cols = A[0].length; |
| 27 | + |
| 28 | + int kadaneCol[] = new int[rows]; |
| 29 | + Result result = new Result(); |
| 30 | + |
| 31 | + //we set a new start boundary for the sub-matrix --> this is the left boundary |
| 32 | + for(int left=0 ; left < cols ; left++){ |
| 33 | + |
| 34 | + //Clear the previous values |
| 35 | + Arrays.fill(kadaneCol,0); //O(n) |
| 36 | + |
| 37 | + //the left boundary is set, but we move the right boundary towards the end of the matrix |
| 38 | + for(int right=left ; right < cols ; right++){ |
| 39 | + |
| 40 | + //add values from the newly shifted right boundary |
| 41 | + //This is done because we are applying Kadane's Algorithm which works on 1D arrays |
| 42 | + //We pass an array(to the kadane's algorithm) containing cummulative sum of each row(which makes the array 1D) |
| 43 | + //Kadane's algo finds the max sum subarray from this array --> This is nothing but the max sum sub matrix |
| 44 | + //left and right are set in this function and Kadane's algo helps to fix the up and low. |
| 45 | + for(int i=0;i<rows;i++){ |
| 46 | + kadaneCol[i] += A[i][right]; |
| 47 | + } |
| 48 | + |
| 49 | + //Update the current result |
| 50 | + KadaneResult kadaneResult = kadane(kadaneCol); |
| 51 | + if(kadaneResult.sum > result.maxSum){ |
| 52 | + result.maxSum = kadaneResult.sum; |
| 53 | + result.leftBound = left; |
| 54 | + result.rightBound = right; |
| 55 | + result.upBound = kadaneResult.start; |
| 56 | + result.lowBound = kadaneResult.end; |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + return result; |
| 62 | + |
| 63 | + } |
| 64 | + |
| 65 | + /* |
| 66 | + Time Complexity - O((col^2)*row) --> cubic |
| 67 | + Space Complexity - O(row) |
| 68 | + */ |
| 69 | + |
| 70 | + public KadaneResult kadane(int A[]){ //to return the maxSum, start and end in linear time |
| 71 | + int n = A.length; |
| 72 | + |
| 73 | + if(n == 0) return 0; |
| 74 | + |
| 75 | + KadaneResult kr = null; |
| 76 | + |
| 77 | + int maxEndingHere = A[0]; |
| 78 | + int maxSoFar = A[0]; |
| 79 | + int maxStart = 0; |
| 80 | + int start = 0; |
| 81 | + int maxEnd = 0; |
| 82 | + |
| 83 | + for(int i=1 ; i < n ; i++){ |
| 84 | + //include current element in the sub-array sum |
| 85 | + maxEndingHere+=A[i]; |
| 86 | + |
| 87 | + //check if starting a new sub-array gives a greater sum than the current sum |
| 88 | + if(maxEndingHere < A[i]){ |
| 89 | + start = i; |
| 90 | + maxEndingHere = A[i]; |
| 91 | + } |
| 92 | + |
| 93 | + //update the overall max-sum |
| 94 | + if(maxSoFar < maxEndingHere){ |
| 95 | + maxSoFar = maxEndingHere; |
| 96 | + maxEnd = i; |
| 97 | + maxStart = start; |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + return new KadaneResult(maxSoFar, maxStart, maxEnd); |
| 102 | + } |
| 103 | + |
| 104 | +} |
0 commit comments