forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
find-the-width-of-columns-of-a-grid.py
48 lines (40 loc) · 1.06 KB
/
find-the-width-of-columns-of-a-grid.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# 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)]