Question:
My code:
import java.util.Stack;
public class Solution {
public int largestRectangleArea(int[] height) {
if (height == null || height.length == 0)
return 0;
int count = height.length;
int[] area = new int[count + 1];
for (int i = 0; i < count; i ++)
area[i] = height[i];
area[count] = 0;
int maxArea = 0;
Stack<Integer> s = new Stack<Integer>();
for (int i = 0; i < count + 1; i++) {
if (s.isEmpty() || area[i] >= area[s.peek()])
s.push(i);
else {
while (!s.isEmpty() && area[i] < area[s.peek()]) {
int top = area[s.pop()];
int currArea = top * (s.isEmpty() ? i : i - s.peek() - 1);
if (currArea > maxArea)
maxArea = currArea;
}
s.push(i);
}
}
return maxArea;
}
public static void main(String[] args) {
int[] height = {2, 1, 5, 6, 2, 3};
Solution test = new Solution();
System.out.println(test.largestRectangleArea(height));
}
}
My test result:
這次作業是因為之前的 Max Square才找到的。因為之前看了相關的思路,所以這道題目就比較順得寫下來了。雖然最后還是出了一些小問題。
他的思路是什么呢?
其實就是將數由小到大放進棧中。當碰到一個比棧頂數(當前最大數)小的數時,將棧中比該數大的數都pop出來,算面積。然后將該數壓入棧中。也就說,棧中的數一直是從小到大排列的。然后碰到小的,就將比其大的pop出來,一個個算面積,然后求出最大面積。
因為值大的不會和值小的構成矩形,只會和值大的構成矩形。所以比該數大的就全部pop出來。然后計算出來的面積是完整的。然后比該值小的數依舊存在棧里面,以后該數pop時,需要連同這些小的一起計算面積。
核心原則是:
小的不能和大的構成矩形算面積。
大的可以和小的構成矩形算面積(切掉頭上方那些多余部分)。
**
總結:還是一種思路問題吧。我覺得用棧這種數據結構用的很妙。同樣需要考慮的問題是,什么樣的數可以進入棧。pop出棧的數據又需要滿足哪些條件。
**
有點疲倦。但寫著寫著腦子又清醒了。
Anyway, Good luck, Richardo!
My code:
public class Solution {
public int largestRectangleArea(int[] height) {
if (height == null || height.length == 0)
return 0;
Stack<Integer> s = new Stack<Integer>();
int[] helper = new int[height.length + 1];
for (int i = 0; i < height.length; i++)
helper[i] = height[i];
int max = 0;
int zeroLocation = -1;
for (int i = 0; i < helper.length; i++) {
if (s.isEmpty() || helper[i] >= helper[s.peek()]) { // insert into stack with increasing elem values
s.push(i);
}
else {
int end = i; // get the end index
while (!s.isEmpty()) {
if (helper[s.peek()] >= helper[i]) { // if bigger, pop, area = value * (end - begin)
int index = s.pop();
int area = 0;
if (s.isEmpty()) {
int begin = zeroLocation;
area = helper[index] * (i - begin - 1);
}
else {
int begin = s.peek();
area = helper[index] * (i - begin - 1);
}
max = Math.max(max, area); // get the biggest area
}
else
break;
}
if (helper[i] > 0)
s.push(i); // push this index into stack
else
zeroLocation = i;
}
}
return max;
}
}
自己又寫了一遍,終于寫出來了。。花費了大約一小時,許多嘗試。
最大的問題出在:
左邊的邊界是 s.peek() + 1;
所以我還考慮了0的情況,覺得0不該插入棧中,然后拿一個變量來記錄每一個0的位置,然后算出每段的區域面積,如果棧空了,就 i - zeroLocation + 1
這個挺麻煩的。然后用我以前寫的代碼,網上看的,就簡潔很多。0也插入進去。
如果棧空了,之前的數中一定沒存在過0,否則0不會彈出。
然后,算總面積,寬為 i
如果棧不空,就拿 s.peek() + 1作為左邊界,不用再去更新zeroLocation,對所有數字也是統一處理,不需要分非0或0.
簡潔了很多。
這道題目是做
- Maximal Rectangle
http://www.lxweimin.com/p/c86e5dec7514
想起來的。之前也做過這道題目,所以知道思路,就是把他當做等高地圖那樣來做。每個值表示的就是一個直方圖,然后將每行的直方圖作為參數送到這個函數里求出最大面積。
遍歷所有行,直到找到最大值。
Anyway, Good luck, Richardo!
My code:
public class Solution {
public int largestRectangleArea(int[] heights) {
if (heights == null || heights.length == 0) {
return 0;
}
Stack<Integer> s = new Stack<Integer>();
int[] h = new int[heights.length + 1];
for (int i = 0; i < heights.length; i++) {
h[i] = heights[i];
}
int max = 0;
for (int i = 0; i < h.length; i++) {
if (s.isEmpty() || h[i] >= h[s.peek()]) {
s.push(i);
}
else {
while (!s.isEmpty() && h[i] < h[s.peek()]) {
int high = h[s.pop()];
int end = i;
int begin = (s.isEmpty() ? 0 : s.peek() + 1);
int area = high * (end - begin);
max = Math.max(max, area);
}
s.push(i);
}
}
return max;
}
}
差不多的思路。一個棧。
最重要的問題是,
彈出一個值算面積,一定要首先確定他的begin and end
Anyway, Good luck, Richardo! -- 08/18/2016
My code:
public class Solution {
public int largestRectangleArea(int[] heights) {
if (heights == null || heights.length == 0) {
return 0;
}
int[] nums = new int[heights.length + 1];
for (int i = 0; i < heights.length; i++) {
nums[i] = heights[i];
}
Stack<Integer> st = new Stack<Integer>();
int i = 0;
int max = 0;
while (i < nums.length) {
if (st.isEmpty() || nums[st.peek()] <= nums[i]) {
st.push(i);
i++;
}
else {
int index = st.pop();
int right = i - 1;
int left = st.isEmpty() ? 0 : st.peek() + 1;
int area = (right - left + 1) * nums[index];
max = Math.max(max, area);
}
}
return max;
}
}
注意找到這個直方塊,比他大的最左邊和最右邊界限。
Anyway, Good luck, Richardo! -- 09/17/2016