-
Notifications
You must be signed in to change notification settings - Fork 274
Add max_Rectangle implementation in cpp #181
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,72 @@ | ||||||
| #include <iostream> | ||||||
| #include <vector> | ||||||
| #include <stack> | ||||||
| #include <algorithm> | ||||||
| using namespace std; | ||||||
|
|
||||||
| // Function to calculate the largest rectangle area in a histogram | ||||||
| int largestRectangleArea(vector<int>& heights) { | ||||||
| stack<int> st; | ||||||
| int maxArea = 0; | ||||||
| int n = heights.size(); | ||||||
|
|
||||||
| for (int i = 0; i <= n; i++) { | ||||||
| while (!st.empty() && (i == n || heights[st.top()] >= heights[i])) { | ||||||
| int height = heights[st.top()]; | ||||||
| st.pop(); | ||||||
| int width = st.empty() ? i : i - st.top() - 1; | ||||||
| maxArea = max(maxArea, height * width); | ||||||
| } | ||||||
| st.push(i); | ||||||
| } | ||||||
|
SurajS27 marked this conversation as resolved.
Outdated
|
||||||
|
|
||||||
| return maxArea; | ||||||
| } | ||||||
|
|
||||||
| // Function to find the maximum rectangle area in a binary matrix | ||||||
| int maxRectangle(vector<vector<int>>& mat) { | ||||||
| if (mat.empty()) return 0; | ||||||
|
|
||||||
| int maxArea = 0; | ||||||
| int rows = mat.size(); | ||||||
| int cols = mat[0].size(); | ||||||
| vector<int> heights(cols, 0); | ||||||
|
|
||||||
| for (int i = 0; i < rows; i++) { | ||||||
| for (int j = 0; j < cols; j++) { | ||||||
| heights[j] = mat[i][j] == 0 ? 0 : heights[j] + 1; | ||||||
| } | ||||||
| maxArea = max(maxArea, largestRectangleArea(heights)); | ||||||
| } | ||||||
|
|
||||||
| return maxArea; | ||||||
| } | ||||||
|
|
||||||
| int main() { | ||||||
| vector<vector<int>> mat = { | ||||||
| {1, 0, 1, 0, 0}, | ||||||
| {1, 0, 1, 1, 1}, | ||||||
| {1, 1, 1, 1, 1}, | ||||||
| {1, 0, 0, 1, 0} | ||||||
| }; | ||||||
|
|
||||||
| cout << "Maximum rectangle area: " << maxRectangle(mat) << endl; | ||||||
|
|
||||||
| return 0; | ||||||
| } | ||||||
|
|
||||||
| /* | ||||||
| * Algorithm Explanation: | ||||||
| * - largestRectangleArea function computes the largest rectangular area in a histogram using a stack. | ||||||
| * For each bar, it finds the largest rectangle that can be formed with that bar as the shortest. | ||||||
| * | ||||||
| * - maxRectangle function finds the largest rectangle in a binary matrix. It converts each row into | ||||||
| * a histogram of heights and calculates the maximum rectangle area by calling largestRectangleArea. | ||||||
| * | ||||||
| * Time Complexity: | ||||||
| * - largestRectangleArea: O(n), where n is the number of columns. | ||||||
| * - maxRectangle: O(rows * cols), where rows and cols are the dimensions of the matrix. | ||||||
| * | ||||||
| * Space Complexity: | ||||||
| * - O(cols), where cols is the number of columns in the matrix due to the heights array. | ||||||
|
||||||
| * - O(cols), where cols is the number of columns in the matrix due to the heights array. | |
| * - O(cols), where cols is the number of columns in the matrix, due to the heights array and the stack used in largestRectangleArea. |
Uh oh!
There was an error while loading. Please reload this page.