DDSA Solutions

2639. Find the Width of Columns of a Grid

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

Problem Overview

Find the Width of Columns of a Grid (Unknown) asks you to solve a structured algorithmic task. This is a common Array / Matrix pattern in coding interviews. For each column track max string width of each integer (handle negative sign).

A full step-by-step explanation is being added. See the study guide for pattern-based practice.

Approach

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

Related patterns: Array, Matrix, Simulation

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;
    }
}
Was this solution helpful?

Related Problems