{"id":8447,"date":"2021-05-08T23:48:26","date_gmt":"2021-05-09T06:48:26","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8447"},"modified":"2021-05-08T23:49:35","modified_gmt":"2021-05-09T06:49:35","slug":"leetcode-1855-maximum-distance-between-a-pair-of-values","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/two-pointers\/leetcode-1855-maximum-distance-between-a-pair-of-values\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1855. Maximum Distance Between a Pair of Values"},"content":{"rendered":"\n<p>You are given two&nbsp;<strong>non-increasing 0-indexed&nbsp;<\/strong>integer arrays&nbsp;<code>nums1<\/code>\u200b\u200b\u200b\u200b\u200b\u200b and&nbsp;<code>nums2<\/code>\u200b\u200b\u200b\u200b\u200b\u200b.<\/p>\n\n\n\n<p>A pair of indices&nbsp;<code>(i, j)<\/code>, where&nbsp;<code>0 &lt;= i &lt; nums1.length<\/code>&nbsp;and&nbsp;<code>0 &lt;= j &lt; nums2.length<\/code>, is&nbsp;<strong>valid<\/strong>&nbsp;if both&nbsp;<code>i &lt;= j<\/code>&nbsp;and&nbsp;<code>nums1[i] &lt;= nums2[j]<\/code>. The&nbsp;<strong>distance<\/strong>&nbsp;of the pair is&nbsp;<code>j - i<\/code>\u200b\u200b\u200b\u200b.<\/p>\n\n\n\n<p>Return&nbsp;<em>the&nbsp;<strong>maximum distance<\/strong>&nbsp;of any&nbsp;<strong>valid<\/strong>&nbsp;pair&nbsp;<\/em><code>(i, j)<\/code><em>. If there are no valid pairs, return&nbsp;<\/em><code>0<\/code>.<\/p>\n\n\n\n<p>An array&nbsp;<code>arr<\/code>&nbsp;is&nbsp;<strong>non-increasing<\/strong>&nbsp;if&nbsp;<code>arr[i-1] &gt;= arr[i]<\/code>&nbsp;for every&nbsp;<code>1 &lt;= i &lt; arr.length<\/code>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums1 = [55,30,5,4,2], nums2 = [100,20,10,10,5]\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong> The valid pairs are (0,0), (2,2), (2,3), (2,4), (3,3), (3,4), and (4,4).\nThe maximum distance is 2 with pair (2,4).\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums1 = [2,2,2], nums2 = [10,10,1]\n<strong>Output:<\/strong> 1\n<strong>Explanation:<\/strong> The valid pairs are (0,0), (0,1), and (1,1).\nThe maximum distance is 1 with pair (0,1).\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums1 = [30,29,19,5], nums2 = [25,25,25,25,25]\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong> The valid pairs are (2,2), (2,3), (2,4), (3,3), and (3,4).\nThe maximum distance is 2 with pair (2,4).\n<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums1 = [5,4], nums2 = [3,2]\n<strong>Output:<\/strong> 0\n<strong>Explanation:<\/strong> There are no valid pairs, so return 0.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= nums1.length &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>1 &lt;= nums2.length &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>1 &lt;= nums1[i], nums2[j] &lt;= 10<sup>5<\/sup><\/code><\/li><li>Both&nbsp;<code>nums1<\/code>&nbsp;and&nbsp;<code>nums2<\/code>&nbsp;are&nbsp;<strong>non-increasing<\/strong>.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Two Pointers<\/strong><\/h2>\n\n\n\n<p>For each i, find the largest j such that nums[j] &gt;= nums[i].<\/p>\n\n\n\n<p>Time complexity: O(n + m)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int maxDistance(vector<int>& nums1, vector<int>& nums2) {    \n    int ans = 0;\n    for (int i = 0, j = -1; i < nums1.size(); ++i) {\n      while (j + 1 < nums2.size() &#038;&#038; nums2[j + 1] >= nums1[i]) ++j;\n      if (j >= i) ans = max(ans, j - i);\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given two&nbsp;non-increasing 0-indexed&nbsp;integer arrays&nbsp;nums1\u200b\u200b\u200b\u200b\u200b\u200b and&nbsp;nums2\u200b\u200b\u200b\u200b\u200b\u200b. A pair of indices&nbsp;(i, j), where&nbsp;0 &lt;= i &lt; nums1.length&nbsp;and&nbsp;0 &lt;= j &lt; nums2.length, is&nbsp;valid&nbsp;if both&nbsp;i &lt;= j&nbsp;and&nbsp;nums1[i]&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[176],"tags":[177,175],"class_list":["post-8447","post","type-post","status-publish","format-standard","hentry","category-two-pointers","tag-medium","tag-two-pointers","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8447","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=8447"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8447\/revisions"}],"predecessor-version":[{"id":8449,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8447\/revisions\/8449"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8447"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8447"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8447"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}