Paths Subjects Questions Quizzes Pricing Search
Advanced Open Pro

Largest Square in a Matrix

Given an m x n binary matrix matrix filled with 0s and 1s, find the largest square containing only 1s, and return its area.

Example 1

Input:
matrix = [
  [1, 0, 1, 0, 0],
  [1, 0, 1, 1, 1],
  [1, 1, 1, 1, 1],
  [1, 0, 0, 1, 0],
]
Output: 4
Explanation: The largest all-1s square has side length 2, area 4.

Example 2

Input: matrix = [[0, 1], [1, 0]]
Output: 1

Constraints

  • 1 <= m, n <= 300
  • matrix[i][j] is 0 or 1.

Share this question

← Back to Dynamic Programming practice

We use cookies for product analytics to improve OmniAtlas. See our Privacy Policy.