R/get_max_area_bf.R

Defines functions get_max_area_bf

Documented in get_max_area_bf

#' An algorthm to identify the largest rectangle in a histogram
#'
#' @param heights a list of the heights of the histogram.
#' @return list
#' @export
#' @importFrom magrittr %>%


get_max_area_bf <- function(heights){

  # browser()
  maxarea = 0

  for(i in 1:length(heights)){
    minheight = heights[i]
    for (j in seq(i,length(heights),1)){
      minheight = min(minheight, heights[j])
      currarea = (j - i +1) * minheight
      maxarea = max(currarea,maxarea)
      if(currarea>=maxarea){
        maxarea_i <- i
        maxheight <-  heights[j]
      }
    }
  }
  list(col=maxarea_i,maxheight=maxheight,maxarea=maxarea)

}
ianmoran11/mmtable2 documentation built on Dec. 20, 2021, 5:58 p.m.