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 →

  • im_awesome_hari1
    5 months ago+ 0 comments

    Not the best algorithm, but works anyway!

    let area = 0;
                for(let i = 0; i < h.length; i++){
                    let height = h[i]
                    let length = 1 
                    let area1 = 0;
                    let area2 = 0;
                    let j = i+1;
                        //forward traversal
                        while(j < h.length){
                            if(height > h[j])break
                            if(height <= h[j])length++
                            j++           
                        }
                        area1 = (height*length);
                        j = i-1;
                        length = 1;
                        // Backward Traversal
                        while(j >= 0){
                            if(height > h[j])break
                            if(height <= h[j])length++
                            j--;         
                        }
                        area2 = (height*(length-1));
                    if(area < (area1+area2))area = (area1+area2) 
                }
                return area;
    
    0|
    Permalink
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy