85. Maximal Rectangle Leetcode Solution

Maximal Rectangle Leetcode Solution

Difficulty: Hard

Topics: Array, Dynamic Programming, Stack, Matrix, Monotonic Stack

Given a rows x cols binary matrix filled with 0‘s and 1‘s, find the largest rectangle containing only 1‘s and return its area.

Example 1:

maximal

  • Input: <code>matrix = [[“1″,”0″,”1″,”0″,”0”],[“1″,”0″,”1″,”1″,”1”],[“1″,”1″,”1″,”1″,”1”],[“1″,”0″,”0″,”1″,”0”]]
  • Output: <code>6
  • Explanation: <code>The maximal rectangle is shown in the above picture.

Example 2:

  • Input: <code>matrix = [[“0”]]
  • Output: <code>0

Example 3:

  • Input: <code>matrix = [[“1”]]
  • Output: <code>1

Constraints:

  • rows == matrix.length
  • cols == matrix[i].length
  • 1 <= row, cols <= 200
  • <code>matrix[i][j] is '0' or '1'.

Solution:

We can follow a dynamic programming approach. This problem can be effectively tackled by treating each row of the matrix as the base of a histogram, where the height of each column in the histogram represents the number of consecutive ‘1’s up to that row.

  1. Convert each row of the matrix into a histogram:
    • Treat each row as the base of a histogram where the height of each bar is the number of consecutive 1s up to that row.
  2. Calculate the maximum area of the rectangle in the histogram for each row:
    • For each row, use a stack to maintain the indices of the histogram bars and calculate the largest rectangle area

Let’s implement this solution in PHP: 85. Maximal Rectangle

<?php
// Test cases
$matrix1 = array(
    array("1","0","1","0","0"),
    array("1","0","1","1","1"),
    array("1","1","1","1","1"),
    array("1","0","0","1","0")
);

$matrix2 = array(
    array("0")
);

$matrix3 = array(
    array("1")
);

echo maximalRectangle($matrix1) . "n"; // Output: 6
echo maximalRectangle($matrix2) . "n"; // Output: 0
echo maximalRectangle($matrix3) . "n"; // Output: 1

?>

Explanation:

  1. Matrix Initialization:
    • If the matrix is empty or the first row is empty, return 0.
  2. Height Array:
    • Initialize a height array that keeps track of the height of ‘1’s for each column.
  3. Updating Heights:
    • Traverse each row of the matrix and update the height of each column based on whether the matrix value is ‘1’.
  4. Histogram Calculation:
    • For each updated height array, calculate the maximal rectangle area using a stack-based approach. This approach computes the maximum area for a histogram efficiently.
  5. Stack-Based Histogram Calculation:
    • Use a stack to keep track of indices of increasing heights.
    • Calculate area whenever a decrease in height is detected and update the maximum area.

The calculateMaxHistogramArea function computes the largest rectangle area in a histogram, which is used to update the maximum area for each row’s histogram. This approach ensures that the overall complexity is manageable given the problem constraints.

Contact Links

If you found this series helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me!

If you want more helpful content like this, feel free to follow me:

If you need any support regarding your website

If you like our blog, Please support us to improve

Buy Me a Coffee

Leave a Reply

Your email address will not be published. Required fields are marked *

RECENT POST
Leetcode Solutions

633. Sum of Square Numbers

Sum of Square Numbers Difficulty: Medium Topics: Math, Two Pointers, Binary Search Given a non-negative integer c, decide whether there’re

Leetcode Solutions

624. Maximum Distance in Arrays

Maximum Distance in Arrays Difficulty: Medium Topics: Array, Greedy You are given m arrays, where each array is sorted in