Skip to content

1413. Maximum Side Length Of A Square With Sum Less Than Or Equal To Threshold

Difficulty: Medium

LeetCode Problem View on GitHub


1413. Maximum Side Length of a Square with Sum Less than or Equal to Threshold

Medium


Given a m x n matrix mat and an integer threshold, return the maximum side-length of a square with a sum less than or equal to threshold or return 0 if there is no such square.

 

Example 1:

Input: mat = [[1,1,3,2,4,3,2],[1,1,3,2,4,3,2],[1,1,3,2,4,3,2]], threshold = 4
Output: 2
Explanation: The maximum side length of square with sum less than 4 is 2 as shown.

Example 2:

Input: mat = [[2,2,2,2,2],[2,2,2,2,2],[2,2,2,2,2],[2,2,2,2,2],[2,2,2,2,2]], threshold = 1
Output: 0

 

Constraints:

  • m == mat.length
  • n == mat[i].length
  • 1 <= m, n <= 300
  • 0 <= mat[i][j] <= 104
  • 0 <= threshold <= 105

Solution

class Solution {
    private int pref[][];
    public int maxSideLength(int[][] mat, int threshold) {
        int n = mat.length, m = mat[0].length;

        buildPref(mat);

        int low = 0, high = Math.min(n, m), ans = -1;
        while (low <= high) {
            int mid = low + (high - low) / 2;
            if (ok(n, m, mid, threshold)) {
                ans = mid ;
                low = mid + 1;
            }
            else 
                high = mid - 1;
        } 
        return ans + 1;
    }
    private boolean ok(int n, int m, int target, int threshold) {
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (i + target < n && j + target < m) {
                    int r1 = i, c1 = j, r2 = i + target, c2 = j + target;
                    if (query(r1, c1, r2, c2) <= threshold) 
                        return true;
                }
            }
        }
        return false;
    }
    private void buildPref(int arr[][]) {
        int n = arr.length, m = arr[0].length;
        pref = new int[n][m];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (i - 1 >= 0) 
                    pref[i][j] += pref[i - 1][j];
                if (j - 1 >= 0) 
                    pref[i][j] += pref[i][j - 1];
                if (i - 1 >= 0 && j - 1 >= 0) 
                    pref[i][j] -= pref[i - 1][j - 1];
                pref[i][j] += arr[i][j];
            }
        }
    }
    private int query(int r1, int c1, int r2, int c2) {
        int total = pref[r2][c2];
        if (r1 - 1 >= 0)
            total -= pref[r1 - 1][c2];
        if (c1 - 1 >= 0)
            total -= pref[r2][c1 - 1];
        if (r1 - 1 >= 0 && c1 - 1 >= 0)
            total += pref[r1 - 1][c1 - 1];
        return total;
    }
}

Complexity Analysis

  • Time Complexity: O(?)
  • Space Complexity: O(?)

Approach

Detailed explanation of the approach will be added here