Skip to content

Commit eddb80c

Browse files
authored
Create number-of-enclaves.py
1 parent 1aa8850 commit eddb80c

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

Python/number-of-enclaves.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Time: O(m * n)
2+
# Space: O(m * n)
3+
4+
class Solution(object):
5+
def numEnclaves(self, A):
6+
"""
7+
:type A: List[List[int]]
8+
:rtype: int
9+
"""
10+
directions = [(0, -1), (0, 1), (-1, 0), (1, 0)]
11+
def dfs(A, i, j):
12+
if not (0 <= i < len(A) and 0 <= j < len(A[0]) and A[i][j]):
13+
return
14+
A[i][j] = 0
15+
for d in directions:
16+
dfs(A, i+d[0], j+d[1])
17+
18+
for i in xrange(len(A)):
19+
dfs(A, i, 0)
20+
dfs(A, i, len(A[0])-1)
21+
for j in xrange(1, len(A[0])-1):
22+
dfs(A, 0, j)
23+
dfs(A, len(A)-1, j)
24+
return sum(sum(row) for row in A)
25+

0 commit comments

Comments
 (0)