Maximum Distance Between a Pair of Values LeetCode solution in JavaScript

1
var maxDistance = function (nums1, nums2) {
2
let i = 0,
3
j = 0;
4

5
let ans = 0;
6
while (i < nums1.length && j < nums2.length) {
7
// maintain the i <= j invariant
8
j = Math.max(j, i);
9

10
// we want to maximize j so move it forward whenever possible
11
while (nums1[i] <= nums2[j]) {
12
ans = Math.max(ans, j - i);
13
j++;
14
}
15

16
// we want to minimize i so move it forward only to maintain invariants
17
i++;
18
}
19

20
return ans;
21
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0