logo CodeStepByStep logo

largestRectangleArea

Language/Type: Java arrays
Related Links:

Write a recursive method named largestRectangleArea that accepts an array of bar heights as its parameter and finds the largest rectangular area that can be drawn in a contiguous region inside those bars. For example, if the bar heights are {1, 2, 1, 6, 5, 2, 3}, the bars could be thought of as follows:

      #
      # #
      # #
      # #   #
  #   # # # #
# # # # # # #
1 2 1 6 5 2 3

The largest rectangle that can be made within these bars has an area of 10 (2 * 5):

      #
     +---+
     |# #|
     |# #|
     |# #|  #
  #  |# #|# #
# # #|# #|# #
     +---+
1 2 1 6 5 2 3

If the array is empty, return 0. If it contains only a single bar, return that bar's height.

Method: Write a Java method as described, not a complete program or class.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.