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 →

  • sabsfilho
    5 months ago+ 0 comments

    for each point, count LEFT and RIGHT length, then evaluate AREA = HEIGHT * (LEFT + RIGHT + 1). important to cast long for c# version

        public static long largestRectangle(List<int> h)
        {
            long max = 0;
            for(int i=0;i<h.Count;i++)
            {
                int n = h[i];
                int l = 0;
                for(int j=i-1;j>=0;j--)
                {
                    int nl = h[j];
                    if (nl < n) break;
                    l++;
                }
                int r = 0;
                for(int j=i+1;j<h.Count;j++)
                {
                    int nr = h[j];
                    if (nr < n) break;
                    r++;
                }
                long a = (long)n*(long)(l+r+1);
                if (a > max) max = a;
            }
            return max;
        }
    
    0|
    Permalink
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy