java.lang.Object
g1501_1600.s1504_count_submatrices_with_all_ones.Solution

public class Solution extends Object
1504 - Count Submatrices With All Ones.<p>Medium</p> <p>Given an <code>m x n</code> binary matrix <code>mat</code>, <em>return the number of <strong>submatrices</strong> that have all ones</em>.</p> <p><strong>Example 1:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2021/10/27/ones1-grid.jpg" alt="" /></p> <p><strong>Input:</strong> mat = [[1,0,1],[1,1,0],[1,1,0]]</p> <p><strong>Output:</strong> 13</p> <p><strong>Explanation:</strong></p> <p>There are 6 rectangles of side 1x1.</p> <p>There are 2 rectangles of side 1x2.</p> <p>There are 3 rectangles of side 2x1.</p> <p>There is 1 rectangle of side 2x2.</p> <p>There is 1 rectangle of side 3x1.</p> <p>Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13.</p> <p><strong>Example 2:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2021/10/27/ones2-grid.jpg" alt="" /></p> <p><strong>Input:</strong> mat = [[0,1,1,0],[0,1,1,1],[1,1,1,0]]</p> <p><strong>Output:</strong> 24</p> <p><strong>Explanation:</strong></p> <p>There are 8 rectangles of side 1x1.</p> <p>There are 5 rectangles of side 1x2.</p> <p>There are 2 rectangles of side 1x3.</p> <p>There are 4 rectangles of side 2x1.</p> <p>There are 2 rectangles of side 2x2.</p> <p>There are 2 rectangles of side 3x1.</p> <p>There is 1 rectangle of side 3x2.</p> <p>Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= m, n <= 150</code></li> <li><code>mat[i][j]</code> is either <code>0</code> or <code>1</code>.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • numSubmat

      public int numSubmat(int[][] mat)