Bottom-up DP | 6ms, 2.92MB
Complexity\n- Time complexity: O(mn)\n- Space complexity: O(mn)\n\n# Code\n\nimpl Solution {\n    pub fn count_squares(matrix: Vec<Vec<i32>>) -> i32 {\n        
# Complexity\n- Time complexity: $$O(m*n)$$\n- Space complexity: $$O(m*n)$$\n\n# Code\n```\nimpl Solution {\n    pub fn count_squares(matrix: Vec<Vec<i32>>) -> i32 {\n        let (m, n) = (matrix.len(), matrix[0].len());\n        let mut dp = matrix.clone();\n        for i in 1..m {\n            for j in 1..n {\n                if matrix[i][j] == 1 { dp[i][j] += dp[i-1][j].min(dp[i-1][j-1]).min(dp[i][j-1]); }\n            }\n        }\n        dp.into_iter().flatten().sum()\n    }\n}\n```
['Rust']