-
-
Notifications
You must be signed in to change notification settings - Fork 300
/
661.py
77 lines (70 loc) · 3.19 KB
/
661.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
__________________________________________________________________________________________________
sample 468 ms submission
import math
class Solution:
def imageSmoother(self, M):
if not M:
return
row = len(M)
col = 0
if M[0]:
col = len(M[0])
copy = [[0]*col for i in range(row)]
for i in range(1, row-1):
for j in range(1, col-1):
copy[i][j] = math.floor((M[i-1][j-1]+M[i-1][j]+M[i-1][j+1]+M[i][j-1]+M[i][j]+M[i][j+1]+M[i+1][j-1]+M[i+1][j]+M[i+1][j+1]) / 9)
if row > 1:
#first row
for j in range(1, col-1):
copy[0][j] = math.floor((M[0][j-1]+M[0][j]+M[0][j+1]+M[1][j-1]+M[1][j]+M[1][j+1]) / 6)
#last row
for j in range(1, col-1):
copy[row-1][j] = math.floor((M[row-1][j-1]+M[row-1][j]+M[row-1][j+1]+M[row-2][j-1]+M[row-2][j]+M[row-2][j+1]) / 6)
if col > 1:
#first col
for i in range(1, row-1):
copy[i][0] = math.floor((M[i-1][0]+M[i-1][1]+M[i][0]+M[i][1]+M[i+1][0]+M[i+1][1]) / 6)
#last col
for i in range(1, row-1):
copy[i][col-1] = math.floor((M[i-1][col-2]+M[i-1][col-1]+M[i][col-2]+M[i][col-1]+M[i+1][col-2]+M[i+1][col-1]) / 6)
else:
for i in range(1, row-1):
copy[i][0] = math.floor((M[i-1][0]+M[i][0]+M[i+1][0]) / 3)
else:
#one row
for j in range(1, col-1):
copy[0][j] = math.floor((M[0][j-1]+M[0][j]+M[0][j+1]) / 3)
#4 corners
if row > 1:
if col > 1:
copy[0][0] = math.floor((M[0][0]+M[0][1]+M[1][0]+M[1][1]) / 4)
copy[0][col-1] = math.floor((M[0][col-2]+M[0][col-1]+M[1][col-2]+M[1][col-1]) / 4)
copy[row-1][0] = math.floor((M[row-2][0]+M[row-2][1]+M[row-1][0]+M[row-1][1]) / 4)
copy[row-1][col-1] = math.floor((M[row-2][col-2]+M[row-2][col-1]+M[row-1][col-2]+M[row-1][col-1]) / 4)
else:
copy[0][0] = math.floor((M[0][0]+M[1][0]) / 2)
copy[row-1][0] = math.floor((M[row-2][0]+M[row-1][0]) / 2)
else:
if col > 1:
copy[0][0] = math.floor((M[0][0]+M[0][1]) / 2)
copy[0][col-1] = math.floor((M[0][col-2]+M[0][col-1]) / 2)
else:
copy[0][0] = M[0][0]
return copy
__________________________________________________________________________________________________
sample 13504 kb submission
class Solution:
def imageSmoother(self, M: List[List[int]]) -> List[List[int]]:
R, C = len(M), len(M[0])
ans = [[0] * C for _ in M]
for r in range(R):
for c in range(C):
count = 0
for nr in (r-1, r, r+1):
for nc in (c-1, c, c+1):
if 0 <= nr < R and 0 <= nc < C:
ans[r][c] += M[nr][nc]
count += 1
ans[r][c] //= count
return ans
__________________________________________________________________________________________________