This problem () asks you to find the maximum distance j - i such that:
i <= jnums1[i] <= nums2[j]- Both arrays are non-increasing (sorted in descending order)
Key Idea
Since both arrays are sorted in descending order, you can use a two-pointer approach:
- Start with
i = 0andj = 0 - If
nums1[i] <= nums2[j], it's valid → update answer and movej++(try to increase distance) - Otherwise → move
i++(need a smaller value innums1)
This works in O(n + m) time.
.png&w=3840&q=75)