Skip to content

Commit 08b691e

Browse files
authored
Create lonely-pixel-ii.py
1 parent 59955f5 commit 08b691e

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

Python/lonely-pixel-ii.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Time: O(m * n)
2+
# Space: O(m * n)
3+
4+
class Solution(object):
5+
def findBlackPixel(self, picture, N):
6+
"""
7+
:type picture: List[List[str]]
8+
:type N: int
9+
:rtype: int
10+
"""
11+
rows, cols = [0] * len(picture), [0] * len(picture[0])
12+
lookup = collections.defaultdict(int)
13+
for i in xrange(len(picture)):
14+
for j in xrange(len(picture[0])):
15+
if picture[i][j] == 'B':
16+
rows[i] += 1
17+
cols[j] += 1
18+
lookup[tuple(picture[i])] += 1
19+
20+
result = 0
21+
for i in xrange(len(picture)):
22+
if rows[i] == N and lookup[tuple(picture[i])] == N:
23+
for j in xrange(len(picture[0])):
24+
result += picture[i][j] == 'B' and cols[j] == N
25+
return result
26+
27+
28+
class Solution2(object):
29+
def findBlackPixel(self, picture, N):
30+
"""
31+
:type picture: List[List[str]]
32+
:type N: int
33+
:rtype: int
34+
"""
35+
lookup = collections.Counter(map(tuple, picture))
36+
cols = [col.count('B') for col in zip(*picture)]
37+
return sum(N * zip(row, cols).count(('B', N)) \
38+
for row, cnt in lookup.iteritems() \
39+
if cnt == N == row.count('B'))

0 commit comments

Comments
 (0)