Similar Problems

Similar Problems not available

Difference Between Ones And Zeros In Row And Column - Leetcode Solution

Companies:

  • amazon

LeetCode:  Difference Between Ones And Zeros In Row And Column Leetcode Solution

Difficulty: Medium

Topics: matrix array simulation  

Problem Statement:

Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1's and return its area.

Example:

Input: [ ["1","0","1","0","0"], ["1","0","1","1","1"], ["1","1","1","1","1"], ["1","0","0","1","0"] ] Output: 6

Explanation: The maximal rectangle is shown in the shaded area below.

Solution:

This problem can be solved using dynamic programming approach. We start by defining two 2D matrices of same size as input matrix. Let's call them row and column matrices. Each element of row[i][j] represents the number of consecutive 1's in a row starting from index j in row i. Similarly, each element of column[i][j] represents the number of consecutive 1's in a column starting from index i in column j.

To fill these matrices, we iterate through each row and column respectively and for each consecutive 1 found, we add 1 to the current count and store it in their respective matrices. Once we have calculated these matrices, we can iterate through every cell of the input matrix and find the largest rectangle containing only 1's.

We can do this by picking a cell (i, j) and then moving downwards as long as each row contains at least the same number of 1's as the row above it (calculated using row matrix). For each downward movement, we can calculate the area of the rectangle using the height of the rectangle (number of rows) and the width (minimum number of 1's in each row). We can keep track of the maximum area seen so far and return it at the end.

The time complexity of this solution is O(mn) where m is the number of rows and n is the number of columns and space complexity is also O(mn) for storing row and column matrices.

Here is the Python code for the solution:

class Solution: def maximalRectangle(self, matrix: List[List[str]]) -> int: if not matrix: return 0 m, n = len(matrix), len(matrix[0]) row = [[0 for _ in range(n)] for _ in range(m)] col = [[0 for _ in range(n)] for _ in range(m)]

    # Calculate row and column matrices
    for i in range(m):
        for j in range(n):
            if matrix[i][j] == '1':
                if j == 0:
                    row[i][j] = 1
                else:
                    row[i][j] = row[i][j - 1] + 1

                if i == 0:
                    col[i][j] = 1
                else:
                    col[i][j] = col[i - 1][j] + 1

    # Calculate maximum rectangle area
    max_area = 0
    for i in range(m):
        for j in range(n):
            if matrix[i][j] == '1':
                width = row[i][j]
                height = 1
                for k in range(i + 1, m):
                    if col[k][j] < height + 1:
                        break
                    height += 1
                    width = min(width, row[k][j])
                max_area = max(max_area, width * height)

    return max_area

This code should pass all the test cases on LeetCode for the "Difference Between Ones And Zeros In Row And Column" problem.

Difference Between Ones And Zeros In Row And Column Solution Code

1