Skip to content

Commit a2ceffa

Browse files
authored
feat: add Rust solution for lc No.1292 (#4981)
1 parent e79aa5d commit a2ceffa

3 files changed

Lines changed: 116 additions & 1 deletion

File tree

solution/1200-1299/1292.Maximum Side Length of a Square with Sum Less than or Equal to Threshold/README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,44 @@ function maxSideLength(mat: number[][], threshold: number): number {
257257
}
258258
```
259259

260+
#### Rust
261+
262+
```rust
263+
impl Solution {
264+
pub fn max_side_length(mat: Vec<Vec<i32>>, threshold: i32) -> i32 {
265+
let m = mat.len();
266+
let n = mat[0].len();
267+
let mut s = vec![vec![0; n + 1]; m + 1];
268+
for i in 1..=m {
269+
for j in 1..=n {
270+
s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + mat[i - 1][j - 1];
271+
}
272+
}
273+
let check = |k: usize| -> bool {
274+
for i in 0..=m - k {
275+
for j in 0..=n - k {
276+
if s[i + k][j + k] - s[i][j + k] - s[i + k][j] + s[i][j] <= threshold {
277+
return true;
278+
}
279+
}
280+
}
281+
false
282+
};
283+
let mut l = 0usize;
284+
let mut r = m.min(n);
285+
while l < r {
286+
let mid = (l + r + 1) >> 1;
287+
if check(mid) {
288+
l = mid;
289+
} else {
290+
r = mid - 1;
291+
}
292+
}
293+
l as i32
294+
}
295+
}
296+
```
297+
260298
<!-- tabs:end -->
261299

262300
<!-- solution:end -->

solution/1200-1299/1292.Maximum Side Length of a Square with Sum Less than or Equal to Threshold/README_EN.md

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,13 @@ tags:
5656

5757
<!-- solution:start -->
5858

59-
### Solution 1
59+
### Solution 1: 2D Prefix Sum + Binary Search
60+
61+
We can precompute a 2D prefix sum array $s$, where $s[i + 1][j + 1]$ represents the sum of elements in the matrix $mat$ from $(0, 0)$ to $(i, j)$. With this, we can calculate the sum of elements in any square region in $O(1)$ time.
62+
63+
Next, we can use binary search to find the maximum side length. We enumerate the side length $k$ of the square, and then iterate through all possible top-left positions $(i, j)$ of the square. We can calculate the sum of elements $v$ for the square. If $v \leq threshold$, it indicates that there exists a square region with side length $k$ whose sum is less than or equal to the threshold; otherwise, no such square exists for the current $k$.
64+
65+
The time complexity is $O(m \times n \times \log \min(m, n))$, and the space complexity is $O(m \times n)$.
6066

6167
<!-- tabs:start -->
6268

@@ -246,6 +252,44 @@ function maxSideLength(mat: number[][], threshold: number): number {
246252
}
247253
```
248254

255+
#### Rust
256+
257+
```rust
258+
impl Solution {
259+
pub fn max_side_length(mat: Vec<Vec<i32>>, threshold: i32) -> i32 {
260+
let m = mat.len();
261+
let n = mat[0].len();
262+
let mut s = vec![vec![0; n + 1]; m + 1];
263+
for i in 1..=m {
264+
for j in 1..=n {
265+
s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + mat[i - 1][j - 1];
266+
}
267+
}
268+
let check = |k: usize| -> bool {
269+
for i in 0..=m - k {
270+
for j in 0..=n - k {
271+
if s[i + k][j + k] - s[i][j + k] - s[i + k][j] + s[i][j] <= threshold {
272+
return true;
273+
}
274+
}
275+
}
276+
false
277+
};
278+
let mut l = 0usize;
279+
let mut r = m.min(n);
280+
while l < r {
281+
let mid = (l + r + 1) >> 1;
282+
if check(mid) {
283+
l = mid;
284+
} else {
285+
r = mid - 1;
286+
}
287+
}
288+
l as i32
289+
}
290+
}
291+
```
292+
249293
<!-- tabs:end -->
250294

251295
<!-- solution:end -->
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
impl Solution {
2+
pub fn max_side_length(mat: Vec<Vec<i32>>, threshold: i32) -> i32 {
3+
let m = mat.len();
4+
let n = mat[0].len();
5+
let mut s = vec![vec![0; n + 1]; m + 1];
6+
for i in 1..=m {
7+
for j in 1..=n {
8+
s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + mat[i - 1][j - 1];
9+
}
10+
}
11+
let check = |k: usize| -> bool {
12+
for i in 0..=m - k {
13+
for j in 0..=n - k {
14+
if s[i + k][j + k] - s[i][j + k] - s[i + k][j] + s[i][j] <= threshold {
15+
return true;
16+
}
17+
}
18+
}
19+
false
20+
};
21+
let mut l = 0usize;
22+
let mut r = m.min(n);
23+
while l < r {
24+
let mid = (l + r + 1) >> 1;
25+
if check(mid) {
26+
l = mid;
27+
} else {
28+
r = mid - 1;
29+
}
30+
}
31+
l as i32
32+
}
33+
}

0 commit comments

Comments
 (0)