Find the Width of Columns of a Grid — LeetCode 2639 Python Solution

EasyArrayMatrix
Problem
#2639
Reading time
2 min

The problem

You are given a 0-indexed m x n integer matrix grid. The width of a column is the maximum length of its integers.

Example

Input
grid = [[1],[22],[333]]
Output
[3]
Explanation
In the 0th column, 333 is of length 3.

Python solution

Python
class Solution:
    def findColumnWidth(self, grid: List[List[int]]) -> List[int]:
        return [max(len(str(x)) for x in col) for col in zip(*grid)]

Complexity

MeasureComplexity
TimeO(m \times n)
SpaceO(\log M) auxiliary

Pattern: Matrix and Grid

Treat a 2-D grid as a graph whose neighbours are the four adjacent cells. LeetCode 2639. Find the Width of Columns of a Grid is filed here on both counts: the reference solution below belongs to the algorithm family this hub collects, and LeetCode tags it Matrix.

The matrix and grid guide has the Python template for the pattern and the 216 LeetCode problems that use it.

Related problems

Frequently asked questions

How hard is LeetCode 2639. Find the Width of Columns of a Grid?
LeetCode 2639. Find the Width of Columns of a Grid is rated Easy on LeetCode.
What is the time complexity of LeetCode 2639. Find the Width of Columns of a Grid?
The Python solution on this page runs in O(m \times n).
What is the space complexity of LeetCode 2639. Find the Width of Columns of a Grid?
The Python solution on this page uses O(\log M) auxiliary space.
What topics does LeetCode 2639. Find the Width of Columns of a Grid cover?
LeetCode 2639. Find the Width of Columns of a Grid is tagged Array and Matrix on LeetCode.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity — invisible to screen sharing.

Get Stealth Interview