Approach #1: Brute Force [Time Limit Exceeded]
Intuition and Algorithm
For each possible center, find the largest plus sign that could be placed by repeatedly expanding it. We expect this algorithm to be , and so take roughly operations. This is a little bit too big for us to expect it to run in time.
Complexity Analysis

Time Complexity: , as we perform two outer loops (), plus the inner loop involving
k
is . 
Space Complexity: .
Approach #2: Dynamic Programming [Accepted]
Intuition
How can we improve our bruteforce? One way is to try to speed up the inner loop involving k
, the order of the candidate plus sign.
If we knew the longest possible arm length in each direction from a center, we could know the order of a plus sign at that center. We could find these lengths separately using dynamic programming.
Algorithm
For each (cardinal) direction, and for each coordinate (r, c)
let's compute the count
of that coordinate: the longest line of '1'
s starting from (r, c)
and going in that direction.
With dynamic programming, it is either 0 if grid[r][c]
is zero, else it is 1
plus the count of the coordinate in the same direction.
For example, if the direction is left and we have a row like 01110110
, the corresponding count values are 01230120
, and the integers are either 1 more than their successor, or 0.
For each square, we want dp[r][c]
to end up being the minimum of the 4 possible counts. At the end, we take the maximum value in dp
.
Complexity Analysis

Time Complexity: , as the work we do under two nested for loops is .

Space Complexity: , the size of
dp
.
Analysis written by: @awice.