d*******u 发帖数: 186 | 1 Hello,
The following code can pass the Leetcode test, if it ran separately.
But it can not pass the Leetcode online. Any problem? thanks.
class Solution {
public:
int largestAreaunderHistogramv(vector histogram, int size)
{
stack histindexlessthan;
int *area = new int[size];
int left = -1;
//get Li
for(int i=0; i
{
while(!histindexlessthan.empty())
{
if(histogram[i] <= histogram[histindexlessthan.top()])
histindexlessthan.pop();
else
break;
}
if(histindexlessthan.empty())
left = -1;
else
left = histindexlessthan.top();
histindexlessthan.push(i);
area[i] = i-left-1;
}
int right = size;
while(!histindexlessthan.empty())
histindexlessthan.pop();
//get Ri
for(int i=size-1; i>=0; i--)
{
while(!histindexlessthan.empty())
{
if(histogram[i] <= histogram[histindexlessthan.top()])
histindexlessthan.pop();
else
break;
}
if(histindexlessthan.empty())
right = size;
else
right = histindexlessthan.top();
histindexlessthan.push(i);
area[i] = area[i]+right-i-1;
}
int max=0;
for(int i=0; i
{
area[i] = (area[i]+1) *histogram[i];//1: itself
if(area[i]>max)
max=area[i];
};
delete area;
return max;
}
int maximalRectangle(vector > &matrix) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
for(int i=1; i
itself.
{
for(int j=0; j
{
if( matrix[i][j] == 1)
matrix[i][j] =matrix[i-1][j] +1;
};
};
int *area = new int[matrix.size()];
int max=0;
vector arr;
for(int i=0; i
{
arr.clear();
for(int j=0; j
arr.push_back(matrix[i][j]);
area[i]=largestAreaunderHistogramv(arr, matrix[i].size());
if(arr[i]>max)
max=area[i];
}
delete area;
return max;
}
}; |
|