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

Update areaHistogram.cpp #580

Open
wants to merge 1 commit into
base: main
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
141 changes: 102 additions & 39 deletions Lecture056 Stacks Questions/areaHistogram.cpp
Original file line number Diff line number Diff line change
@@ -1,62 +1,125 @@

#include <vector>
#include <stack>
#include <climits>
using namespace std;

class Solution {
private:
vector<int> nextSmallerElement(vector<int> arr, int n) {
// Function to find the next smaller element's index for each element
vector<int> nextSmallerElement(const vector<int>& arr, int n) {
stack<int> s;
s.push(-1);
vector<int> ans(n);
vector<int> ans(n, n); // Initialize with 'n' assuming no smaller element to the right

for(int i=n-1; i>=0 ; i--) {
int curr = arr[i];
while(s.top() != -1 && arr[s.top()] >= curr)
{
for (int i = n - 1; i >= 0; i--) {
while (!s.empty() && arr[s.top()] >= arr[i]) {
s.pop();
}
//ans is stack ka top
ans[i] = s.top();
if (!s.empty()) {
ans[i] = s.top();
}
s.push(i);
}
return ans;
}

vector<int> prevSmallerElement(vector<int> arr, int n) {

// Function to find the previous smaller element's index for each element
vector<int> prevSmallerElement(const vector<int>& arr, int n) {
stack<int> s;
s.push(-1);
vector<int> ans(n);
vector<int> ans(n, -1); // Initialize with '-1' assuming no smaller element to the left

for(int i=0; i<n; i++) {
int curr = arr[i];
while(s.top() != -1 && arr[s.top()] >= curr)
{
for (int i = 0; i < n; i++) {
while (!s.empty() && arr[s.top()] >= arr[i]) {
s.pop();
}
//ans is stack ka top
ans[i] = s.top();
if (!s.empty()) {
ans[i] = s.top();
}
s.push(i);
}
return ans;
return ans;
}

public:
int largestRectangleArea(vector<int>& heights) {
int n= heights.size();
int n = heights.size();

// Get next and previous smaller elements' indices
vector<int> next = nextSmallerElement(heights, n);
vector<int> prev = prevSmallerElement(heights, n);

int maxArea = 0;

// Calculate area for each histogram bar
for (int i = 0; i < n; i++) {
int height = heights[i];
int width = next[i] - prev[i] - 1; // Calculate width of the rectangle
int area = height * width;
maxArea = max(maxArea, area); // Update max area
}

return maxArea;
}
};
// class Solution {
// private:
// vector<int> nextSmallerElement(vector<int> arr, int n) {
// stack<int> s;
// s.push(-1);
// vector<int> ans(n);

// for(int i=n-1; i>=0 ; i--) {
// int curr = arr[i];
// while(s.top() != -1 && arr[s.top()] >= curr)
// {
// s.pop();
// }
// //ans is stack ka top
// ans[i] = s.top();
// s.push(i);
// }
// return ans;
// }

// vector<int> prevSmallerElement(vector<int> arr, int n) {
// stack<int> s;
// s.push(-1);
// vector<int> ans(n);

// for(int i=0; i<n; i++) {
// int curr = arr[i];
// while(s.top() != -1 && arr[s.top()] >= curr)
// {
// s.pop();
// }
// //ans is stack ka top
// ans[i] = s.top();
// s.push(i);
// }
// return ans;
// }

// public:
// int largestRectangleArea(vector<int>& heights) {
// int n= heights.size();

vector<int> next(n);
next = nextSmallerElement(heights, n);
// vector<int> next(n);
// next = nextSmallerElement(heights, n);

vector<int> prev(n);
prev = prevSmallerElement(heights, n);
// vector<int> prev(n);
// prev = prevSmallerElement(heights, n);

int area = INT_MIN;
for(int i=0; i<n; i++) {
int l = heights[i];
// int area = INT_MIN;
// for(int i=0; i<n; i++) {
// int l = heights[i];

if(next[i] == -1) {
next[i] = n;
}
int b = next[i] - prev[i] - 1;
int newArea = l*b;
area = max(area, newArea);
}
return area;
}
};
// if(next[i] == -1) {
// next[i] = n;
// }
// int b = next[i] - prev[i] - 1;
// int newArea = l*b;
// area = max(area, newArea);
// }
// return area;
// }
// };