Loading [MathJax]/jax/output/CommonHTML/config.js
前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >LeetCode 0085 - Maximal Rectangle

LeetCode 0085 - Maximal Rectangle

作者头像
Reck Zhang
发布于 2021-08-11 02:49:33
发布于 2021-08-11 02:49:33
26300
代码可运行
举报
文章被收录于专栏:Reck ZhangReck Zhang
运行总次数:0
代码可运行

Maximal Rectangle

Desicription

Given a 2D binary matrix filled with 0’s and 1’s, find the largest rectangle containing only 1’s and return its area.

For example, given the following matrix:

1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0 1 0

Return 6.

Solution

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
class Solution {
public:
    int maximalRectangle(vector<vector<char>>& matrix) {
        if(matrix.empty())
            return 0;
        int row_size = matrix.size();
        int col_size = matrix[0].size();
        vector<int> left(col_size, 0);
        vector<int> right(col_size, col_size);
        vector<int> height(col_size, 0);
        int res = 0;
        for(int i = 0; i < row_size; i++){
            int cur_left = 0;
            int cur_right = col_size;
            for(int j = 0; j < col_size; j++){
                if(matrix[i][j] == '1')
                    height[j]++;
                else
                    height[j] = 0;
            }
            for(int j = 0; j < col_size; j++){
                if(matrix[i][j] == '1')
                    left[j] = max(left[j], cur_left);
                else
                    left[j] = 0, cur_left = j + 1;
            }
            for(int j = col_size - 1; j >= 0; j--){
                if(matrix[i][j] == '1')
                    right[j] = min(right[j], cur_right);
                else
                    right[j] = col_size, cur_right = j;
            }
            for(int j = 0; j < col_size; j++)
                res = max(res, (right[j] - left[j]) * height[j]);
        }
        return res;
    }
};
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017-11-20,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
Leetcode 85 Maximal Rectangle 推荐!
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1's and return its area. For example, given the following matrix: 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0 1 0 Return 6. 求最大子矩阵。 和84如出一辙,http://blog.csdn.net/accepthj
triplebee
2018/01/12
6360
leetcode: 85. Maximal Rectangle
From LeetCode 笔记系列 18 Maximal Rectangle [学以致用]:
JNingWei
2018/09/27
7230
leetcode: 85. Maximal Rectangle
LeetCode <Stack>84&85. Maximal Rectangle&Largest Rectangle in Histogram
Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram.
大学里的混子
2018/11/20
4380
【leetcode刷题】T23-最大矩形
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1's and return its area.
木又AI帮
2019/07/17
4560
LeetCode 0363 - Max Sum of Rectangle No Larger Than K
Given a non-empty 2D matrix matrix and an integer k, find the max sum of a rectangle in the matrix such that its sum is no larger than k.
Reck Zhang
2021/08/11
2230
LeetCode 0304 - Range Sum Query 2D - Immutable
Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper left corner (row1, col1) and lower right corner (row2, col2).
Reck Zhang
2021/08/11
2330
LeetCode 0304 - Range Sum Query 2D - Immutable
LeetCode 0329 - Longest Increasing Path in a Matrix
Given an integer matrix, find the length of the longest increasing path.
Reck Zhang
2021/08/11
1830
Leetcode 221. Maximal Square
Given a 2D binary matrix filled with 0's and 1's, find the largest square containing only 1's and return its area. For example, given the following matrix: 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0 1 0 Return 4. 找出矩阵中最大的由1组成的正方形的大小。 dp[i][j]表示以这个点为右下角的最大正方形的边长
triplebee
2018/01/12
6550
算法细节系列(12):破除想当然
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u014688145/article/details/70820874
用户1147447
2019/05/26
3260
LeetCode 85. Maximal Rectangle
需要有两个DP数组,dp[i][j] 和dp2[i][j] , 在递推的过程相互轮换。dp[i][j]表示上一层的状态数组,dp2[i][j]表示当前层的状态数组
ShenduCC
2019/11/20
2800
leetcode 931. 下降路径最小和
---- 下降路径最小和题解汇总 自上而下的动态规划 自下而上的动态规划 动态规划的优化---一维数组 记忆化递归 ---- 自上而下的动态规划 矩阵中的动态规划基本上都比较容易入手。这道题也算是入门题,我们可以设dp[i][j]表示到(i, j)位置的最小和,通过题目描述和手动模拟我们很容易得出状态转移方程: dp[i][j]=min(dp[i-1][j-1],dp[i-1][j],dp[i-1][j+1])+A[i][j] 最后取dp最后一行的最小值即可 对于这种需要考虑边界的情况,我
大忽悠爱学习
2021/11/15
9300
单调栈结构 && 84. Largest Rectangle in Histogram&&最大子矩阵的大小
Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram.
大学里的混子
2019/02/24
5520
LeetCode 0084 - Largest Rectangle in Histogram
Given n non-negative integers representing the histogram’s bar height where the width of each bar is 1, find the area of largest rectangle in the histogram.
Reck Zhang
2021/08/11
2240
LeetCode: 221_Maximal Square | 二维0-1矩阵中计算包含1的最大正方形的面积 | Medium
题目: Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and return its area. For example, given the following matrix: 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0 1 0 Return 4. 解题思路:   这种包含最大、最小等含优化的字眼时,一般都需要用到动态规划进行求解
Linux云计算网络
2018/01/11
1.3K0
LeetCode 0074 - Search a 2D Matrix
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:
Reck Zhang
2021/08/11
1700
LeetCode 85. 最大矩形(DP/单调递增栈,难)
给定一个仅包含 0 和 1 的二维二进制矩阵,找出只包含 1 的最大矩形,并返回其面积。
Michael阿明
2020/07/13
6010
​LeetCode刷题实战85: 最大矩形
https://leetcode-cn.com/problems/maximal-rectangle/
程序员小猿
2021/01/19
4420
​LeetCode刷题实战85: 最大矩形
leetcode题解 | 48. 旋转图像
你必须在原地旋转图像,这意味着你需要直接修改输入的二维矩阵。请不要使用另一个矩阵来旋转图像。
ACM算法日常
2018/08/07
5350
leetcode题解 | 48. 旋转图像
【代码随想录】二刷-数组
数组 二分查找 704. 二分查找 方法1 注意: 边界控制。 前提是有序数组。 循环控制 解释: 这里使用我最好理解的一种方式。 使用mid控制下标访问,nums[mid]大于target,+1更新左边界,反之,-1更新右边界。相等即找到目标数。 while循环条件left<=right,left=right仍有需要判断的数。即目标数的索引在数组的 边界。 防止left+right过大会溢出,所以写成(right-left)/2,等同于(right+left)/2 // 时间复杂
半生瓜的blog
2023/05/13
3500
leetcode-太平洋大西洋水流问题
水流既可以流动到“太平洋"意思是:从任意位置出发,能达到 大陆的左边界和上边界 就可以。(~ ~ ~)
早起的鸟儿有虫吃
2019/08/23
6450
leetcode-太平洋大西洋水流问题
相关推荐
Leetcode 85 Maximal Rectangle 推荐!
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验