Problem · Array

Maximum Capped Histogram Area After One Removal

Learn this problem
MediumMyntra logoMyntraINTERNOA

Problem statement

You are given positive building heights heights and a positive area limit limit. Remove exactly one building; the buildings on its two sides become adjacent.

For the remaining histogram, compute its ordinary largest axis-aligned rectangle area. A removal is eligible only when that largest area is at most limit. Return the greatest eligible largest-rectangle area over all removals. If no removal is eligible, return 0.

Function

maxCappedAreaAfterRemoval(heights: long[], limit: long) → long

Examples

Example 1

heights = [2,1,5,6,2,3]limit = 10return = 10

Removing the height 1 leaves [2, 5, 6, 2, 3], whose largest rectangle has area 10. It is eligible and no eligible removal produces a larger area.

Example 2

heights = [2,4,2]limit = 6return = 4

Every possible removal leaves a two-building or one-building histogram whose largest rectangle area is 4.

Example 3

heights = [3,3,3]limit = 4return = 0

After any removal, the two remaining buildings form a largest rectangle of area 6, so no removal is eligible.

Constraints

  • 1 <= heights.length <= 2000.
  • 1 <= heights[i] <= 10^9.
  • 1 <= limit <= 10^18.

More Myntra problems

drafts saved locally
public long maxCappedAreaAfterRemoval(long[] heights, long limit) {
    // Return the best eligible largest-rectangle area.
}
heights[2,1,5,6,2,3]
limit10
expected10
checking account