Skip to content
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

Create LargestRecInHistogram.cpp #6796

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions code/data_structures/src/stack/stack/LargestRecInHistogram.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include <iostream>
#include <vector>
#include <stack>
using namespace std;

int largestRectangleArea(vector<int>& heights) {
stack<int> s; // Stack to store indices of the bars
int maxArea = 0; // Variable to store the maximum area
int n = heights.size();

// Traverse all bars of the histogram
for (int i = 0; i < n; i++) {
// If this bar is lower than the bar at the stack's top, pop the stack and calculate area
while (!s.empty() && heights[s.top()] > heights[i]) {
int h = heights[s.top()];
s.pop();
int width = s.empty() ? i : i - s.top() - 1;
maxArea = max(maxArea, h * width);
}
s.push(i); // Push the current bar index onto the stack
}

// Process the remaining bars in the stack
while (!s.empty()) {
int h = heights[s.top()];
s.pop();
int width = s.empty() ? n : n - s.top() - 1;
maxArea = max(maxArea, h * width);
}

return maxArea;
}

int main() {
vector<int> heights = {2, 1, 5, 6, 2, 3};
int result = largestRectangleArea(heights);
cout << "The largest rectangle area is: " << result << endl;
return 0;
}