1855. Maximum Distance Between a Pair of Values Medium

@problem@discussion
#Array#Two Pointers#Binary Search#Greedy



1/**
2 * [1855] Maximum Distance Between a Pair of Values
3 *
4 * You are given two non-increasing 0-indexed integer arrays nums1​​​​​​ and nums2​​​​​​.
5 * 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​​​​.
6 * Return the maximum distance of any valid pair (i, j). If there are no valid pairs, return 0.
7 * An array arr is non-increasing if arr[i-1] >= arr[i] for every 1 <= i < arr.length.
8 *  
9 * Example 1:
10 * 
11 * Input: nums1 = [55,30,5,4,2], nums2 = [100,20,10,10,5]
12 * Output: 2
13 * Explanation: The valid pairs are (0,0), (2,2), (2,3), (2,4), (3,3), (3,4), and (4,4).
14 * The maximum distance is 2 with pair (2,4).
15 * 
16 * Example 2:
17 * 
18 * Input: nums1 = [2,2,2], nums2 = [10,10,1]
19 * Output: 1
20 * Explanation: The valid pairs are (0,0), (0,1), and (1,1).
21 * The maximum distance is 1 with pair (0,1).
22 * 
23 * Example 3:
24 * 
25 * Input: nums1 = [30,29,19,5], nums2 = [25,25,25,25,25]
26 * Output: 2
27 * Explanation: The valid pairs are (2,2), (2,3), (2,4), (3,3), and (3,4).
28 * The maximum distance is 2 with pair (2,4).
29 * 
30 *  
31 * Constraints:
32 * 
33 * 	1 <= nums1.length, nums2.length <= 10^5
34 * 	1 <= nums1[i], nums2[j] <= 10^5
35 * 	Both nums1 and nums2 are non-increasing.
36 * 
37 */
38pub struct Solution {}
39
40// problem: https://leetcode.com/problems/maximum-distance-between-a-pair-of-values/
41// discuss: https://leetcode.com/problems/maximum-distance-between-a-pair-of-values/discuss/?currentPage=1&orderBy=most_votes&query=
42
43// submission codes start here
44
45impl Solution {
46    pub fn max_distance(nums1: Vec<i32>, nums2: Vec<i32>) -> i32 {
47        0
48    }
49}
50
51// submission codes end
52
53#[cfg(test)]
54mod tests {
55    use super::*;
56
57    #[test]
58    fn test_1855() {
59    }
60}
61


Back
© 2025 bowen.ge All Rights Reserved.