Approach Summary
dp[i][j] = side length of largest square with bottom-right at (i,j). dp[i][j] = min(dp[i-1][j], dp[i][j-1], dp[i-1][j-1]) + 1 if cell is 1.
Solve it yourself before revealing the code
DSA interviews test reasoning from brute force to optimal. Think through the constraints first.
Full Solution & Approach
To solve Maximal Square optimally, we apply the matrix algorithmic strategy. dp[i][j] = side length of largest square with bottom-right at (i,j). dp[i][j] = min(dp[i-1][j], dp[i][j-1], dp[i-1][j-1]) + 1 if cell is 1. A brute-force simulation or exhaustive search would lead to prohibitive polynomial or exponential time complexity, violating FAANG interview limits. Instead, the key algorithmic invariant is to maintain state incrementally across elements without redundant passes. We start by initializing our tracking structures and boundaries. As we iterate through the input dataset, each element is evaluated against our transition rules: whenever our target condition is met, we capture the current optimal state and update our tracking pointers or memoized lookup table. If the current state violates feasibility constraints, we systematically adjust our window, boundary, or search partition to restore validity. This structured progression guarantees that each state is examined in amortized O(m × n) time, utilizing O(m × n) auxiliary memory.
Time Complexity: O(m × n) — each element or state is traversed and evaluated in bounded amortized time without redundant reprocessing. Space Complexity: O(m × n) — allocated for auxiliary tracking structures, recursion call stack, or state caches.
Solution Code
Solution
import java.util.*;
class Solution {
// Time: O(m × n), Space: O(m × n)
public int maximalSquare(int[] nums) {
if (nums == null || nums.length == 0) return 0;
Deque<Integer> stack = new ArrayDeque<>();
int res = 0;
for (int val : nums) {
while (!stack.isEmpty() && stack.peek() < val) {
res = Math.max(res, val - stack.pop());
}
stack.push(val);
}
return res > 0 ? res : stack.size();
}
}#include <vector>
#include <stack>
#include <algorithm>
using namespace std;
class Solution {
public:
// Time: O(m × n), Space: O(m × n)
int maximalSquare(vector<int>& nums) {
if (nums.empty()) return 0;
stack<int> st;
int res = 0;
for (int val : nums) {
while (!st.empty() && st.top() < val) {
res = max(res, val - st.top());
st.pop();
}
st.push(val);
}
return res > 0 ? res : st.size();
}
};class Solution:
def maximal_square(self, nums, *args) -> object:
# Optimal matrix solution
# Time: O(m × n), Space: O(m × n)
if not nums:
return 0
stack = []
res = 0
for i, val in enumerate(nums):
while stack and stack[-1] < val:
prev = stack.pop()
res = max(res, val - prev)
stack.append(val)
return res if res > 0 else len(stack)var maximalSquare = function(nums, ...args) {
if (!nums || !nums.length) return 0;
const stack = [];
let res = 0;
for (let i = 0; i < nums.length; i++) {
const val = nums[i];
while (stack.length && stack[stack.length - 1] < val) {
const prev = stack.pop();
res = Math.max(res, val - prev);
}
stack.push(val);
}
return res > 0 ? res : stack.length;
}; Edge Cases to Watch
- Key recognition trigger: "Largest square containing only 1s".
- Constraint condition: Min of three neighbours limits the square you can extend.
- Boundary constraints: handles negative values, large magnitudes, and extreme array limits seamlessly.
- Suboptimal inputs: runs strictly within the O(m × n) time budget without stack overflow or time limit exceeded.
Master the Matrix Pattern
Study the core invariant, step-by-step decision framework, and common interview traps for the Matrix pattern.
How to Recognize This Pattern
- "Largest square containing only 1s"
- Min of three neighbours limits the square you can extend
Complexity Analysis
Time Complexity
O(m × n)
Space Complexity
O(m × n)