logo CodeStepByStep logo

largestRectangleArea

Language/Type: C++ Vector
Related Links:

Write a recursive function named largestRectangleArea that accepts a reference to a Vector 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 vector is empty, return 0. If it contains only a single bar, return that bar's height.

Function: Write a C++ function as described, not a complete program.

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.