# Largest Rectangle in Background
Imagine a photo taken to be used in an advertisement. The background on the left of the motive is whitish and you want to write some text on that background. So you scan the photo with a high resolution scanner and, for each line, count the number of pixels from the left that are sufficiently white and suitable for being written on. Your job is to find the area of the largest text box you can place on those pixels.
Example:
In the figure below, the whitish background pixels of the scanned photo are represented by asterisks.
```
*********************************
*********
*******
******
******
******
**************
**************
**************
***************
*********************
``` 
If you count the pixels on each line from the left you get the list (or array, depending on which language you are using) `[33, 9, 7, 6, 6, 6, 14, 14, 14, 15, 21]`. The largest reactangle that you can place on these pixels has an area of 70, and is represented by the dots in the figure below.
```
*********************************
*********
*******
******
******
******
..............
..............
..............
..............*
..............*******
``` 
Write a function that, given a list of the number whitish pixels on each line in the background, returns the area of the largest rectangle that fits on that background.
def largest_rect(histogram):
  dim = len(histogram)
  if dim == 0: return 0
  max_el = max(histogram)
  min_el = min(histogram)
  if max_el == min_el: return dim * max_el
  area = max_el if max_el > min_el * dim else min_el * dim
  
  arr_work = [histogram]
  while len(arr_work) > 0:
    h = arr_work.pop()
    dim = len(h)
    max_el = max(h)
    min_el = min(h)
    if max_el * dim < area:
      continue
    if max_el == min_el: 
      if area < dim * max_el:
        area = dim * max_el
      continue
    area = min_el * dim if area < min_el * dim else area
    split_index = h.index(min_el)
    if split_index * max_el >= area:
      arr_work.append(list(reversed(h[:split_index])))
    if (dim - 1 - split_index) * max_el >= area:
      arr_work.append(list(reversed(h[(split_index + 1):])))

  return area
def largest_rect(histogram):
  dim = len(histogram)
  if dim == 0: return 0
  max_el = max(histogram)
  min_el = min(histogram)
  if max_el == min_el: return dim * max_el
  area = max_el if max_el > min_el * dim else min_el * dim
  
  arr_work = [histogram]
  while len(arr_work) > 0:
    h = arr_work.pop()
    dim = len(h)
    max_el = max(h)
    min_el = min(h)
    if max_el * dim < area:
      continue
    if max_el == min_el: 
      if area < dim * max_el:
        area = dim * max_el
      continue
    area = min_el * dim if area < min_el * dim else area
    split_index = h.index(min_el)
    if split_index * max_el >= area:
      arr_work.append(list(reversed(h[:split_index])))
    if (dim - 1 - split_index) * max_el >= area:
      arr_work.append(list(reversed(h[(split_index + 1):])))

  return area