LeetCode1504. Count Submatrices With All Ones

文章目录

一、题目

Given an m x n binary matrix mat, return the number of submatrices that have all ones.

Example 1:

Input: mat = [[1,0,1],[1,1,0],[1,1,0]]
Output: 13
Explanation:
There are 6 rectangles of side 1x1.
There are 2 rectangles of side 1x2.
There are 3 rectangles of side 2x1.
There is 1 rectangle of side 2x2.
There is 1 rectangle of side 3x1.
Total number of rectangles = 6 + 2 + 3 + 1 + 1 = 13.
Example 2:

Input: mat = [[0,1,1,0],[0,1,1,1],[1,1,1,0]]
Output: 24
Explanation:
There are 8 rectangles of side 1x1.
There are 5 rectangles of side 1x2.
There are 2 rectangles of side 1x3.
There are 4 rectangles of side 2x1.
There are 2 rectangles of side 2x2.
There are 2 rectangles of side 3x1.
There is 1 rectangle of side 3x2.
Total number of rectangles = 8 + 5 + 2 + 4 + 2 + 2 + 1 = 24.

Constraints:

1 <= m, n <= 150
mat[i][j] is either 0 or 1.

二、题解

class Solution {
   
public:
    int numSubmat(vector<vector<int>>& mat) {
   
        int res = 0;
        int m = mat.size(),n = mat[0].size();
        vector<int> heights(n,0);
        for(int i = 0;i < m;i++){
   
            for(int j = 0;j < n;j++){
   
                heights[j] = mat[i][j] == 0 ? 0 : heights[j] + 1;
            }
            res += countFromBottom(heights);
        }
        return res;
    }
    int countFromBottom(vector<int>& heights){
   
        int n = heights.size();
        int res = 0;
        stack<int> st;
        for(int i = 0;i < n;i++){
   
            while(!st.empty() && heights[i] <= heights[st.top()]){
   
                int cur = st.top();
                st.pop();
                if(heights[cur] > heights[i]){
   
                    int left = st.empty() ? -1 : st.top();
                    int len = i - left - 1;
                    int bottom = max(left == -1 ? 0 : heights[left],heights[i]);
                    res += (heights[cur] - bottom) * len * (len + 1) / 2;
                }
            }
            st.push(i);
        }
        while(!st.empty()){
   
            int cur = st.top();
            st.pop();
            int left = st.empty() ? -1 : st.top();
            int len = n - left - 1;
            int bottom = left == -1 ? 0 : heights[left];
            res += (heights[cur] - bottom) * len * (len + 1) / 2;
        }
        return res;
    }
};

相关推荐

  1. LeetCode 150, 112, 130

    2024-01-30 06:56:05       24 阅读
  2. LeetCode1504. Count Submatrices With All Ones

    2024-01-30 06:56:05       53 阅读
  3. LeetCode1534. Count Good Triplets

    2024-01-30 06:56:05       47 阅读
  4. Leetcode面试经典150

    2024-01-30 06:56:05       40 阅读
  5. LeetCode 150:逆波兰表达式

    2024-01-30 06:56:05       40 阅读
  6. leecode面试经典150

    2024-01-30 06:56:05       29 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-30 06:56:05       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-30 06:56:05       106 阅读
  3. 在Django里面运行非项目文件

    2024-01-30 06:56:05       87 阅读
  4. Python语言-面向对象

    2024-01-30 06:56:05       96 阅读

热门阅读

  1. 基于机器学习的无损缺陷检测技术研究进展

    2024-01-30 06:56:05       68 阅读
  2. 机器学习复习(1)——任务整理流程

    2024-01-30 06:56:05       60 阅读
  3. 怎么创建docker镜像

    2024-01-30 06:56:05       60 阅读
  4. 升级anaconda中python到3.10版本

    2024-01-30 06:56:05       47 阅读
  5. 中间件

    2024-01-30 06:56:05       46 阅读
  6. 大语言模型的未来进化路径及其影响

    2024-01-30 06:56:05       53 阅读
  7. docker 修改镜像存储路径

    2024-01-30 06:56:05       48 阅读