From 4aefe301deae9606a0b9ffcb8e0c84326570779b Mon Sep 17 00:00:00 2001 From: chayan das <110921638+Chayandas07@users.noreply.github.com> Date: Sun, 14 Apr 2024 00:44:36 +0530 Subject: [PATCH] Create 85. Maximal Rectangle --- 85. Maximal Rectangle | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 85. Maximal Rectangle diff --git a/85. Maximal Rectangle b/85. Maximal Rectangle new file mode 100644 index 0000000..7a5d782 --- /dev/null +++ b/85. Maximal Rectangle @@ -0,0 +1,36 @@ +class Solution { +public: + int maximalRectangle(vector>& matrix) { + int maxArea = 0; + + if(matrix.size() == 0 || matrix[0].size() == 0) return 0; + int cols = matrix[0].size(); + int rows = matrix.size(); + vectorhistogram(cols+1, 0); + histogram[cols] = -1; + stack>monotonic; + + for(auto row: matrix) { + for(int i = 0; i < cols; i++) { + if(row[i] == '1') { + histogram[i]++; + } else { + histogram[i] = 0; + } + } + + for(int i = 0; i < cols+1; i++) { + int x = 0; + while(!monotonic.empty() && monotonic.top().first >= histogram[i]) { + auto tp = monotonic.top(); monotonic.pop(); + int h = tp.first, steps = tp.second; + x += steps; + maxArea = max(maxArea, h*x); + } + monotonic.push({histogram[i], x+1}); + } + } + + return maxArea; + } +};