Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

find the largest area in this 2d array

i need help in that

You are again the owner of a coworking space like WeWork and your office building is rectangular. You team just created many wall partitions to create mini offices for startups. Your office campus is represented by a 2D array of 1s (floor spaces) and 0s (walls). Each point on this array is a one foot by one foot square. Before renting to tenants, you want to reserve an office for yourself. You wish to fit the largest possible rectangular table in your office, and you will select the office that fits this table. The table sides will always be parallel to the boundaries of the office building. What is the area of the biggest table that can fit in your office?

Functions biggestTable() has one parameter:

grid: a 2D grid/array of 1s and 0s

Input Format For some of our templates, we have handled parsing for you. If we do not provide you a parsing function, you will need to parse the input directly. In this problem, our input format is as follows:

The first line is the number of rows in the 2D array The second line is the number of columns in the 2D array The rest of the input contains the data to be processed Here is an example of the raw input:

4
5
11110
11010
11000
00000

Expected Output Return the area of the biggest area made of 1s in the grid. Assume the grid is surrounded by 0s (walls).

Constraints Assume that the bounds of the array are the following: The total amount of elements in the array: width x height <= 10^6

Example Example biggestTable() Input

grid: 
    [[1, 0, 1, 1, 1],
     [1, 0, 1, 1, 1],
     [1, 1, 1, 1, 1],
     [1, 0, 0, 1, 0]]

Example Output

9

Solution

The top right of the grid consists of a rectangle with nine 1s in it, the biggest possible space for our table.

like image 907
mohamed nageh Avatar asked Nov 09 '19 00:11

mohamed nageh


1 Answers

The problem can be approached in a logical way where you loop through the building and check for potential space where tables can be placed, then just return the biggest table found:

function biggestTable(grid) {
    const tableExist = (x, y, w, h) => {
        let exist = 1;
        for(let i = 0; i < w ; i++) {
            for(let j = 0; j < h ; j++) {
                exist &= grid[j + y] !== undefined && grid[j + y][i + x] == 1;
            }
        }
        return exist;
    };

    const biggestTableAt = (x, y) => {
        let max = 0;
        for(let w = 1; w <= grid[0].length; w++) {
            for(let h = 1; h <= grid.length; h++) {
                const table_size = w * h;
                if (tableExist(x, y, w, h) && table_size>max) {
                    max = table_size;
                }
            }
        }
        return max;
    };

    let max = 0;
    for(let x = 0; x < grid[0].length; x++) {
        for(let y= 0; y < grid.length; y++) {
            const table_size = biggestTableAt(x, y);
            if (table_size > max) {
                max = table_size;
            }
        }
    }
    return max;
}
like image 83
Ghebrehiywet Avatar answered Oct 23 '22 09:10

Ghebrehiywet