File tree 3 files changed +48
-2
lines changed
solution/2400-2499/2428.Maximum Sum of an Hourglass
3 files changed +48
-2
lines changed Original file line number Diff line number Diff line change @@ -161,7 +161,22 @@ func max(a, b int) int {
161
161
### ** TypeScript**
162
162
163
163
``` ts
164
-
164
+ function maxSum(grid : number [][]): number {
165
+ const m = grid .length , n = grid [0 ].length ;
166
+ let threeSum = Array .from ({ length: m }, () => new Array (n - 2 ).fill (0 ));
167
+ for (let i = 0 ; i < m ; i ++ ) {
168
+ for (let j = 1 ; j < n - 1 ; j ++ ) {
169
+ threeSum [i ][j - 1 ] = grid [i ][j - 1 ] + grid [i ][j ] + grid [i ][j + 1 ];
170
+ }
171
+ }
172
+ let ans = 0 ;
173
+ for (let i = 1 ; i < m - 1 ; i ++ ) {
174
+ for (let j = 1 ; j < n - 1 ; j ++ ) {
175
+ ans = Math .max (ans , threeSum [i - 1 ][j - 1 ] + grid [i ][j ] + threeSum [i + 1 ][j - 1 ]);
176
+ }
177
+ }
178
+ return ans ;
179
+ };
165
180
```
166
181
167
182
### ** ...**
Original file line number Diff line number Diff line change @@ -147,7 +147,22 @@ func max(a, b int) int {
147
147
### ** TypeScript**
148
148
149
149
``` ts
150
-
150
+ function maxSum(grid : number [][]): number {
151
+ const m = grid .length , n = grid [0 ].length ;
152
+ let threeSum = Array .from ({ length: m }, () => new Array (n - 2 ).fill (0 ));
153
+ for (let i = 0 ; i < m ; i ++ ) {
154
+ for (let j = 1 ; j < n - 1 ; j ++ ) {
155
+ threeSum [i ][j - 1 ] = grid [i ][j - 1 ] + grid [i ][j ] + grid [i ][j + 1 ];
156
+ }
157
+ }
158
+ let ans = 0 ;
159
+ for (let i = 1 ; i < m - 1 ; i ++ ) {
160
+ for (let j = 1 ; j < n - 1 ; j ++ ) {
161
+ ans = Math .max (ans , threeSum [i - 1 ][j - 1 ] + grid [i ][j ] + threeSum [i + 1 ][j - 1 ]);
162
+ }
163
+ }
164
+ return ans ;
165
+ };
151
166
```
152
167
153
168
### ** ...**
Original file line number Diff line number Diff line change
1
+ function maxSum ( grid : number [ ] [ ] ) : number {
2
+ const m = grid . length , n = grid [ 0 ] . length ;
3
+ let threeSum = Array . from ( { length : m } , ( ) => new Array ( n - 2 ) . fill ( 0 ) ) ;
4
+ for ( let i = 0 ; i < m ; i ++ ) {
5
+ for ( let j = 1 ; j < n - 1 ; j ++ ) {
6
+ threeSum [ i ] [ j - 1 ] = grid [ i ] [ j - 1 ] + grid [ i ] [ j ] + grid [ i ] [ j + 1 ] ;
7
+ }
8
+ }
9
+ let ans = 0 ;
10
+ for ( let i = 1 ; i < m - 1 ; i ++ ) {
11
+ for ( let j = 1 ; j < n - 1 ; j ++ ) {
12
+ ans = Math . max ( ans , threeSum [ i - 1 ] [ j - 1 ] + grid [ i ] [ j ] + threeSum [ i + 1 ] [ j - 1 ] ) ;
13
+ }
14
+ }
15
+ return ans ;
16
+ } ;
You can’t perform that action at this time.
0 commit comments