student student Interview Question
Students StudentsCountry: India
i have also tried it by creating histogram. but after creating the histogram i am not able to calculate the max area. by using stack it seems some difficult to me. so i m giving my code. can you please correct it. there is something wrong which i m not able to found.
//calculating maximum area from histogram
int area=0;
int maxarea=0;
int col=0;
for(i=0; i<m; i++)
{
for( j=0; j<n-1; j++)
{ //if(B[i][j]==0)
// area=area;
if(B[i][j]>=B[i][j+1])
{
col=col+1;
area=col*B[i][j+1];
}
if(B[i][j]==0)
{}
else
area=col*B[i][j+1];
if(maxarea<area)
maxarea=area;
@sneha: I think your logic is not correct. This will work only of the matrix has one row only.
If matrix has more than 1 ro, it will fail.
Check my code.
@nitingupta180: Nice & optimal solution.
The algorithm for largestArea() could be made a little more faster, if you see that you only need the first loop (for computing L). That loop can be modified like so: whenever you pop an element from the stack you update the result with the rectangle corresponding to that element (you know where it starts and you know that it ends here at i). You also need to take care of elements not popped at the end of the loop.
However, I think that the solution that computes both L and R is easier to understand.
Ohh good, looks like CC has added code editor out of the box. This is very good, now code actually looks like a code.
Thanks Mrs Gayle.
Can be done in O(n2)
Here is the alog:
a[n][n]
O/p: updating 4 points of a rectangle (A, B, C, D)
1. Start from a[0][0] and find out first 1 in the array
A = step1 output
2. Loop starts from step 1 element (i, j)
{ a. another loop to get 1 in the same colomn (starting from end towards j) }, if no 1 exists in the current col, then increment A colomn number and start from begenning.
{ b. B = stepa output
{ c. From B start another loop to get corresponding right edge by getting extreme in the same row, if no 1 found decrement B row number and start from step a
{ d. C = output of stepc }
{ e. start a loop at C , to find top point in the same col }, if not found decrement C and start from step c }
{ d. if D and A are on the same col, break all loops (A, B, C, D is the output)
{ If ( D and A are not in the same col, then get tow pointer from A and D and find out common element where both have 1, we can search until we reach the B, if not found rectangle doesnt exists with the given data, other wise return A, B , C and D }
Consider this matrix
0 1 1 0 0
0 0 0 1 1
0 1 0 1 1
0 1 1 1 0
0 1 1 1 0
1 0 1 1 0
At every row i will do a[i] && a[i+i]
Which will give me if i am able to form any new rectangles
Simultaneously i will maintain a max heap of rectangles
Rectangle = corner1, corner2, area.
Optional step
I will remove a rectangle if it is evident there can be no more 1's added to it and it is not the Max area rectangle.
I will then do a a[i] && a[i-1] and see if the any previous rectangles can be increased in size.
If so i will do so.
In the end i will output the max from heap.
here's how it will go
Just showing this for explanation i am not changing the array or taking extra array.
0 1 1 0 0
0 0 0 2 2
0 0 0 2 2
0 6 6 6 0
0 6 6/8 6/8 0
0 0 8 8 0
List of Rectangles
1 corner1,corner2 size=2
3 corner1,corner2 size=4
6 corner1,corner2 size=6
8 corner1,corner2 size=4
Max Size = 6
Complexity -- I am only scanning the array once. But each time i may have to look at all the previous rectangles which may be.
O( rows * (number of rectangles) )
use a new matrix sum[row][column][2] which represents the sum of left 1s and top 1s
say
1,1,0,1,0
0,1,1,1,0
1,1,1,0,1
1,0,1,1,0
you'll have a new matrix sum[0][0]={1,1} sum[0][1]={2,1}, etc
so you can check from bottom right to top left of this matrix to get the each rectangle's area, and each element will be visited once, which has time complexity O(row*column).
Why do we need to change 0s to negative infinite value. We can find the maximum sum for the given array itself..
This is the problem of largest area histogram. Compute sum of rows by iterative deepening (row-wise), and for every 1-D array generated get largest area histogram for it.
Finally return the maximum.
----------------------------------------------------------
Program to find largest area of histogram.
----------------------------------------------------
Program to get 1-D array by summing each row every time.
---------------------------------------------------------
- Nitin Gupta February 07, 2013