Maximal Rectangle โ€” Algorithm Visualization & Coding Challenge

Choose Your Learning Path

How would you like to learn today?
Visualize algorithms in real time, explore them step by step, or challenge yourself with a test.Choose a path to focusโ€”or scroll down to preview all options.

๐Ÿง  Active Learning

Visualize the algorithm step-by-step with interactive animations in real time.

๐Ÿ“– Passive Learning

Read the full explanation, examples, and starter code at your own pace.

๐ŸŽฏ Challenge Mode

Drag and arrange the algorithm steps in the correct execution order.

๐Ÿง  Select Active to activate

JUMP INTO VISUALIZATION
Watch algorithms run step by step.

Follow every state change, comparison, and transformation as the execution unfolds in real time.

๐Ÿ“– Select Passive to activate

Understanding Maximal Rectangle
Detailed explanation and reference materials
Problem Overview

๐Ÿงฑ Maximal Rectangle

Difficulty: ๐Ÿ”ด Hard
Topics: Dynamic Programming, Stack, Matrix
Goal: Find the largest rectangle made of only 1s in a binary matrix and return its area.


๐Ÿ“Œ Problem Statement (One Line)

Given a binary matrix, return the area of the largest rectangle containing only 1's.


๐Ÿงช Examples

โœ… Example 1

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

Output: 6

๐Ÿ“ฆ The largest rectangle of 1s has area = 6.


โŒ Example 2

Input: [["0"]]
Output: 0

โœ… Example 3

Input: [["1"]]
Output: 1

import java.util.Stack;

public class Main {

public static void main(String[] args) {

    int[][] matrix = {
        {1, 0, 1, 0, 0},
        {1, 0, 1, 1, 1},
        {1, 1, 1, 1, 1},
        {1, 0, 0, 1, 0}
    };

    System.out.println(maximalRectangle(matrix)); // Output: 6
}

public static int maximalRectangle(int[][] matrix) {
    if (matrix.length == 0) return 0;

    int cols = matrix[0].length;
    int[] heights = new int[cols];
    int maxArea = 0;

    for (int[] row : matrix) {
        // Build histogram heights
        for (int j = 0; j < cols; j++) {
            heights[j] = row[j] == 1 ? heights[j] + 1 : 0;
        }

        // Calculate max rectangle area for current histogram
        maxArea = Math.max(maxArea, area(heights));
    }

    return maxArea;
}

// Function to calculate largest rectangle area in histogram
public static int area(int[] heights) {
    Stack<Integer> stack = new Stack<>();
    int maxArea = 0;

    for (int i = 0; i <= heights.length; i++) {
        int currentHeight = (i == heights.length) ? 0 : heights[i];

        while (!stack.isEmpty() && currentHeight < heights[stack.peek()]) {
            int height = heights[stack.pop()];
            int width = stack.isEmpty() ? i : i - stack.peek() - 1;
            maxArea = Math.max(maxArea, height * width);
        }
        stack.push(i);
    }

    return maxArea;
}

}

โ€” Written by Saurabh Patil โ€ข B.Tech CSE โ€ข Software Developer

Categories
2d-arrays
stacks
dp
leetcode-problem-of-the-day
java
Reference Link
https://leetcode.com/problems/maximal-rectangle/

Loading component...

Starter Code
Test, modify, or copy the starter code. Click "Visualize" to import into the canvas.
Java
Output:
Understood Algorithm, Test Me now ๐ŸŽฎ

๐ŸŽฏ Select Challenge to activate

๐Ÿง  Logic Puzzle
Think & Arrange, Don't Just Copy-Paste

Drag and arrange the algorithm steps in the correct execution order instead of spending time typing code letter by letter.

Arrange the Algorithm Correctly ๐Ÿงฉ

The algorithm is divided into three logical parts. Carefully rearrange each section in the correct order to form a complete and valid solution.

Understand Below Algorithm

Don't Know Current Algorithm ? ย 

Green text means the instruction is placed in the correct position.

Red text means the instruction is in the wrong position.

Block Colors

Instructions with the same background color indicate particular blocks start and end.

A tick mark means the instruction is correct and locked.

๐Ÿ”’ Locked steps cannot be moved. Only unlocked steps are draggable.

๐Ÿ”Š Enable sound for swap feedback and completion effects.

DrawToCode โ€” Visualize, Practice & Master Algorithms

Learn data structures and algorithms through interactive visualizations. Practice coding problems, track your progress, and understand concepts deeply.

Visualization Boardย |ย Problemsย |ย Aboutย |ย Privacy Policy
EmailLinkedInTwitterInstagramGitHub
ยฉ 2026 DrawToCode. All rights reserved.