File tree Expand file tree Collapse file tree 1 file changed +35
-1
lines changed Expand file tree Collapse file tree 1 file changed +35
-1
lines changed Original file line number Diff line number Diff line change @@ -273,4 +273,38 @@ const greatestProduct = (matrix) => {
273
273
}
274
274
}
275
275
276
- console . log ( greatestProduct ( matr ) )
276
+ console . log ( greatestProduct ( matr ) )
277
+
278
+
279
+ const greatestProductOfDiagonals = matrix => {
280
+ let max = 0 ;
281
+ //1. iterate the rows;
282
+ for ( i = 0 ; i < matrix . length ; i ++ ) {
283
+
284
+ //2. iterate the columns;
285
+ for ( j = 0 ; j < matrix . length ; j ++ ) {
286
+
287
+ // check the maximum product in diagonal going through down - right
288
+ if ( ( ( i - 3 ) >= 0 ) && ( ( j - 3 ) >= 0 ) ) {
289
+ result = ( matrix [ i ] [ j ] * matrix [ i - 1 ] [ j - 1 ] *
290
+ matrix [ i - 2 ] [ j - 2 ] * matrix [ i - 3 ] [ j - 3 ] )
291
+
292
+ if ( max < result ) {
293
+ max = result }
294
+ }
295
+
296
+ //check the maximum product in diagonal going through up - right
297
+ if ( ( ( i - 3 ) >= 0 ) && ( ( j - 1 ) <= 0 ) ) {
298
+ result = ( matrix [ i ] [ j ] * matrix [ i - 1 ] [ j + 1 ] *
299
+ matrix [ i - 2 ] [ j + 2 ] * matrix [ i - 3 ] [ j + 3 ] )
300
+
301
+ if ( max < result ) {
302
+ max = result }
303
+ }
304
+
305
+
306
+ } return max
307
+ }
308
+ }
309
+
310
+ greatestProductOfDiagonals ( matr )
You can’t perform that action at this time.
0 commit comments