EasyNo. 2639

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

Topics
Array, Matrix
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

PythonSolution
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

Time
O(m × n)
Space · auxiliary
O(log M)

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

Related problems
No.ProblemDifficulty
2643Row With Maximum OnesEasy
0054Spiral MatrixMedium
0059Spiral Matrix IIMedium
0289Game of LifeMedium
0498Diagonal TraverseMedium
0566Reshape the MatrixEasy

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 × 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.

Free to download. Check it runs on your machine, then pick a plan.