Skip to content

Commit 6faff35

Browse files
authored
Create cells-with-odd-values-in-a-matrix.py
1 parent 45c2316 commit 6faff35

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Time: O(n + m)
2+
# Space: O(n + m)
3+
4+
class Solution(object):
5+
def oddCells(self, n, m, indices):
6+
"""
7+
:type n: int
8+
:type m: int
9+
:type indices: List[List[int]]
10+
:rtype: int
11+
"""
12+
row, col = [0]*n, [0]*m
13+
for r, c in indices:
14+
row[r] ^= 1
15+
col[c] ^= 1
16+
row_sum, col_sum = sum(row), sum(col)
17+
return row_sum*m+col_sum*n-2*row_sum*col_sum
18+
19+
20+
# Time: O(n + m)
21+
# Space: O(n + m)
22+
import collections
23+
import itertools
24+
25+
26+
class Solution2(object):
27+
def oddCells(self, n, m, indices):
28+
"""
29+
:type n: int
30+
:type m: int
31+
:type indices: List[List[int]]
32+
:rtype: int
33+
"""
34+
fn = lambda x: sum(count&1 for count in collections.Counter(x).itervalues())
35+
row_sum, col_sum = map(fn, itertools.izip(*indices))
36+
return row_sum*m+col_sum*n-2*row_sum*col_sum

0 commit comments

Comments
 (0)