Skip to content

Commit ffbad56

Browse files
authored
Create Maximum Distance Between a Pair of Values.java
1 parent e521241 commit ffbad56

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
public int maxDistance(int[] nums1, int[] nums2) {
3+
int maxDistance = 0;
4+
for (int i = 0; i < nums1.length; i++) {
5+
int idx = getValidIdx(nums2, i, nums2.length - 1, nums1[i]);
6+
if (idx != -1) {
7+
maxDistance = Math.max(maxDistance, idx - i);
8+
}
9+
}
10+
return maxDistance;
11+
}
12+
13+
private int getValidIdx(int[] nums, int start, int end, int limit) {
14+
int idx = -1;
15+
while (start <= end) {
16+
int mid = (start + end) / 2;
17+
if (nums[mid] >= limit) {
18+
idx = Math.max(idx, mid);
19+
start = mid + 1;
20+
} else {
21+
end = mid - 1;
22+
}
23+
}
24+
return idx;
25+
}
26+
}

0 commit comments

Comments
 (0)