2639. Find the Width of Columns of a Grid
UnknownView on LeetCode
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
- 498. Diagonal Traverse(Medium)
- 885. Spiral Matrix III(Medium)
- 1260. Shift 2D Grid(Easy)
- 1861. Rotating the Box(Easy)
- 2017. Grid Game(Unknown)
- 2022. Convert 1D Array Into 2D Array(Medium)