diff --git a/code/data_structures/src/stack/stack/LargestRecInHistogram.cpp b/code/data_structures/src/stack/stack/LargestRecInHistogram.cpp new file mode 100644 index 0000000000..41bd5bee62 --- /dev/null +++ b/code/data_structures/src/stack/stack/LargestRecInHistogram.cpp @@ -0,0 +1,39 @@ +#include +#include +#include +using namespace std; + +int largestRectangleArea(vector& heights) { + stack 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 heights = {2, 1, 5, 6, 2, 3}; + int result = largestRectangleArea(heights); + cout << "The largest rectangle area is: " << result << endl; + return 0; +}