We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • HackerRank Home

    HackerRank

  • |
  • Prepare
  • Certify
  • Compete
  • Apply
  • Hiring developers?
  1. Prepare
  2. Data Structures
  3. Stacks
  4. Largest Rectangle
  5. Discussions

Largest Rectangle

Problem
Submissions
Leaderboard
Discussions
Editorial

    You are viewing a single comment's thread. Return to all comments →

  • advaitpatole10
    4 months ago+ 0 comments

    My c++ code using recursion: T.C -> O(n) S.C -> O(n)

    long largestRectangle(vector<int> h) {
        long maxArea;
        //base case
        if (h.empty()) {
            return 0;
        }
        //second stack to divide the current stack in two
        vector<int> a;
        //minimum element present in current stack
        int minimum = *min_element(h.begin(),h.end());
    		//divide the stack
        while (h.back()!=minimum) {
            a.push_back(h.back());
            h.pop_back();
        }
        // find area using minimum value stack 
        long ar = minimum * (a.size() + h.size());
        //remove the minimum value
        h.pop_back();
        //compare the max area of current stack and that from the divided stack using recursion
        maxArea = max(ar,max(largestRectangle(a),largestRectangle(h)));
        return maxArea;
    }
    
    0|
    Permalink
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy