2639. Find the Width of Columns of a Grid
Approach
For each column track max string width of each integer (handle negative sign).
Key Techniques
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 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 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.
// 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;
}
}