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
- 4. Median of Two Sorted Arrays(Hard)
- 11. Container With Most Water(Medium)
- 15. 3Sum(Medium)
- 16. 3Sum Closest(Medium)
- 26. Remove Duplicates from Sorted Array(Easy)
- 27. Remove Element(Easy)