Skip to content

Commit 7197798

Browse files
authored
Create lucky-numbers-in-a-matrix.py
1 parent 3c4efbc commit 7197798

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

Python/lucky-numbers-in-a-matrix.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Time: O(m * n)
2+
# Space: O(m + n)
3+
4+
import itertools
5+
6+
7+
class Solution(object):
8+
def luckyNumbers (self, matrix):
9+
"""
10+
:type matrix: List[List[int]]
11+
:rtype: List[int]
12+
"""
13+
rows = map(min, matrix)
14+
cols = map(max, itertools.izip(*matrix))
15+
return [cell for i, row in enumerate(matrix)
16+
for j, cell in enumerate(row) if rows[i] == cols[j]]
17+
18+
19+
# Time: O(m * n)
20+
# Space: O(m + n)
21+
import itertools
22+
23+
24+
class Solution2(object):
25+
def luckyNumbers (self, matrix):
26+
"""
27+
:type matrix: List[List[int]]
28+
:rtype: List[int]
29+
"""
30+
return list(set(map(min, matrix)) &
31+
set(map(max, itertools.izip(*matrix))))
32+

0 commit comments

Comments
 (0)