-
Notifications
You must be signed in to change notification settings - Fork 275
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| #include <iostream> | ||
| #include <vector> | ||
| #include <stack> | ||
| #include <algorithm> | ||
| // Function to calculate the largest rectangle area in a histogram | ||
| int largestRectangleArea(std::vector<int>& heights) { | ||
| std::stack<int> st; | ||
| int maxArea = 0; | ||
| int n = heights.size(); | ||
|
|
||
| for (int i = 0; i < n; i++) { | ||
| while (!st.empty() && heights[st.top()] >= heights[i]) { | ||
| int height = heights[st.top()]; | ||
| st.pop(); | ||
| int width = st.empty() ? i : i - st.top() - 1; | ||
| maxArea = std::max(maxArea, height * width); | ||
| } | ||
| st.push(i); | ||
| } | ||
|
|
||
| // Final pass to process remaining indices in the stack | ||
| while (!st.empty()) { | ||
| int height = heights[st.top()]; | ||
| st.pop(); | ||
| int width = st.empty() ? n : n - st.top() - 1; | ||
| maxArea = std::max(maxArea, height * width); | ||
| } | ||
|
|
||
| return maxArea; | ||
| } | ||
|
|
||
| // Function to find the maximum rectangle area in a binary matrix | ||
| int maxRectangle(std::vector<std::vector<int>>& mat) { | ||
| if (mat.empty()) return 0; | ||
|
|
||
| int maxArea = 0; | ||
| int rows = mat.size(); | ||
| int cols = mat[0].size(); | ||
| std::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 = std::max(maxArea, largestRectangleArea(heights)); | ||
| } | ||
|
|
||
| return maxArea; | ||
| } | ||
|
|
||
| int main() { | ||
| std::vector<std::vector<int>> mat = { | ||
| {1, 0, 1, 0, 0}, | ||
| {1, 0, 1, 1, 1}, | ||
| {1, 1, 1, 1, 1}, | ||
| {1, 0, 0, 1, 0} | ||
| }; | ||
|
|
||
| std::cout << "Maximum rectangle area: " << maxRectangle(mat) << std::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: | ||
| * - largestRectangleArea: O(cols) for the stack used to store indices. | ||
| * - maxRectangle: O(cols) for the heights array. | ||
| * - Overall auxiliary space: O(cols) + O(cols) = O(cols). | ||
| */ | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] Embedding a main function inside an algorithm source file reduces reusability and can cause multiple definition issues when linking with other executables. Consider moving this demo to a separate example/test file or wrapping it in an #ifdef DEMO guard.