Find the Width of Columns of a Grid — LeetCode 2639 Python Solution
EasyArrayMatrix
- Problem
- #2639
- Pattern
- Matrix and Grid
- Reading time
- 2 min
- Source
- leetcode.com
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
| Measure | Complexity |
|---|---|
| Time | O(m \times n) |
| Space | O(\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.