You are given two non-increasing 0-indexed integer arrays nums1
and nums2
.
A pair of indices (i, j)
, where 0 <= i < nums1.length
and 0 <= j < nums2.length
, is valid if both i <= j
and nums1[i] <= nums2[j]
. The distance of the pair is j - i
.
Return the maximum distance of any valid pair (i, j)
. If there are no valid pairs, return 0
.
An array arr
is non-increasing if arr[i-1] >= arr[i]
for every 1 <= i < arr.length
.
Example 1:
Input: nums1 = [55,30,5,4,2], nums2 = [100,20,10,10,5] Output: 2 Explanation: The valid pairs are (0,0), (2,2), (2,3), (2,4), (3,3), (3,4), and (4,4). The maximum distance is 2 with pair (2,4).
Example 2:
Input: nums1 = [2,2,2], nums2 = [10,10,1] Output: 1 Explanation: The valid pairs are (0,0), (0,1), and (1,1). The maximum distance is 1 with pair (0,1).
Example 3:
Input: nums1 = [30,29,19,5], nums2 = [25,25,25,25,25] Output: 2 Explanation: The valid pairs are (2,2), (2,3), (2,4), (3,3), and (3,4). The maximum distance is 2 with pair (2,4).
Example 4:
Input: nums1 = [5,4], nums2 = [3,2] Output: 0 Explanation: There are no valid pairs, so return 0.
Constraints:
1 <= nums1.length <= 105
1 <= nums2.length <= 105
1 <= nums1[i], nums2[j] <= 105
- Both
nums1
andnums2
are non-increasing.
Solution: Two Pointers
For each i, find the largest j such that nums[j] >= nums[i].
Time complexity: O(n + m)
Space complexity: O(1)
C++
1 2 3 4 5 6 7 8 9 10 11 12 |
// Author: Huahua class Solution { public: int maxDistance(vector<int>& nums1, vector<int>& nums2) { int ans = 0; for (int i = 0, j = -1; i < nums1.size(); ++i) { while (j + 1 < nums2.size() && nums2[j + 1] >= nums1[i]) ++j; if (j >= i) ans = max(ans, j - i); } return ans; } }; |
请尊重作者的劳动成果,转载请注明出处!花花保留对文章/视频的所有权利。
如果您喜欢这篇文章/视频,欢迎您捐赠花花。
If you like my articles / videos, donations are welcome.
Be First to Comment