返回介绍

solution / 1800-1899 / 1855.Maximum Distance Between a Pair of Values / README_EN

发布于 2024-06-17 01:03:13 字数 9021 浏览 0 评论 0 收藏 0

1855. Maximum Distance Between a Pair of Values

中文文档

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).

 

Constraints:

  • 1 <= nums1.length, nums2.length <= 105
  • 1 <= nums1[i], nums2[j] <= 105
  • Both nums1 and nums2 are non-increasing.

Solutions

Solution 1: Binary Search

Assume the lengths of $nums1$ and $nums2$ are $m$ and $n$ respectively.

Traverse array $nums1$, for each number $nums1[i]$, perform a binary search for numbers in $nums2$ in the range $[i,n)$, find the last position $j$ that is greater than or equal to $nums1[i]$, calculate the distance between this position and $i$, and update the maximum distance value $ans$.

The time complexity is $O(m \times \log n)$, where $m$ and $n$ are the lengths of $nums1$ and $nums2$ respectively. The space complexity is $O(1)$.

class Solution:
  def maxDistance(self, nums1: List[int], nums2: List[int]) -> int:
    ans = 0
    nums2 = nums2[::-1]
    for i, v in enumerate(nums1):
      j = len(nums2) - bisect_left(nums2, v) - 1
      ans = max(ans, j - i)
    return ans
class Solution {
  public int maxDistance(int[] nums1, int[] nums2) {
    int ans = 0;
    int m = nums1.length, n = nums2.length;
    for (int i = 0; i < m; ++i) {
      int left = i, right = n - 1;
      while (left < right) {
        int mid = (left + right + 1) >> 1;
        if (nums2[mid] >= nums1[i]) {
          left = mid;
        } else {
          right = mid - 1;
        }
      }
      ans = Math.max(ans, left - i);
    }
    return ans;
  }
}
class Solution {
public:
  int maxDistance(vector<int>& nums1, vector<int>& nums2) {
    int ans = 0;
    reverse(nums2.begin(), nums2.end());
    for (int i = 0; i < nums1.size(); ++i) {
      int j = nums2.size() - (lower_bound(nums2.begin(), nums2.end(), nums1[i]) - nums2.begin()) - 1;
      ans = max(ans, j - i);
    }
    return ans;
  }
};
func maxDistance(nums1 []int, nums2 []int) int {
  ans, n := 0, len(nums2)
  for i, num := range nums1 {
    left, right := i, n-1
    for left < right {
      mid := (left + right + 1) >> 1
      if nums2[mid] >= num {
        left = mid
      } else {
        right = mid - 1
      }
    }
    if ans < left-i {
      ans = left - i
    }
  }
  return ans
}
function maxDistance(nums1: number[], nums2: number[]): number {
  let ans = 0;
  let m = nums1.length;
  let n = nums2.length;
  for (let i = 0; i < m; ++i) {
    let left = i;
    let right = n - 1;
    while (left < right) {
      const mid = (left + right + 1) >> 1;
      if (nums2[mid] >= nums1[i]) {
        left = mid;
      } else {
        right = mid - 1;
      }
    }
    ans = Math.max(ans, left - i);
  }
  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;
    for i in 0..m {
      let mut left = i;
      let mut right = n;
      while left < right {
        let mid = left + (right - left) / 2;
        if nums2[mid] >= nums1[i] {
          left = mid + 1;
        } else {
          right = mid;
        }
      }
      res = res.max((left - i - 1) as i32);
    }
    res
  }
}
/**
 * @param {number[]} nums1
 * @param {number[]} nums2
 * @return {number}
 */
var maxDistance = function (nums1, nums2) {
  let ans = 0;
  let m = nums1.length;
  let n = nums2.length;
  for (let i = 0; i < m; ++i) {
    let left = i;
    let right = n - 1;
    while (left < right) {
      const mid = (left + right + 1) >> 1;
      if (nums2[mid] >= nums1[i]) {
        left = mid;
      } else {
        right = mid - 1;
      }
    }
    ans = Math.max(ans, left - i);
  }
  return ans;
};

Solution 2

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
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;
  }
}
class Solution {
public:
  int maxDistance(vector<int>& nums1, vector<int>& nums2) {
    int m = nums1.size(), n = nums2.size();
    int ans = 0;
    for (int i = 0, j = 0; i < m; ++i) {
      while (j < n && nums1[i] <= nums2[j]) {
        ++j;
      }
      ans = max(ans, j - i - 1);
    }
    return ans;
  }
};
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;
}
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
  }
}
/**
 * @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;
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文