2639. Find the Width of Columns of a Grid
Read the full problem statement on LeetCode.
Difficulty: easy Acceptance: 69% Topics: Array, Matrix
View full problem on LeetCode Reading material
Reference solution (spoiler · python)
# Time: O(m * n)
# Space: O(1)
# array
class Solution(object):
def findColumnWidth(self, grid):
"""
:type grid: List[List[int]]
:rtype: List[int]
"""
def length(x):
l = 1
if x < 0:
x = -x
l += 1
while x >= 10:
x //= 10
l += 1
return l
return [max(length(grid[i][j]) for i in xrange(len(grid))) for j in xrange(len(grid[0]))]
# Time: O(m * n)
# Space: O(logr)
# array
class Solution2(object):
def findColumnWidth(self, grid):
"""
:type grid: List[List[int]]
:rtype: List[int]
"""
return [max(len(str(grid[i][j])) for i in xrange(len(grid))) for j in xrange(len(grid[0]))]
# Time: O(m * n)
# Space: O(m + logr)
import itertools
# array
class Solution3(object):
def findColumnWidth(self, grid):
"""
:type grid: List[List[int]]
:rtype: List[int]
"""
return [max(len(str(x)) for x in col) for col in itertools.izip(*grid)]
Solution from kamyu104/LeetCode-Solutions · MIT
Similar questions