/**
* Problem: LeetCode 1855 - Maximum Distance Between a Pair of Values
* Difficulty: Medium
* Language: Java
* Author: CodeByTushu
*/
class Solution {
public int maxDistance(int[] nums1, int[] nums2) {
int maxDist = 0;
int i = 0;
int j = 0;
final int m = nums1.length;
final int n = nums2.length;
while (i < m && j < n) {
if (nums1[i] <= nums2[j]) {
maxDist = Math.max(maxDist, j - i);
j++; // Greedily extend distance with current i
} else {
i++; // nums1[i] is too large for remaining elements in nums2
}
}
return maxDist;
}
}
"""
Problem: LeetCode 1855 - Maximum Distance Between a Pair of Values
Difficulty: Medium
Language: Python
Author: CodeByTushu
"""
from typing import List
class Solution:
def maxDistance(self, nums1: List[int], nums2: List[int]) -> int:
max_dist = 0
i = 0
j = 0
m, n = len(nums1), len(nums2)
while i < m and j < n:
if nums1[i] <= nums2[j]:
max_dist = max(max_dist, j - i)
j += 1
else:
i += 1
return max_dist
/**
* Problem: LeetCode 1855 - Maximum Distance Between a Pair of Values
* Difficulty: Medium
* Language: C++
* Author: CodeByTushu
*/
#include <vector>
#include <algorithm>
using namespace std;
class Solution {
public:
int maxDistance(vector<int>& nums1, vector<int>& nums2) {
int maxDist = 0;
int i = 0;
int j = 0;
const int m = nums1.size();
const int n = nums2.size();
while (i < m && j < n) {
if (nums1[i] <= nums2[j]) {
maxDist = max(maxDist, j - i);
j++;
} else {
i++;
}
}
return maxDist;
}
};
/**
* Problem: LeetCode 1855 - Maximum Distance Between a Pair of Values
* Difficulty: Medium
* Language: JavaScript
* Author: CodeByTushu
*/
/**
* @param {number[]} nums1
* @param {number[]} nums2
* @return {number}
*/
var maxDistance = function(nums1, nums2) {
let maxDist = 0;
let i = 0;
let j = 0;
const m = nums1.length;
const n = nums2.length;
while (i < m && j < n) {
if (nums1[i] <= nums2[j]) {
if (j - i > maxDist) {
maxDist = j - i;
}
j++;
} else {
i++;
}
}
return maxDist;
};