File tree 2 files changed +34
-0
lines changed
2 files changed +34
-0
lines changed Original file line number Diff line number Diff line change 473
473
621|[ Task Scheduler] ( ./0621-task-scheduler.js ) |Medium|
474
474
622|[ Design Circular Queue] ( ./0622-design-circular-queue.js ) |Medium|
475
475
623|[ Add One Row to Tree] ( ./0623-add-one-row-to-tree.js ) |Medium|
476
+ 624|[ Maximum Distance in Arrays] ( ./0624-maximum-distance-in-arrays.js ) |Medium|
476
477
628|[ Maximum Product of Three Numbers] ( ./0628-maximum-product-of-three-numbers.js ) |Easy|
477
478
637|[ Average of Levels in Binary Tree] ( ./0637-average-of-levels-in-binary-tree.js ) |Easy|
478
479
643|[ Maximum Average Subarray I] ( ./0643-maximum-average-subarray-i.js ) |Easy|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 624. Maximum Distance in Arrays
3
+ * https://leetcode.com/problems/maximum-distance-in-arrays/
4
+ * Difficulty: Medium
5
+ *
6
+ * You are given m arrays, where each array is sorted in ascending order.
7
+ *
8
+ * You can pick up two integers from two different arrays (each array picks one) and calculate
9
+ * the distance. We define the distance between two integers a and b to be their absolute
10
+ * difference |a - b|.
11
+ *
12
+ * Return the maximum distance.
13
+ */
14
+
15
+ /**
16
+ * @param {number[][] } arrays
17
+ * @return {number }
18
+ */
19
+ var maxDistance = function ( arrays ) {
20
+ let result = 0 ;
21
+ let min = arrays [ 0 ] [ 0 ] ;
22
+ let max = arrays [ 0 ] [ arrays [ 0 ] . length - 1 ] ;
23
+
24
+ for ( let i = 1 ; i < arrays . length ; i ++ ) {
25
+ const newMin = arrays [ i ] [ 0 ] ;
26
+ const newMax = arrays [ i ] [ arrays [ i ] . length - 1 ] ;
27
+ result = Math . max ( result , Math . abs ( newMax - min ) , Math . abs ( max - newMin ) ) ;
28
+ min = Math . min ( min , newMin ) ;
29
+ max = Math . max ( max , newMax ) ;
30
+ }
31
+
32
+ return result ;
33
+ } ;
You can’t perform that action at this time.
0 commit comments