363 Max Sum of Rectangle No Larger Than K

Problem:

Given a non-empty 2D matrix matrix and an integer k, find the max sum of a rectangle in the matrix such that its sum is no larger than k.

Example:

Given matrix = [
  [1,  0, 1],
  [0, -2, 3]
]
k = 2

The answer is 2. Because the sum of rectangle [[0, 1], [-2, 3]] is 2 and 2 is the max number no larger than k (k = 2).

Note:

  1. The rectangle inside the matrix must have an area > 0.

  2. What if the number of rows is much larger than the number of columns?

Solutions:

public class Solution {
    public int maxSumSubmatrix(int[][] matrix, int k) {
        if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
            return 0;
        }
        int[][] maxs = new int[matrix.length][matrix[0].length];
        
        init(maxs, matrix, k);
        for (int i = 1; i < matrix.length; i ++) {
            for (int j = 1; j < matrix[0].length; j ++) {
                maxs[i][j] = maxs[i - 1][j] + maxs[i][j - 1] - maxs[i - 1][j - 1] + matrix[i][j];
            }
        }
        int max = Integer.MIN_VALUE;
        for (int i = 0; i < maxs.length; i ++) {
            for (int j = 0; j < maxs[0].length; j ++) {
                for (int m = i; m < maxs.length; m ++) {
                    for (int n = j; n < maxs[0].length; n ++) {
                        int tmp = maxs[m][n];
                        if (i - 1 >= 0) {
                            tmp -= maxs[i - 1][n];
                        }
                        if (j - 1 >= 0) {
                            tmp -= maxs[m][j - 1];
                        }
                        if (i - 1 >= 0 && j - 1 >= 0) {
                            tmp += maxs[i - 1][j - 1];
                        }
                        if (tmp <= k) {
                            max = Math.max(max, tmp);
                        }
                    }
                }
            }
        }
        return max;
    }
    private void init(int[][] maxs, int[][] matrix, int k) {
        for (int i = 0; i < matrix.length; i ++) {
            if (i == 0) {
                maxs[i][0] = matrix[i][0];
            }
            else {
                maxs[i][0] = maxs[i - 1][0] + matrix[i][0];
            }
        }
        for (int j = 0; j < matrix[0].length; j ++) {
            if (j == 0) {
                maxs[0][j] = matrix[0][j];
            }
            else {
                maxs[0][j] = maxs[0][j - 1] + matrix[0][j];
            }
        }
    }
}

Solutions:

Last updated