Menu
CoddyTech

29. Largest Rectangle in Histo

Lesson 30 of 31 in Coddy's 30 Days of Logic Building in Javascript course.

challenge icon

Challenge

Medium

Given an array of non-negative integers representing the height of bars in a histogram, find the largest rectangle that can be formed from the histogram.

  • Input: <strong>[2, 1, 5, 6, 2, 3]</strong>
  • Expected Output: <strong>10</strong> (The largest rectangle is formed by heights 5 and 6)

Note - The area of this rectangle is calculated as the width (number of bars) times the minimum height of the bars within the rectangle: <strong>2 * min(5, 6) = 2 * 5 = 10</strong>.

Try it yourself

function largestRectangleArea(heights) {
    // write your code below 
    
}

All lessons in 30 Days of Logic Building in Javascript

Practice on your own: Online JavaScript compiler