← Back to index
MediumTypeScript3 min read

Largest Plus Sign

find the largest plus sign order by computing longest consecutive 1s in four directions using dp.

dynamic-programmingmatrix

Problem link

View on LeetCode

Solutions in this repo

  • TypeScript../largest-plus-sign/solution.tsTypeScript solution

for each cell, compute the longest streak of 1s in four directions: left, right, up, down. use separate passes for each direction to build these counts efficiently.

the order of a plus sign centered at a cell is the minimum of all four directional streaks. find the maximum order across all cells.

approach

  • build four 2d arrays for left, right, up, down streaks
  • for each direction, do a single pass to compute streaks
  • for each cell, take min of four directions as plus order
  • track the maximum order found

complexity

O(n²) time for four passes over the grid, O(n²) space for the four direction arrays. efficient dp solution.