Maximum Distance Between a Pair of Values LeetCode solution in C++

1
class Solution {
2
public:
3
int maxDistance(vector<int> &nums1, vector<int> &nums2) {
4
reverse(nums2.begin(), nums2.end());
5
int ans = 0;
6
for (int i = 0; i < nums1.size(); ++i) {
7
auto it = lower_bound(nums2.begin(), nums2.end(), nums1[i]) -
8
nums2.begin(); // Finds first element greater than or equal to nums1[i]
9
int j = nums2.size() - 1 - it; // Index of the found element in the original array
10
if (i <= j) ans = max(ans, j - i); // Update distance
11
}
12
return ans;
13
}
14
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0