-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
design-neighbor-sum-service.py
48 lines (38 loc) · 1.18 KB
/
design-neighbor-sum-service.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
# Time: ctor: O(n^2)
# adjacentSum: O(1)
#. diagonalSum: O(1)
# Space: O(n^2)
# hash table
class neighborSum(object):
ADJACENTS = ((1, 0), (0, 1), (-1, 0), (0, -1))
DIAGONALS = ((1, 1), (1, -1), (-1, 1), (-1, -1))
def __init__(self, grid):
"""
:type grid: List[List[int]]
"""
self.__grid = grid
self.__lookup = [None]*(len(grid)*len(grid[0]))
for i in xrange(len(grid)):
for j in xrange(len(grid[0])):
self.__lookup[grid[i][j]] = (i, j)
def adjacentSum(self, value):
"""
:type value: int
:rtype: int
"""
return self.__sum(value, neighborSum.ADJACENTS)
def diagonalSum(self, value):
"""
:type value: int
:rtype: int
"""
return self.__sum(value, neighborSum.DIAGONALS)
def __sum(self, value, directions):
i, j = self.__lookup[value]
total = 0
for di, dj in directions:
ni, nj = i+di, j+dj
if not (0 <= ni < len(self.__grid) and 0 <= nj < len(self.__grid[0])):
continue
total += self.__grid[ni][nj]
return total