Skip to content

Commit 6fcefaa

Browse files
chore: add LeetCode daily solution
1 parent 088ce7d commit 6fcefaa

5 files changed

Lines changed: 142 additions & 0 deletions

File tree

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Maximum Side Length of a Square with Sum Less than or Equal to Threshold (Medium)
2+
3+
**Problem ID:** 1292
4+
**Date:** 2026-01-19
5+
**Link:** https://leetcode.com/problems/maximum-side-length-of-a-square-with-sum-less-than-or-equal-to-threshold/
6+
7+
## Approach
8+
9+
To solve the problem of finding the maximum side length of a square with a sum less than or equal to a given threshold in a matrix, we can employ a combination of prefix sums and binary search.
10+
11+
### Approach:
12+
13+
1. **Prefix Sum Calculation**:
14+
- First, we create a prefix sum matrix that allows us to compute the sum of any sub-square in constant time. The prefix sum at position `(i, j)` represents the sum of all elements in the sub-matrix from the top-left corner `(0, 0)` to `(i, j)`.
15+
- The prefix sum can be computed using the formula:
16+
\[
17+
\text{prefix}[i][j] = \text{mat}[i][j] + \text{prefix}[i-1][j] + \text{prefix}[i][j-1] - \text{prefix}[i-1][j-1]
18+
\]
19+
- This allows us to efficiently calculate the sum of any square sub-matrix.
20+
21+
2. **Binary Search for Maximum Side Length**:
22+
- We can use binary search to determine the largest possible side length `k` for which we can find a square of size `k x k` with a sum less than or equal to the threshold.
23+
- The search range for `k` is from `0` to `min(m, n)` (the dimensions of the matrix).
24+
25+
3. **Checking Validity of Side Length**:
26+
- For each candidate side length `k`, we iterate over all possible top-left corners of `k x k` squares in the matrix. For each position `(i, j)`, we calculate the sum of the square using the prefix sum matrix.
27+
- If the sum is less than or equal to the threshold, we can consider `k` as a valid side length.
28+
29+
4. **Complexity**:
30+
- The prefix sum computation takes \(O(m \times n)\).
31+
- The binary search runs in \(O(\log(\min(m, n)))\) iterations.
32+
- For each iteration of the binary search, checking all possible squares of size `k` requires \(O((m-k+1) \times (n-k+1))\), which in the worst case can be approximated as \(O(m \times n)\).
33+
- Thus, the overall time complexity is \(O(m \times n \log(\min(m, n)))\).
34+
35+
### Summary:
36+
The main idea is to utilize a prefix sum matrix to facilitate quick sum calculations of square sub-matrices and to apply binary search to efficiently find the maximum side length of a square that meets the sum constraint. This approach balances preprocessing with efficient querying, making it suitable for the problem's constraints.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class Solution {
2+
public int maxSideLength(int[][] mat, int threshold) {
3+
int m = mat.length, n = mat[0].length;
4+
int[][] prefixSum = new int[m + 1][n + 1];
5+
6+
// Calculate prefix sums
7+
for (int i = 1; i <= m; i++) {
8+
for (int j = 1; j <= n; j++) {
9+
prefixSum[i][j] = mat[i - 1][j - 1]
10+
+ prefixSum[i - 1][j]
11+
+ prefixSum[i][j - 1]
12+
- prefixSum[i - 1][j - 1];
13+
}
14+
}
15+
16+
int maxSide = 0;
17+
18+
// Check for the maximum side length of the square
19+
for (int side = 1; side <= Math.min(m, n); side++) {
20+
for (int i = side; i <= m; i++) {
21+
for (int j = side; j <= n; j++) {
22+
int total = prefixSum[i][j]
23+
- prefixSum[i - side][j]
24+
- prefixSum[i][j - side]
25+
+ prefixSum[i - side][j - side];
26+
if (total <= threshold) {
27+
maxSide = side;
28+
}
29+
}
30+
}
31+
}
32+
33+
return maxSide;
34+
}
35+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
var maxSideLength = function(mat, threshold) {
2+
const m = mat.length, n = mat[0].length;
3+
const prefixSum = Array.from({ length: m + 1 }, () => Array(n + 1).fill(0));
4+
5+
for (let i = 1; i <= m; i++) {
6+
for (let j = 1; j <= n; j++) {
7+
prefixSum[i][j] = mat[i - 1][j - 1] + prefixSum[i - 1][j] + prefixSum[i][j - 1] - prefixSum[i - 1][j - 1];
8+
}
9+
}
10+
11+
let left = 0, right = Math.min(m, n);
12+
13+
while (left < right) {
14+
const mid = Math.floor((left + right + 1) / 2);
15+
let found = false;
16+
17+
for (let i = mid; i <= m; i++) {
18+
for (let j = mid; j <= n; j++) {
19+
const sum = prefixSum[i][j] - prefixSum[i - mid][j] - prefixSum[i][j - mid] + prefixSum[i - mid][j - mid];
20+
if (sum <= threshold) {
21+
found = true;
22+
break;
23+
}
24+
}
25+
if (found) break;
26+
}
27+
28+
if (found) {
29+
left = mid;
30+
} else {
31+
right = mid - 1;
32+
}
33+
}
34+
35+
return left;
36+
};
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class Solution:
2+
def maxSideLength(self, mat: List[List[int]], threshold: int) -> int:
3+
m, n = len(mat), len(mat[0])
4+
prefix_sum = [[0] * (n + 1) for _ in range(m + 1)]
5+
6+
for i in range(1, m + 1):
7+
for j in range(1, n + 1):
8+
prefix_sum[i][j] = mat[i - 1][j - 1] + prefix_sum[i - 1][j] + prefix_sum[i][j - 1] - prefix_sum[i - 1][j - 1]
9+
10+
def get_sum(x1, y1, x2, y2):
11+
return prefix_sum[x2 + 1][y2 + 1] - prefix_sum[x1][y2 + 1] - prefix_sum[x2 + 1][y1] + prefix_sum[x1][y1]
12+
13+
left, right = 0, min(m, n)
14+
result = 0
15+
16+
while left <= right:
17+
mid = (left + right) // 2
18+
found = False
19+
20+
for i in range(m - mid + 1):
21+
for j in range(n - mid + 1):
22+
if get_sum(i, j, i + mid - 1, j + mid - 1) <= threshold:
23+
found = True
24+
break
25+
if found:
26+
break
27+
28+
if found:
29+
result = mid
30+
left = mid + 1
31+
else:
32+
right = mid - 1
33+
34+
return result

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,3 +338,4 @@ Through completing the Blind 75 and NeetCode 150, you will have mastered:
338338
- 2026-01-16 — [Maximum Square Area by Removing Fences From a Field](https://leetcode.com/problems/maximum-square-area-by-removing-fences-from-a-field/) (Medium) → `Medium/2026-01-16-2975-Maximum-Square-Area-by-Removing-Fences-From-a-Field`
339339
- 2026-01-17 — [Find the Largest Area of Square Inside Two Rectangles](https://leetcode.com/problems/find-the-largest-area-of-square-inside-two-rectangles/) (Medium) → `Medium/2026-01-17-3047-Find-the-Largest-Area-of-Square-Inside-Two-Rectangles`
340340
- 2026-01-18 — [Largest Magic Square](https://leetcode.com/problems/largest-magic-square/) (Medium) → `Medium/2026-01-18-1895-Largest-Magic-Square`
341+
- 2026-01-19 — [Maximum Side Length of a Square with Sum Less than or Equal to Threshold](https://leetcode.com/problems/maximum-side-length-of-a-square-with-sum-less-than-or-equal-to-threshold/) (Medium) → `Medium/2026-01-19-1292-Maximum-Side-Length-of-a-Square-with-Sum-Less-than-or-Equal-to-Threshold`

0 commit comments

Comments
 (0)