Welcome to Subscribe On Youtube

Formatted question description: https://leetcode.ca/all/1855.html

1855. Maximum Distance Between a Pair of Values

Level

Medium

Description

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 <= 10^5
  • 1 <= nums2.length <= 10^5
  • 1 <= nums1[i], nums2[j] <= 10^5
  • Both nums1 and nums2 are non-increasing.

Solution

Use two pointers. Loop over index2 from nums2.length - 1 to 0. For each index2, find the smallest index1 such that nums1[index1] <= nums2[index2] and calculate index2 - index1 as the distance. Since the distance is at least 0, if index1 > index2, the distance is negative and will never exceed 0. Only the non-negative instances are stored to maintain the maximum distance.

  • class Solution {
        public int maxDistance(int[] nums1, int[] nums2) {
            int maxDistance = 0;
            int length1 = nums1.length, length2 = nums2.length;
            int index1 = length1 - 1, index2 = length2 - 1;
            while (index1 >= 0 && index2 >= 0) {
                while (index1 >= 0 && nums1[index1] <= nums2[index2]) {
                    maxDistance = Math.max(maxDistance, index2 - index1);
                    index1--;
                }
                index2--;
            }
            return maxDistance;
        }
    }
    
    ############
    
    class Solution {
        public int maxDistance(int[] nums1, int[] nums2) {
            int m = nums1.length, n = nums2.length;
            int ans = 0;
            for (int i = 0, j = 0; i < m; ++i) {
                while (j < n && nums1[i] <= nums2[j]) {
                    ++j;
                }
                ans = Math.max(ans, j - i - 1);
            }
            return ans;
        }
    }
    
  • // OJ: https://leetcode.com/problems/maximum-distance-between-a-pair-of-values/
    // Time: O(NlogN)
    // Space: O(1)
    class Solution {
    public:
        int maxDistance(vector<int>& A, vector<int>& B) {
            int ans = 0;
            for (int i = 0; i < A.size(); ++i) {
                int j = upper_bound(begin(B), end(B), A[i], greater()) - begin(B);
                ans = max(ans, max(0, j - 1 - i));
            }
            return ans;
        }
    };
    
  • class Solution:
        def maxDistance(self, nums1: List[int], nums2: List[int]) -> int:
            m, n = len(nums1), len(nums2)
            ans = i = j = 0
            while i < m:
                while j < n and nums1[i] <= nums2[j]:
                    j += 1
                ans = max(ans, j - i - 1)
                i += 1
            return ans
    
    ############
    
    # 1855. Maximum Distance Between a Pair of Values
    # https://leetcode.com/problems/maximum-distance-between-a-pair-of-values/
    
    class Solution:
        def maxDistance(self, nums1: List[int], nums2: List[int]) -> int:
        
            n1, n2 = len(nums1), len(nums2)
            res = 0
            i = j = 0
            
            while i < n1 or j < n2:
                if nums1[i] <= nums2[j]:
                    res = max(res, j - i)
                    if j < n2 - 1:
                        j += 1
                    elif i < n1 - 1:
                        i += 1
                    else:
                        break
                else:
                    if i < n1 - 1:
                        i += 1
                    elif j < n2 - 1:
                        j += 1
                    else:
                        break
            
            return res
    
    
  • func maxDistance(nums1 []int, nums2 []int) int {
    	m, n := len(nums1), len(nums2)
    	ans := 0
    	for i, j := 0, 0; i < m; i++ {
    		for j < n && nums1[i] <= nums2[j] {
    			j++
    		}
    		if ans < j-i-1 {
    			ans = j - i - 1
    		}
    	}
    	return ans
    }
    
  • function maxDistance(nums1: number[], nums2: number[]): number {
        let ans = 0;
        const m = nums1.length;
        const n = nums2.length;
        for (let i = 0, j = 0; i < m; ++i) {
            while (j < n && nums1[i] <= nums2[j]) {
                j++;
            }
            ans = Math.max(ans, j - i - 1);
        }
        return ans;
    }
    
    
  • /**
     * @param {number[]} nums1
     * @param {number[]} nums2
     * @return {number}
     */
    var maxDistance = function (nums1, nums2) {
        let ans = 0;
        const m = nums1.length;
        const n = nums2.length;
        for (let i = 0, j = 0; i < m; ++i) {
            while (j < n && nums1[i] <= nums2[j]) {
                j++;
            }
            ans = Math.max(ans, j - i - 1);
        }
        return ans;
    };
    
    
  • impl Solution {
        pub fn max_distance(nums1: Vec<i32>, nums2: Vec<i32>) -> i32 {
            let m = nums1.len();
            let n = nums2.len();
            let mut res = 0;
            let mut j = 0;
            for i in 0..m {
                while j < n && nums1[i] <= nums2[j] {
                    j += 1
                }
                res = res.max((j - i - 1) as i32)
            }
            res
        }
    }
    

All Problems

All Solutions