-1
    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());

Shroud
  • 35
  • 7
  • 1
    Related: https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h – RoQuOTriX Jul 20 '22 at 09:35
  • 2
    there is no member `reverse` in `std::vector`. You probably used `std::reverse` before – 463035818_is_not_an_ai Jul 20 '22 at 09:35
  • Used std::vector , working fine now – Shroud Jul 20 '22 at 09:37
  • but reverse(a.begin(), a.end()); worked for me earlier – Shroud Jul 20 '22 at 09:39
  • you are expected to do a minimum of research before posting the question. https://en.cppreference.com/w/cpp/container/vector and https://en.cppreference.com/w/cpp/algorithm/reverse – 463035818_is_not_an_ai Jul 20 '22 at 09:40
  • @Shroud You did not treat is as a member when it worked, and there was a `using namespace std;` so you could write "reverse" rather than "std::reverse". Perhaps it's time to review some basics in [a good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list)? – molbdnilo Jul 20 '22 at 09:44
  • 3
    `left.reverse(left.begin(),left.end());` and `reverse(left.begin(),left.end());` are not the same code at all. – john Jul 20 '22 at 10:07
  • Minor point: in that `if ... else if` ladder you don't need to keep testing `s.size() > 0`; you already know that that's true, because if it wasn't, the first branch (`if (s.size() == 0)`) would have been selected. Also, if `s.top().first <= heights[i]` is true, the third branch won't be reached. Really, the tests just need to be `if (s.size() == 0) ... else if (s.size() <= heights[i]) ... else ...`. – Pete Becker Jul 20 '22 at 12:26

1 Answers1

1

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.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • I have fixed the code but now the problem is vector makes its size double unnecessarily which results in the wrong output. The extra indices contain zero creating problems in the output. I can fix this but it might again create problems in the future. I have allocated vectors size of height array which is enough. There is no extra space required at run time. Why it makes size double ? – Shroud Jul 20 '22 at 11:11
  • @Shroud You should restore the original code and close the question selecting the best answer. Otherwise the question and answers will confuse readers. If you have a new problem then ask a new question after closing this question. – Vlad from Moscow Jul 20 '22 at 12:18
  • I have fixed it. Thank you will make new post for other issue. – Shroud Jul 20 '22 at 13:59
  • could you please answer regarding vector as there is limit on asking question – Shroud Jul 20 '22 at 14:03
  • @Shroud Which vector in which function? – Vlad from Moscow Jul 20 '22 at 14:04
  • @Shroud It seems you mean the following int n = heights.size(); vector v(n); . After these lines the vector v is enlarged using the method push_back. It seems you need not initially to set the number of elements in the vector v equal to n. It seems all elements should be created in the vector using the method push_back. – Vlad from Moscow Jul 20 '22 at 14:10