LeetCode,leetcodeoj

來源:互聯網
上載者:User

LeetCode,leetcodeoj

題目:

Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram.


Above is a histogram where width of each bar is 1, given height = [2,1,5,6,2,3].


The largest rectangle is shown in the shaded area, which has area = 10 unit.

For example,
Given height = [2,1,5,6,2,3],
return 10.

思路:

1)對每個直方維護它能拓展到左右兩邊最遠的邊界。

package area;public class LargestRectangleInHistogram {    public int largestRectangleArea(int[] height) {        int n = height.length;        int max = 0;        int[] left = new int[n];        int[] right = new int[n];        for (int pos = 0; pos < n; ++pos) {            left[pos] = right[pos] = pos;            int i = pos - 1;            for (; i >= 0; --i) {                if (height[i] < height[pos]) {                    left[pos] = i + 1;                    break;                }            }            if (i == -1) left[pos] = 0;                        for (i = pos + 1; i < n; ++i) {                if (height[i] < height[pos]) {                    right[pos] = i - 1;                    break;                }            }            if (i == n) right[pos] = n - 1;            int area = height[pos] * (right[pos] - left[pos] + 1);            if (area > max)                max = area;        }        return max;    }        public static void main(String[] args) {        // TODO Auto-generated method stub        int[] height = { 1,1,1,1 };        LargestRectangleInHistogram l = new LargestRectangleInHistogram();        System.out.println(l.largestRectangleArea(height));    }}

 

2)維護一個棧,對比當前元素,如果大於當前元素,則pop,否則push當前元素的index

package area;import java.util.Stack;public class LargestRectangleInHistogram {    public int largestRectangleArea(int[] height) {        Stack<Integer> stack = new Stack<Integer>();        int max = 0;        int n = height.length;        int[] newHeight = new int[n + 1];        for (int i = 0; i < n; ++i) newHeight[i] = height[i];        newHeight[n] = 0;        int i = 0;        while (i < n + 1) {            if (stack.isEmpty() || newHeight[stack.peek()] <= newHeight[i]) {                stack.push(i++);            } else {                int index = stack.pop();                int area = (stack.isEmpty() ? i : (i - stack.peek() - 1)) * newHeight[index];                max = (max < area ? area : max);            }        }                return max;    }        public static void main(String[] args) {        // TODO Auto-generated method stub        int[] height = { 1,1 };        LargestRectangleInHistogram l = new LargestRectangleInHistogram();        System.out.println(l.largestRectangleArea(height));    }}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.