-
Notifications
You must be signed in to change notification settings - Fork 89
Open
Description
| def maxRectangleInHistogram(heights: List[Int]): Int = { |
def maxRectangleInHistogram(heights: List[Int]): Int = {
@tailrec def solve(stack: List[(Int, Int)], remaining: List[(Int, Int)],maxArea:Int): Int = {
def area(x: Int, y: Int) = (heights.length - remaining.length - x) * y
(stack, remaining) match {
case ( Nil, Nil) => maxArea
case ((y, x) :: rest, Nil) => solve( rest, Nil, maxArea max area(x, y))
case ((y, x) :: rest, (h, i) :: hs) if h <= y => solve( rest, (h, x) :: hs, maxArea max area(x, y))
case ( _, block :: hs) => solve(block :: stack, hs, maxArea)
}
}
solve(Nil, heights.zipWithIndex,0)
}Metadata
Metadata
Assignees
Labels
No labels