47
47
48
48
** 方法一:枚举**
49
49
50
- 从左上角开始,枚举每个可能的沙漏的中间坐标 $ (i, j)$,计算沙漏的元素和,取最大值即可 。
50
+ 我们观察题目发现,每个沙漏就是一个 $3 \times 3$ 的矩阵挖去中间行的首尾两个元素。因此,我们可以从左上角开始,枚举每个沙漏的中间坐标 $ (i, j)$,然后计算沙漏的元素和,取其中的最大值即可 。
51
51
52
- 时间复杂度 $O(m\times n)$。其中 $m$ 和 $n$ 分别是矩阵的行数和列数。
52
+ 时间复杂度 $O(m \times n)$,空间复杂度 $O(1 )$。其中 $m$ 和 $n$ 分别是矩阵的行数和列数。
53
53
54
54
<!-- tabs:start -->
55
55
@@ -64,14 +64,10 @@ class Solution:
64
64
ans = 0
65
65
for i in range (1 , m - 1 ):
66
66
for j in range (1 , n - 1 ):
67
- t = 0
68
- for x in [i - 1 , i, i + 1 ]:
69
- for y in [j - 1 , j, j + 1 ]:
70
- t += grid[x][y]
71
-
72
- t -= grid[i][j - 1 ]
73
- t -= grid[i][j + 1 ]
74
- ans = max (ans, t)
67
+ s = - grid[i][j - 1 ] - grid[i][j + 1 ]
68
+ s += sum (grid[x][y] for x in range (i - 1 , i + 2 )
69
+ for y in range (j - 1 , j + 2 ))
70
+ ans = max (ans, s)
75
71
return ans
76
72
```
77
73
@@ -86,15 +82,13 @@ class Solution {
86
82
int ans = 0 ;
87
83
for (int i = 1 ; i < m - 1 ; ++ i) {
88
84
for (int j = 1 ; j < n - 1 ; ++ j) {
89
- int t = 0 ;
85
+ int s = - grid[i][j - 1 ] - grid[i][j + 1 ] ;
90
86
for (int x = i - 1 ; x <= i + 1 ; ++ x) {
91
87
for (int y = j - 1 ; y <= j + 1 ; ++ y) {
92
- t += grid[x][y];
88
+ s += grid[x][y];
93
89
}
94
90
}
95
- t -= grid[i][j - 1 ];
96
- t -= grid[i][j + 1 ];
97
- ans = Math . max(ans, t);
91
+ ans = Math . max(ans, s);
98
92
}
99
93
}
100
94
return ans;
@@ -112,15 +106,13 @@ public:
112
106
int ans = 0;
113
107
for (int i = 1; i < m - 1; ++i) {
114
108
for (int j = 1; j < n - 1; ++j) {
115
- int t = 0 ;
109
+ int s = -grid [ i ] [ j - 1 ] - grid [ i ] [ j + 1 ] ;
116
110
for (int x = i - 1; x <= i + 1; ++x) {
117
111
for (int y = j - 1; y <= j + 1; ++y) {
118
- t += grid[ x] [ y ] ;
112
+ s += grid[ x] [ y ] ;
119
113
}
120
114
}
121
- t -= grid[ i] [ j - 1 ] ;
122
- t -= grid[ i] [ j + 1 ] ;
123
- ans = max(ans, t);
115
+ ans = max(ans, s);
124
116
}
125
117
}
126
118
return ans;
@@ -131,23 +123,20 @@ public:
131
123
### **Go**
132
124
133
125
```go
134
- func maxSum(grid [][]int) int {
126
+ func maxSum(grid [][]int) (ans int) {
135
127
m, n := len(grid), len(grid[0])
136
- ans := 0
137
128
for i := 1; i < m-1; i++ {
138
129
for j := 1; j < n-1; j++ {
139
- t := 0
130
+ s := -grid[i][j-1] - grid[i][j+1]
140
131
for x := i - 1; x <= i+1; x++ {
141
132
for y := j - 1; y <= j+1; y++ {
142
- t += grid[x][y]
133
+ s += grid[x][y]
143
134
}
144
135
}
145
- t -= grid[i][j-1]
146
- t -= grid[i][j+1]
147
- ans = max(ans, t)
136
+ ans = max(ans, s)
148
137
}
149
138
}
150
- return ans
139
+ return
151
140
}
152
141
153
142
func max(a, b int) int {
@@ -162,21 +151,18 @@ func max(a, b int) int {
162
151
163
152
``` ts
164
153
function maxSum(grid : number [][]): number {
165
- const m = grid .length ,
166
- n = grid [0 ].length ;
167
- let threeSum = Array .from ({ length: m }, () => new Array (n - 2 ).fill (0 ));
168
- for (let i = 0 ; i < m ; i ++ ) {
169
- for (let j = 1 ; j < n - 1 ; j ++ ) {
170
- threeSum [i ][j - 1 ] = grid [i ][j - 1 ] + grid [i ][j ] + grid [i ][j + 1 ];
171
- }
172
- }
154
+ const m = grid .length ;
155
+ const n = grid [0 ].length ;
173
156
let ans = 0 ;
174
- for (let i = 1 ; i < m - 1 ; i ++ ) {
175
- for (let j = 1 ; j < n - 1 ; j ++ ) {
176
- ans = Math .max (
177
- ans ,
178
- threeSum [i - 1 ][j - 1 ] + grid [i ][j ] + threeSum [i + 1 ][j - 1 ],
179
- );
157
+ for (let i = 1 ; i < m - 1 ; ++ i ) {
158
+ for (let j = 1 ; j < n - 1 ; ++ j ) {
159
+ let s = - grid [i ][j - 1 ] - grid [i ][j + 1 ];
160
+ for (let x = i - 1 ; x <= i + 1 ; ++ x ) {
161
+ for (let y = j - 1 ; y <= j + 1 ; ++ y ) {
162
+ s += grid [x ][y ];
163
+ }
164
+ }
165
+ ans = Math .max (ans , s );
180
166
}
181
167
}
182
168
return ans ;
0 commit comments