Redian新闻
>
有用java过Maximal Rectangle的judge large的吗?
avatar
有用java过Maximal Rectangle的judge large的吗?# JobHunting - 待字闺中
c*t
1
俺用了类似Largest Rectangle in Histogram,用stack达到O(M*N)的解法,依然过不
了judge large. time limit exceeded.
有人用java过了judge large的吗?
多谢!
avatar
f*t
4
不知为啥过不了,在我电脑上200*200的test case秒出答案啊
算出来147对不对?
avatar
n*r
5
可以过
public class Solution {

public static class Bar{
int height, startIdx;
Bar(int h, int i){ height = h; startIdx = i; }
}
public int maximalRectangle(char[][] matrix) {
if (matrix.length == 0 || matrix[0].length == 0) return 0;
int n = matrix.length, m = matrix[0].length;
int[][] aux = new int[n][m];

for (int i=0; iaux[0][i] = matrix[0][i] - '0';
}
for (int i=1; ifor (int j=0; jaux[i][j] = matrix[i][j] - '0';
if (aux[i][j] == 1)
aux[i][j] += aux[i-1][j];
}
}
int maxArea = 0;
for (int i=0; imaxArea = Math.max(maxArea, largestBarArea(aux[i]));
}
return maxArea;
}

private int largestBarArea(int[] h){
Stack s = new Stack();
s.push(new Bar(-1, 0));
int maxArea = 0;
for (int i=0; i<=h.length; i++){
int height = 0;
if (i < h.length){
height = h[i];
}
Bar cur = new Bar(height, i+1);
while (s.peek().height > height){
Bar prev = s.pop();
int area = prev.height * (i+1 - prev.startIdx);
if (area > maxArea) maxArea = area;
cur.startIdx = prev.startIdx;
}
s.push(cur);
}
return maxArea;
}
}
avatar
s*s
6
在我的机器上试了你的code,还是过不了large judge.难道这个是YMMV的?求
ihasleetcode解释。

