-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
right-triangles.py
63 lines (55 loc) · 1.86 KB
/
right-triangles.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# Time: O(n * m)
# Space: O(min(n, m))
# combinatorics
class Solution(object):
def numberOfRightTriangles(self, grid):
"""
:type grid: List[List[int]]
:rtype: int
"""
def get(i, j):
return grid[i][j] if n < m else grid[j][i]
n, m = len(grid), len(grid[0])
result = 0
cnt1 = [sum(get(i, j) for j in xrange(max(n, m))) for i in xrange(min(n, m))]
for j in xrange(max(n, m)):
cnt2 = sum(get(i, j) for i in xrange(min(n, m)))
result += sum((cnt1[i]-1)*(cnt2-1) for i in xrange(min(n, m)) if get(i, j))
return result
# Time: O(n * m)
# Space: O(n + m)
# combinatorics
class Solution2(object):
def numberOfRightTriangles(self, grid):
"""
:type grid: List[List[int]]
:rtype: int
"""
n, m = len(grid), len(grid[0])
cnt1 = [sum(grid[i][j] for j in xrange(m)) for i in xrange(n)]
cnt2 = [sum(grid[i][j] for i in xrange(n)) for j in xrange(m)]
return sum((cnt1[i]-1)*(cnt2[j]-1) for i in xrange(n) for j in xrange(m) if grid[i][j])
# Time: O(n * m)
# Space: O(min(n, m))
# freq table
class Solution3(object):
def numberOfRightTriangles(self, grid):
"""
:type grid: List[List[int]]
:rtype: int
"""
def get(i, j):
return grid[i][j] if n < m else grid[j][i]
def count(direction):
result = 0
cnt = [0]*min(n, m)
for j in direction(xrange(max(n, m))):
c = sum(get(i, j) for i in xrange(len(cnt)))
for i in xrange(len(cnt)):
if get(i, j) == 0:
continue
result += cnt[i]
cnt[i] += c-1
return result
n, m = len(grid), len(grid[0])
return count(lambda x: x)+count(reversed)