Finding the largest rectangle in the bar chart -- O (n) time complexity java Implementation

Source: Internet
Author: User

Recently, I was looking for a job. I learned this question and used java to implement the O (n) time complexity algorithm.

A set of non-negative integers are given to represent a bar chart. An algorithm is designed to obtain the area of the largest rectangle in the bar chart. For example, enter the following data:, 5, 3. Each number indicates the height of a column. The width of the column is 1 by default, the area of the largest rectangle is 8.

Idea: Use a stack to save input columns. Each column contains two pieces of information: (1) height of the column bar; (2) x coordinate (index) of the column bar ). The column entries in the array are prepared into the stack in order. The inbound stack condition is: when the height of the inbound stack Element e is greater than or equal to the height of the element at the top of the stack, Element e is written into the stack; otherwise, outputs the top element of the stack and updates the maxValue of the maximum rectangle.

The algorithm for updating maxValue is as follows:

1. Calculate the area of the rectangle with the width of the elements at the top of the current stack: tmpValue = topElement. height * (e. index-topElement. index );

2. Update maxValue: maxValue = (maxValue> tmpValue )? MaxValue: tmpValue;

After all elements are added to the stack, the remaining elements in the stack are output to the stack in sequence, and the value of maxValue is updated according to the same idea.

Java implementation:

Import java. util. arrayList; import java. util. list; import java. util. stack; public class MaxRectangle {// use the Stack to store each column. When the height of the column to be written into the Stack is smaller than the height of the column at the top of the Stack, first let the top element of the stack go out of the stack, and calculate the maximum rectangle size. public int maxRectangleValue (int [] array) {if (array = null | array. length <= 0) return-1; int maxValue = 0; List <Element> inputList = new ArrayList <Element> (); int len = array. length; for (int I = 0; I <len; I ++) {Element element = new Ele Ment (array [I], I); inputList. add (element) ;}// start the Stack operation <Element> stack = new Stack <Element> (); for (Element e: inputList) {if (stack. empty () stack. add (e); else {while (e. height <stack. peek (). height) {// outputs the stack and calculates the maximum size of the rectangle Element topElement = stack. pop (); int tmpValue = topElement. height * (e. index-topElement. index); // height * widthif (tmpValue> maxValue) maxValue = tmpValue; if (stack. empty () break;} // Stack. add (e) ;}/// all elements contained in the stack are pushed out of the stack, and the maximum rectangle size is updated while (! Stack. empty () {Element topElement = stack. pop (); int tmpValue = topElement. height * (len-1)-topElement. index + 1); // height * widthif (tmpValue> maxValue) maxValue = tmpValue;} return maxValue;} public static void main (String [] args) {int [] array = {2, 1, 4, 5, 3, 3}; MaxRectangle mr = new MaxRectangle (); System. out. println (mr. maxRectangleValue (array) ;}} class Element {public int height; // The height of each bar (width: 1) public int index; // The x coordinate value of each bar, representing the relative order of their appearance public Element (int height, int index) {this. height = height; this. index = index ;}}

Algorithm analysis:

All array elements need to be pushed to the stack at a time, so the total time complexity is: O (n)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.