class Solution {
public:
void print (vector<int> array)
{
for (int i=0;i<array.size();i++)
{
cout<<array[i]<<" ";
}
cout<<endl;
}
vector<int> nsr(vector<int> heights)
{
int n = heights.size();
vector<int> v(n);
stack <pair<int,int>> s;
for (int i=0 ;i<n;i++)
{
if (s.size()== 0)
{
v.push_back(-1);
}
else if (s.size()>0 && s.top().first<= heights[i])
{
v.push_back (s.top().second);
}
else if (s.size()>0 && s.top().first >=heights[i])
{
while (s.size()>0 && s.top().first>= heights[i])
{
s.pop();
}
if (s.size() == 0)
v.push_back(-1);
else
v.push_back (s.top().second);
}
s.push({heights[i], i});
}
return v ;
}
vector<int> nsl(vector<int> heights)
{
int n = heights.size();
vector<int> v(n);
print(v);
stack <pair<int,int>> s;
for (int i=n-1; i>=0;i--)
{
if (s.size()== 0)
{
v.push_back(n);
}
else if (s.size()>0 && s.top().first<= heights[i])
{
v.push_back (s.top().second);
}
else if (s.size()>0 && s.top().first >=heights[i])
{
while (s.size()>0 && s.top().first>= heights[i])
{
s.pop();
}
if (s.size()== 0)
v.push_back(n);
else
v.push_back (s.top().second);
}
s.push({heights[i], i});
}
// print (v);
return v;
}
int largestRectangleArea(vector<int>& heights) {
vector<int> width ;
vector <int> left= nsl(heights);
left.reverse(left.begin(),left.end());
vector <int> right = nsr(heights);
// print(left);
// print(right);
for (int i = 0 ;i< heights.size()-1;i++)
{
int element = left[i] - right[i] - 1;
width.push_back (element);
}
int area = INT_MIN;
for (int i =0 ;i<heights.size()-1;i++)
{
int newarea = heights[i]* width[i];
area = max(area, newarea);
//cout<< area <<endl;
}
return area ;
}
};
I am using reverse() in vector but it's showing an error. I have tried using header files but the error is same. I had used reverse with vector many times but it never gave an error.
Error :
Line 80: Char 14: error: no member named 'reverse' in 'std::vector<int, std::allocator>' left.reverse(left.begin(),left.end());
There is no member function reverse
in the class template std::vector
. So this statement
left.reverse(left.begin(),left.end());
is invalid.
You should use the standard algorithm std::reverse
declared in the header <algorithm>
as for example
reverse(left.begin(),left.end());
There are other problems with your code.
For example in this declaration
vector <int> right = right(heights);
the declared identifier right
hides a function right
used as an initializer expression. (I suppose that in the right side there is used a call of a function with the name right
.) That is in the both sides there is used the vector right
. So the declaration does not make a sense.
If you mean the vector right
in the both sides then just write
vector<int> right(heights);
Or it is unclear where the name element
used in this for loop
for (int i = 0 ;i< heigths.size()-1;i++)
{
element = left[i] - rigth[i] - 1;
width.push_back (element);
}
is declared.
Or for example this for loop
for (int i =0 ;i<heights.size()-1;i++)
{
int area = INT_MIN;
int newarea = heights[i]* width[i];
area = max(area, newarea);
}
does not find the maximum area.
And moreover outside the loop the name area
used in the return statement
return area ;
is undefined.
You need to revise your code entirely from the very beginning.