Skip to content

Conversation

Siddhram
Copy link

@Siddhram Siddhram commented Oct 13, 2025

This PR introduces a complete and well-documented implementation of the Maximal Square problem in R.

Overview

The implementation provides solutions to find the area of the largest square containing only 1s in a binary matrix. It includes both a 2D DP approach with padding and a 1D space-optimized variant, along with embedded examples and explanatory comments.

Features

  • 2D DP with padding:
    • dp[r+1][c+1] = 1 + min(dp[r][c+1], dp[r+1][c], dp[r][c]) when matrix[r][c] == 1
    • Tracks the maximum side length; returns area = max_side^2
  • Space-optimized 1D DP:
    • Reduces memory to O(n) using a rolling array and prev-diagonal carry
    • Supports both character ('0'/'1') and numeric (0/1) matrices
  • Includes embedded examples and outputs
  • Consistent comments and structure with other DP files

Complexity

  • Time Complexity: O(m·n)
  • Space Complexity:
    • 2D DP: O(m·n)
    • 1D optimized: O(n)

Directory

  • Updated DIRECTORY.md to include “Maximal Square” under Dynamic Programming

Demonstration

Run the script to execute built-in examples:

From Windows command line:

Rscript "R/dynamic_programming/maximal_square.r"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant