Difficulty: Easy | Acceptance: 70.40% | Paid: No Topics: Array, Matrix
You are given a 0-indexed m x n integer matrix grid. The width of a column is the maximum length of its integers.
For example, if grid = [[-10], [3], [12]], the width of the only column is 3 since -10 is of length 3.
Return an array ans of size n where ans[i] is the width of the ith column.
The width of an integer x is the number of digits if x >= 0, or the number of digits plus 1 if x < 0.
- Examples
- Constraints
- Column-wise Traversal
- Transpose and Process
- Manual Digit Counting
Examples
Input: grid = [[1],[22],[333]]
Output: [3]
Explanation: In the 0th column, 333 is of length 3.
Input: grid = [[-15,1,3],[15,7,12],[5,6,-2]]
Output: [3,1,2]
Explanation:
- In the 0th column, only -15 is of length 3.
- In the 1st column, all integers are of length 1.
- In the 2nd column, 12 is of length 2.
Constraints
m == grid.length
n == grid[i].length
1 <= m, n <= 100
-10^9 <= grid[i][j] <= 10^9
Column-wise Traversal
Intuition Iterate through each column and find the maximum string length by converting each number to a string.
Steps
- Initialize result array with n zeros
- For each column j, iterate through all rows i
- Convert grid[i][j] to string and track maximum length
- Store the maximum width for column j in result
class Solution:
def findColumnWidth(self, grid: List[List[int]]) -> List[int]:
m, n = len(grid), len(grid[0])
result = [0] * n
for j in range(n):
max_width = 0
for i in range(m):
width = len(str(grid[i][j]))
max_width = max(max_width, width)
result[j] = max_width
return resultComplexity
- Time: O(m × n)
- Space: O(n) for the result array
- Notes: Simple and readable, uses built-in string conversion
Transpose and Process
Intuition Transpose the grid so columns become rows, then find max string length for each row.
Steps
- Extract each column as a separate array
- For each column array, find the maximum string length
- Build result array from these maximums
class Solution:
def findColumnWidth(self, grid: List[List[int]]) -> List[int]:
m, n = len(grid), len(grid[0])
result = []
for j in range(n):
col = [grid[i][j] for i in range(m)]
result.append(max(len(str(x)) for x in col))
return resultComplexity
- Time: O(m × n)
- Space: O(m) for temporary column extraction
- Notes: More functional style, may use extra memory for column arrays
Manual Digit Counting
Intuition Count digits manually without string conversion by repeatedly dividing by 10, handling negative sign separately.
Steps
- For each number, handle zero as special case (length 1)
- If negative, count the sign and work with absolute value
- Repeatedly divide by 10 to count digits
- Track maximum width for each column
class Solution:
def findColumnWidth(self, grid: List[List[int]]) -> List[int]:
def count_digits(num):
if num == 0:
return 1
count = 0
if num < 0:
count = 1
num = -num
while num > 0:
count += 1
num //= 10
return count
m, n = len(grid), len(grid[0])
result = [0] * n
for j in range(n):
max_width = 0
for i in range(m):
width = count_digits(grid[i][j])
max_width = max(max_width, width)
result[j] = max_width
return resultComplexity
- Time: O(m × n × d) where d is average digit count (max 10 for given constraints)
- Space: O(n) for the result array
- Notes: Avoids string allocation overhead, more efficient for large inputs