Javatpoint Logo
Javatpoint Logo

Largest Rectangle Hackerrank Solution in Python

Hackerrank is a popular online platform for programming challenges, and one of the most popular problems on the platform is the "Largest Rectangle" problem. This problem requires you to find the largest rectangular area in a given histogram. In this article, we will walk you through the solution of this problem in Python.

Problem statement

You are given a list of heights representing a histogram's heights. Your task is to find the largest rectangular area in the histogram. The width of each rectangle is considered to be 1, and the height of the corresponding histogram bar gives the height.

Solution

To solve this problem, we can use the stack data structure. We start by initializing an empty stack and iterating through the list of heights. For each height, we perform the following steps:

  1. If the stack is empty or the current height is greater than or equal to the height at the top of the stack, we push the current index onto the stack.
  2. If the current height is less than the height at the top of the stack, we pop the top index from the stack and calculate the area of the rectangle formed by the height at the popped index and the current index.
  3. We repeat step 2 until the height at the top of the stack is less than or equal to the current height. For each popped index, we calculate the area of the rectangle as (current index - popped index) * height at popped index.
  4. We push the current index onto the stack.
  5. Once we have iterated through all the heights, we pop any remaining indices from the stack and calculate the area of the rectangle using the same formula as in Step 3.
  6. We keep track of the maximum area calculated at each step and return the maximum area as the answer.

Example:

Let's see how this solution looks in Python code:

In this code, we initialize an empty stack and a variable to store the maximum area. After that, we use a while loop to iterate through the heights list. Within the loop, we check if the stack is empty or if the current height is greater than or equal to the height at the top of the stack. If either of these conditions is true, we push the current index onto the stack and increment i by 1.

If the current height is less than the height at the top of the stack, we pop the top index from the stack and calculate the area of the rectangle formed by the height at the popped index and the current index. We use the formula (i - stack[-1] - 1) * h[top] to calculate the area, where i is the current index, stack[-1] is the index at the top of the stack, and h[top] is the height at the popped index.

We repeat step 2 until the height at the top of the stack is less than or equal to the current height. For each popped index, we calculate the area of the rectangle as (i - stack[-1] - 1) * h[top]. If the stack is empty, we use the formula i * h[top] instead.

Let's test our solution on a sample input:

Output:

12

Here, we have a histogram with heights [6, 2, 5, 4, 5, 1, 6]. The largest rectangle in this histogram has height 4 and width 3, so its area is 12. Our function correctly returns 12 as the output.

Conclusion:

In conclusion, we have seen how to solve the "Largest Rectangle" problem on Hackerrank using the stack data structure in Python. This problem is a great example of how to use the stack data structure to solve a complex problem efficiently. The key insight is that we can use the stack to keep track of the indices of bars in the histogram that form rectangles of increasing height. By doing this, we can avoid computing areas that we know will be smaller than the maximum area we have found so far.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA