Count Submatrices With All Ones LeetCode solution in Rust
1impl Solution {2pub fn num_submat(mat: Vec<Vec<i32>>) -> i32 {3let (m, n) = (mat.len(), mat[0].len());4let (mut clm, mut row) = (vec![vec![0; n]; m + 1], vec![vec![0; n + 1]; m]);56for j in 0..n {7for i in 0..m {8clm[i + 1][j] = mat[i][j] as usize + clm[i][j];9}10}1112for i in 0..m {13for j in 0..n {14row[i][j + 1] = mat[i][j] as usize + clm[i][j];15}16}1718let mut ret = 0;19for i in 0..m {20for j in 0..n {21let mut h = m - i;22for k in j..n {23// trying to for a rectangle from [i, j] to [i, k] as base with maximum hight h24while h > 0 && clm[i + h][k] - clm[i][k] < h {25h -= 1;26}27if h == 0 {28break;29}30ret += h; // we could have h rectangles with same base, but different height31}32}33}3435ret as _36}37}