Skip to content

Commit c2f3a95

Browse files
committed
Add solution #624
1 parent 9bf7916 commit c2f3a95

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,7 @@
473473
621|[Task Scheduler](./0621-task-scheduler.js)|Medium|
474474
622|[Design Circular Queue](./0622-design-circular-queue.js)|Medium|
475475
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|
476477
628|[Maximum Product of Three Numbers](./0628-maximum-product-of-three-numbers.js)|Easy|
477478
637|[Average of Levels in Binary Tree](./0637-average-of-levels-in-binary-tree.js)|Easy|
478479
643|[Maximum Average Subarray I](./0643-maximum-average-subarray-i.js)|Easy|
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
};

0 commit comments

Comments
 (0)