【在 n****r 的大作中提到】
: 可以过
: public class Solution {
:
: public static class Bar{
: int height, startIdx;
: Bar(int h, int i){ height = h; startIdx = i; }
: }
: public int maximalRectangle(char[][] matrix) {
: if (matrix.length == 0 || matrix[0].length == 0) return 0;
: int n = matrix.length, m = matrix[0].length;

avatar
d*g
7
刚写的,796ms过了。我这个计算largest bar area的函数写得复杂了,吃个饭回来看
看楼上那个简洁代码的思路。
public int maximalRectangle(char[][] matrix)
{
if(matrix == null || matrix.length==0) return 0;
int[] count1 = new int[matrix[0].length];

for(int j = 0; j < matrix[0].length; ++j)
count1[j] = matrix[0][j] - '0';

int result = 0;
for(int i = 1; i < matrix.length; ++i)
{
result = Math.max(result, processRow(count1));
int[] count2 = new int[matrix[0].length];
for(int j = 0; j < matrix[0].length; ++j)
count2[j] = matrix[i][j] == '0' ? 0 : count1[j]+1;
count1 = count2;
}
result = Math.max(result, processRow(count1));

return result;
}
private int processRow(int[] array)
{
Stack stack = new Stack();
int[] lengthToRight = new int[array.length];
stack.push(0);
for(int i = 1; i < array.length; ++i)
{
while(!stack.isEmpty() && array[stack.peek()] > array[i])
{
lengthToRight[stack.peek()] = i-stack.peek();
stack.pop();
}
stack.push(i);
}
while(!stack.isEmpty())
{
lengthToRight[stack.peek()] = array.length-stack.peek();
stack.pop();
}
int[] lengthToLeft = new int[array.length];
stack.push(array.length-1);
for(int i = array.length-2; i >= 0; --i)
{
while(!stack.isEmpty() && array[stack.peek()] > array[i])
{
lengthToLeft[stack.peek()] = stack.peek()-i-1;
stack.pop();
}
stack.push(i);
}
while(!stack.isEmpty())
{
lengthToLeft[stack.peek()] = stack.peek();
stack.pop();
}

int result = 0;
for(int i = 0; i < array.length; ++i)
{
result = Math.max(result, array[i]*(lengthToLeft[i]+lengthToRight[i]
));
}
return result;
}
avatar
d*g
8
呃。。我也过不了你这个代码。。。好奇怪

【在 n****r 的大作中提到】
: 可以过
: public class Solution {
:
: public static class Bar{
: int height, startIdx;
: Bar(int h, int i){ height = h; startIdx = i; }
: }
: public int maximalRectangle(char[][] matrix) {
: if (matrix.length == 0 || matrix[0].length == 0) return 0;
: int n = matrix.length, m = matrix[0].length;

avatar
n*r
9
上个版本的aux数组多余!写个用数组实现栈的版本,更快一些。
public class Solution {
public static class Bar {
int height, startIdx;
Bar(){}
Bar(int h, int s){ height = h; startIdx = s; }
}
public static int maxn = 1000;
public static Bar[] stack = new Bar[maxn];

public static int largestRectangle(int[] h){
int top = 0, max = 0;
stack[top++] = new Bar(-1, 0);
for (int i=0; i<=h.length; i++){
Bar cur = new Bar(0, i);
if (i < h.length) cur.height = h[i];
while (stack[top-1].height > cur.height){
Bar prev = stack[--top];
int area = (i - prev.startIdx)*prev.height;
if (area > max)
max = area;
cur.startIdx = prev.startIdx;
}
stack[top++] = cur;
}
return max;
}
public int maximalRectangle(char[][] matrix){
if (matrix == null || matrix.length == 0 || matrix[0].length == 0)
return 0;
int n = matrix.length, m = matrix[0].length;
int max = 0;
int[] h = new int[m];
for (int i=0; ifor (int j=0; jif (matrix[i][j] == '1'){
h[j] += 1;
}else
h[j] = 0;
}
int area = largestRectangle(h);
if (area > max)
max = area;
}
return max;
}
}
avatar
n*r
10
我这边两个都能过,第一个700多milli secs,第二个596 milli secs
avatar
p*2
11
多试几次

【在 d*********g 的大作中提到】
: 呃。。我也过不了你这个代码。。。好奇怪
avatar
d*g
12

确实多试几次就过了。。。nibber第二个版本代码很简洁也很快,学习了。

【在 p*****2 的大作中提到】
: 多试几次
avatar
p*2
13

不过话说回来,这题有人面试碰到过吗?貌似已经废除了吧?

【在 d*********g 的大作中提到】
:
: 确实多试几次就过了。。。nibber第二个版本代码很简洁也很快,学习了。

avatar
c*a
14
这个时灵时不灵,有时候能过,有时候不能过.....
Run Status: Accepted!
Program Runtime: 840 milli secs
public class Solution {
public int maximalRectangle(char[][] matrix) {
// Start typing your Java solution below
// DO NOT write main() function
if(matrix.length <=0 )
return 0;
int col=matrix[0].length;
int[] ar = new int[col];
int max=0;
for(int i=0; ifor(int j=0; jif(matrix[i][j]=='1')
ar[j]++;
else ar[j] =0 ;
}
max = Math.max(max,largestRectangleArea(ar));
}
return max;
}
public int largestRectangleArea(int[] height) {
int max=0;
Stack stack = new Stack();
int i =0;
int count=0;
while(iif(stack.isEmpty() || stack.peek() <= height[i]){
stack.push(height[i]);
}
else{
count = 0;
while(!stack.isEmpty() && stack.peek()> height[i]){
count++;
int val = stack.pop();
max = Math.max(max, count * val);
}
for(; count>=0; count--)
stack.push(height[i]);
}
i++;
}
count = 0;
while(!stack.isEmpty()){
count++;
int val = stack.pop();
max = Math.max(max, count * val);
}
return max;
}
}
avatar
c*t
15
我算出来怎么是48?

【在 f*******t 的大作中提到】
: 不知为啥过不了,在我电脑上200*200的test case秒出答案啊
: 算出来147对不对?

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。