Leetcode209: Maximal Rectangle

2024-08-28 19:32

本文主要是介绍Leetcode209: Maximal Rectangle,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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

给定一个矩阵中,只有0和1,求出这个矩阵的一个最大的子矩阵,其中只包含1.

例如

01101

11010

01110

11110

11111

00000

其实这个问题可以转化为Largest Rectangle in Histogram,先将上面的矩阵转化为:

01101

12010

03110

14210

25321

00000

然后对每一行求直方图的最大面积。

class Solution {
public:int maxArea(vector<int> &line) {if (line.size() < 1) return 0;stack<int> S;line.push_back(0);int sum = 0;for (int i = 0; i < line.size(); i++) {if (S.empty() || line[i] > line[S.top()]) S.push(i);else {int tmp = S.top();S.pop();sum = max(sum, line[tmp]*(S.empty()? i : i-S.top()-1));i--;}}return sum;}int maximalRectangle(vector<vector<char> > &matrix) {if (matrix.size() < 1) return 0;int n = matrix.size();if (n == 0) return 0;int m = matrix[0].size();if (m == 0) return 0;vector<vector<int> > lines(n, vector<int>(m, 0));for (int i = 0; i < n; ++i) {for (int j = 0; j < m; ++j) {if (i == 0) {lines[i][j] = ((matrix[i][j] == '1') ? 1 : 0);} else {lines[i][j] += ((matrix[i][j] == '1') ? lines[i-1][j] + 1 : 0);}}}int maxRec = 0, tmpRec;for (int i = 0; i < n; ++i) {tmpRec = maxArea(lines[i]);maxRec = (maxRec > tmpRec) ? maxRec : tmpRec;}return maxRec;}
};


这篇关于Leetcode209: Maximal Rectangle的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1115684

相关文章

[LeetCode] 221. Maximal Square

题:https://leetcode.com/problems/maximal-square/description/ #题目 Given a 2D binary matrix filled with 0’s and 1’s, find the largest square containing only 1’s and return its area. Example: Input:

C# 使用中点查找矩形的角(Find Corners of Rectangle using mid points)

考虑一个矩形 ABCD,我们给出了边 AD 和 BC 中点(分别为 p 和 q)的坐标以及它们的长度 L(AD = BC = L)。现在给定参数,我们需要打印 4 个点 A、B、C 和 D 的坐标。 例子:  输入:p = (1, 0)         q = (1, 2)         L = 2 输出:(0,0),(0,2),(2,2),(2,0) 解释: 打

leetcode209. Minimum Size Subarray Sum

题目 Given an array of positive integers nums and a positive integer target, return the minimal length of a subarray whose sum is greater than or equal to target. If there is no such subarray, return 0

OpenCV绘图函数(15)图像上绘制矩形函数 rectangle()的使用

操作系统:ubuntu22.04 OpenCV版本:OpenCV4.9 IDE:Visual Studio Code 编程语言:C++11 算法描述 绘制一个简单的、粗的或填充的直立矩形。 这个函数 cv::rectangle 绘制一个矩形轮廓或一个填充的矩形,其两个相对的顶点分别是 pt1 和 pt2。 函数原型1 void cv::rectangle(InputOutputArra

Leetcode176: Maximal Square

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 01 0 1 1 11 1 1 1 11 0 0 1 0

LeetCode | 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 01 0 1 1 11 1 1 1 11 0 0

【leetcode209】长度最小的子数组

209. 长度最小的子数组 难度中等512收藏分享切换为英文接收动态反馈 给定一个含有 n 个正整数的数组和一个正整数 s ,找出该数组中满足其和 ≥ s 的长度最小的 连续 子数组,并返回其长度。如果不存在符合条件的子数组,返回 0。 示例: 输入:s = 7, nums = [2,3,1,2,4,3]输出:2解释:子数组 [4,3] 是该条件下的长度最小的子数组。 进阶:

Leetcode 391. Perfect Rectangle

解题思想 这道题是说给了一堆小矩形的坐标(左下角和右上角围成的),问其能否组合成一个完美的矩形(刚刚好,不多,不少,不交叉重复)。 核心思想就是:能够正好围成一个矩形的情况就是: 有且只有: 最左下/最左上/最右下/最右上的四个点只出现过一次,其他肯定是出现两次和四次(保证完全覆盖) 上面四个点围成的面积,正好等于所有子矩形的面积之和(保证不重复) Leetcode代码收录,求粉求星星。

LeetCode-492. Construct the Rectangle

问题:https://leetcode.com/problems/construct-the-rectangle/?tab=Description For a web developer, it is very important to know how to design a web page’s size. So, given a specific rectangular web page’

[LeetCode] Rectangle Area

Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is defined by its bottom left corner and top right corner as shown in the figure. Assume that the total area