id/email
password
forgot password | create account
about | help | code help+videos | done | prefs
CodingBat code practice

 

bryce.hulett@hotmail.com > largestRectangleArea
prev  |  next  |  chance

Write a 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.

The challenge is to solve this problem in linear time.
Brute force will pass the test cases but is too slow.

largestRectangleArea([2, 1, 5, 6, 2, 3]) → 10
largestRectangleArea([5, 4, 5, 1, 6, 5, 2, 7]) → 12
largestRectangleArea([3, 4, 1, 6, 6, 1, 2, 3, 2, 2, 3, 8, 2, 3, 1]) → 16

...Save, Compile, Run (ctrl-enter)

public int largestRectangleArea(int[] heights){ }

Editor font size %:
Shorter output


Forget It! -- delete my code for this problem

Progress graphs:
 Your progress graph for this problem
 Random user progress graph for this problem
 Random Epic Progress Graph

Java Help

Misc Code Practice

Difficulty: 200 Post-solution available

Copyright Nick Parlante 2017 - privacy