DDSA Solutions

2639. Find the Width of Columns of a Grid

Time: O(mn * digits)
Space: O(n)
Advertisement

Approach

For each column track max string width of each integer (handle negative sign).

Key Techniques

Array

Array problems involve manipulating elements stored in a contiguous block of memory. Key techniques include two-pointer traversal, prefix sums, sliding windows, and in-place partitioning. In C#, arrays are zero-indexed and fixed in size — use List<T> when you need dynamic resizing.

Matrix

Matrix problems often involve BFS/DFS flood fill, dynamic programming on 2D grids, or spiral/diagonal traversal. For row × column DP, break it into 1D sub-problems column by column. Common pitfalls: boundary checks and modifying the input matrix in-place.

Simulation

Simulation problems require implementing the described process step by step. Focus on correctly handling edge cases and state transitions. Common in geometry, game problems, and string manipulation. Optimize only if the naive simulation exceeds the time limit.

2639.cs
C#
// Approach: For each column track max string width of each integer (handle negative sign).
// Time: O(mn * digits) Space: O(n)

public class Solution
{
    public int[] FindColumnWidth(int[][] grid)
    {
        int m = grid.Length;
        int n = grid[0].Length;

        int[] ans = new int[n];
        for (int j = 0; j < n; j++)
        {
            for (int i = 0; i < m; i++)
            {
                ans[j] = Math.Max(ans[j], grid[i][j].ToString().Length);
            }
        }

        return ans;
    }
}
Advertisement
Was this solution helpful?