-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0624-maximum-distance-in-arrays.js
More file actions
44 lines (39 loc) · 1.09 KB
/
0624-maximum-distance-in-arrays.js
File metadata and controls
44 lines (39 loc) · 1.09 KB
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
/**
* Maximum Distance In Arrays
* Time Complexity: O(M)
* Space Complexity: O(1)
*/
var maxDistance = function (arrays) {
let maxDistanceResult = 0;
let overallMinimumValue = arrays[0][0];
let overallMaximumValue = arrays[0][arrays[0].length - 1];
for (
let currentArrayIndex = 1;
currentArrayIndex < arrays.length;
currentArrayIndex++
) {
let currentArray = arrays[currentArrayIndex];
let currentArrayFirstElement = currentArray[0];
let currentArrayLastElement = currentArray[currentArray.length - 1];
let potentialDifferenceOne = Math.abs(
currentArrayLastElement - overallMinimumValue,
);
let potentialDifferenceTwo = Math.abs(
overallMaximumValue - currentArrayFirstElement,
);
maxDistanceResult = Math.max(
maxDistanceResult,
potentialDifferenceOne,
potentialDifferenceTwo,
);
overallMinimumValue = Math.min(
overallMinimumValue,
currentArrayFirstElement,
);
overallMaximumValue = Math.max(
overallMaximumValue,
currentArrayLastElement,
);
}
return maxDistanceResult;
};