由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - Largest Rectangle in Histogram
相关主题
Largest Rectangle in Histogram不同算法的复杂度问个题
challenge: 找bug贡献Google电面面经(不小心误删除了,再发一遍并update)
leetcode: largest rectangle in histogram求帮助leetcode container with most water
google的一道题求解Maximal Rectangle O(mn) 解法 非 histogram
我也来问问LC的Maximal RectangleO(NlogN) largest rectangle in histogram
histogram问题leetcode一道题
问个largest rectangle in histogram的问题再问Maximal Rectangle的N^2解法
面试遇到像largest rectangle in histogram这样的高难题要怎么办?问一道题
相关话题的讨论汇总
话题: height话题: int话题: left话题: right话题: max
进入JobHunting版参与讨论
1 (共1页)
b**m
发帖数: 1466
1
没想出用stack方法,我这个也是O(n)吧?
public class Solution {
public int largestRectangleArea(int[] height) {
if(height.length ==0 ){
return 0;
}
int[] left= new int[height.length]; // width on its left side(and
itself)
int[] right= new int[height.length]; // width on its right side(and
itself)

left[0] = 1;
right[right.length-1] = 1;


for(int i=1;i< height.length; i++){
if(height[i]> height[i-1]){
left[i] = 1;
}else if(height[i] == height[i-1]){
left[i] = left[i-1] +1;
}else{
left[i] = 1;
int j = i-1;
while(j>=0 && height[i] <= height[j]){
left[i] += left[j];
j-= left[j]; //jump
}
}
}

for(int i = right.length -2;i>=0;i--){
if(height[i]>height[i+1]){
right[i] = 1;
}else if(height[i] == height[i+1]){
right[i] = right[i+1] +1;
}else{
right[i] = 1;
int j = i+1;
while(j < right.length && height[i] <= height[j]){
right[i] += right[j];
j+= right[j]; //jump
}
}
}

int max = 0;
for(int i=0;i int area = (left[i] + right[i] -1) *height[i];
if(area> max){
max = area;
}
}
return max;
}
}
b********g
发帖数: 28
2
public class Solution {
public int largestRectangleArea(int[] height) {
// Start typing your Java solution below
// DO NOT write main() function
if(height == null || height.length == 0) return 0;
int n = height.length;
int[] left = new int[n];
Stack stack = new Stack();
int max = 0;
for(int i = 0; i < n; i++){
if(stack.empty() || height[stack.peek()] <= height[i]){
left[i] = i;
stack.push(i);
}else{
while(!stack.empty() && height[stack.peek()] > height[i]){
int idx = stack.pop();
left[i] = left[idx];
max = Math.max(max, height[idx] * (i - left[idx]));
}
stack.push(i);
}
}
while(!stack.empty()){
int idx = stack.pop();
max = Math.max(max, height[idx] * (n - left[idx]));
}
return max;
}
}
1 (共1页)
进入JobHunting版参与讨论
相关主题
问一道题我也来问问LC的Maximal Rectangle
求“bar组成的histogram求最大内切矩形”的link...histogram问题
大家有没有经历过interviewer出错的时候?问个largest rectangle in histogram的问题
这个histgram的最大面积问题面试遇到像largest rectangle in histogram这样的高难题要怎么办?
Largest Rectangle in Histogram不同算法的复杂度问个题
challenge: 找bug贡献Google电面面经(不小心误删除了,再发一遍并update)
leetcode: largest rectangle in histogram求帮助leetcode container with most water
google的一道题求解Maximal Rectangle O(mn) 解法 非 histogram
相关话题的讨论汇总
话题: height话题: int话题: left话题: right话题: max