最近在準備找工作,知道了這道題,用java實現了O(n)時間複雜度的演算法。
具體題目如下:給一組非負的整數來表示一個柱狀圖,設計一個演算法獲得柱狀圖中最大矩形的面積。比如,輸入如下資料:2,1,4,5,1,3,3 ,其中每個數表示一個柱狀條的高度,柱狀條的寬度為預設值1,則計算得最大矩形的面積為8。
思路:使用一個棧來儲存輸入柱狀條,每個柱狀條包含兩個資訊:(1)柱狀條的高度(height);(2)柱狀條的x座標(index) 。數組中的柱狀條按序準備入棧,入棧的條件:當入棧元素e的高度>=棧頂元素的高度時,元素e入棧;否則,將棧頂元素出棧,同時更新最大矩形maxValue的值。
更新maxValue的演算法如下:
1. 計算以當前棧頂元素的高度為寬度的矩形的面積:tmpValue = topElement.height * (e.index - topElement.index);
2. 更新maxValue: maxValue = (maxValue > tmpValue) ? maxValue : tmpValue;
所有元素入棧完畢,將棧中剩餘的元素依次出棧,同時按照相同的思路更新maxValue的值。
java實現:
import java.util.ArrayList;import java.util.List;import java.util.Stack;public class MaxRectangle {// 使用棧來儲存每個柱狀條,噹噹前準備入棧的柱狀條的高度小於當前棧頂的柱狀條高度時,先讓棧頂元素出棧,同時計算最大的矩形大小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 Element(array[i], i);inputList.add(element);}// 開始入棧操作Stack<Element> stack = new Stack<Element>();for (Element e : inputList) {if (stack.empty())stack.add(e);else {while (e.height < stack.peek().height) { // 出棧,並計算最大矩形大小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);}}// 將堆棧中包含的所有元素出棧,同時更新最大的矩形大小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,1,3,3};MaxRectangle mr = new MaxRectangle();System.out.println(mr.maxRectangleValue(array));}}class Element {public int height; // 每一個柱狀條的高度(寬度為1)public int index; // 每個柱狀條的x座標值,代表它們出現的相對次序public Element(int height, int index) {this.height = height;this.index = index;}}
演算法分析:
所有數組元素需要一次進棧和一次出棧,所以總的時間複雜度為:O(n)