-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathe3242.py
49 lines (40 loc) · 1.44 KB
/
e3242.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
class neighborSum:
def __init__(self, grid: List[List[int]]):
self.grid = grid
@cache
def findIndx(self, value: int) -> (int, int) :
for r in range(len(self.grid)) :
for c in range(len(self.grid[0])) :
if self.grid[r][c] == value :
return r, c
return -1, -1
@cache
def adjacentSum(self, value: int) -> int:
r, c = self.findIndx(value)
output = 0
if r - 1 >= 0 :
output += self.grid[r - 1][c]
if r + 1 < len(self.grid) :
output += self.grid[r + 1][c]
if c + 1 < len(self.grid[0]) :
output += self.grid[r][c + 1]
if c - 1 >= 0 :
output += self.grid[r][c - 1]
return output
@cache
def diagonalSum(self, value: int) -> int:
r, c = self.findIndx(value)
output = 0
if r - 1 >= 0 and c - 1 >= 0 :
output += self.grid[r - 1][c - 1]
if r - 1 >= 0 and c + 1 < len(self.grid[0]) :
output += self.grid[r - 1][c + 1]
if r + 1 < len(self.grid) and c - 1 >= 0 :
output += self.grid[r + 1][c - 1]
if r + 1 < len(self.grid) and c + 1 < len(self.grid[0]):
output += self.grid[r + 1][c + 1]
return output
# Your neighborSum object will be instantiated and called as such:
# obj = neighborSum(grid)
# param_1 = obj.adjacentSum(value)
# param_2 = obj.diagonalSum(value)