Skip to content

Commit 31c8aa0

Browse files
committed
Runtime: 0 ms (Top 100.0%) | Memory: 2.30 MB (Top 75.0%)
1 parent 70701bb commit 31c8aa0

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Runtime: 0 ms (Top 100.0%) | Memory: 2.30 MB (Top 75.0%)
2+
3+
impl Solution {
4+
pub fn largest1_bordered_square(grid: Vec<Vec<i32>>) -> i32 {
5+
let mut max_len:i32 = 0;
6+
for i in 0..grid.len() {
7+
let mut tmp_max: i32 = 0;
8+
for j in 0..grid[0].len() {
9+
if grid[i][j] == 1 {
10+
tmp_max += 1;
11+
if tmp_max > max_len {
12+
13+
if tmp_max == 1 {
14+
max_len = 1;
15+
} else {
16+
for k in ((j as i32)-tmp_max+1) as usize..j {
17+
if Solution::check_valid_square(&grid, i, k,j-k +1) {
18+
// println!("in line {}, the max is {}, col is {}", i, tmp_max,(tmp_max-k as i32));
19+
if (j-k+1)as i32 > max_len {
20+
max_len = (j-k+1)as i32;
21+
}
22+
23+
}
24+
}
25+
26+
}
27+
}
28+
} else {
29+
tmp_max = 0;
30+
}
31+
}
32+
}
33+
max_len * max_len
34+
}
35+
pub fn check_valid_square(matrix: &Vec<Vec<i32>>, r: usize, c: usize, len:usize) -> bool {
36+
if r+len-1 >= matrix.len() {
37+
return false
38+
}
39+
for j in c..(c+len) {
40+
if matrix[r][j] == 0 ||matrix[r+len-1][j] == 0 {
41+
return false
42+
}
43+
}
44+
45+
for i in r..(r+len) {
46+
if matrix[i][c+len-1] == 0 || matrix[i][c] == 0{
47+
return false
48+
}
49+
}
50+
true
51+
}
52+
}

0 commit comments

Comments
 (0)