{"id":10063,"date":"2023-07-15T08:50:52","date_gmt":"2023-07-15T15:50:52","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=10063"},"modified":"2023-07-15T08:55:44","modified_gmt":"2023-07-15T15:55:44","slug":"leetcode-2771-longest-non-decreasing-subarray-from-two-arrays","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-2771-longest-non-decreasing-subarray-from-two-arrays\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2771. Longest Non-decreasing Subarray From Two Arrays"},"content":{"rendered":"\n<p>You are given two&nbsp;<strong>0-indexed<\/strong>&nbsp;integer arrays&nbsp;<code>nums1<\/code>&nbsp;and&nbsp;<code>nums2<\/code>&nbsp;of length&nbsp;<code>n<\/code>.<\/p>\n\n\n\n<p>Let&#8217;s define another&nbsp;<strong>0-indexed<\/strong>&nbsp;integer array,&nbsp;<code>nums3<\/code>, of length&nbsp;<code>n<\/code>. For each index&nbsp;<code>i<\/code>&nbsp;in the range&nbsp;<code>[0, n - 1]<\/code>, you can assign either&nbsp;<code>nums1[i]<\/code>&nbsp;or&nbsp;<code>nums2[i]<\/code>&nbsp;to&nbsp;<code>nums3[i]<\/code>.<\/p>\n\n\n\n<p>Your task is to maximize the length of the&nbsp;<strong>longest non-decreasing subarray<\/strong>&nbsp;in&nbsp;<code>nums3<\/code>&nbsp;by choosing its values optimally.<\/p>\n\n\n\n<p>Return&nbsp;<em>an integer representing the length of the&nbsp;<strong>longest non-decreasing<\/strong>&nbsp;subarray in<\/em>&nbsp;<code>nums3<\/code>.<\/p>\n\n\n\n<p><strong>Note:&nbsp;<\/strong>A&nbsp;<strong>subarray<\/strong>&nbsp;is a contiguous&nbsp;<strong>non-empty<\/strong>&nbsp;sequence of elements within an array.<\/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 = [2,3,1], nums2 = [1,2,1]\n<strong>Output:<\/strong> 2\n<strong>Explanation: <\/strong>One way to construct nums3 is: \nnums3 = [nums1[0], nums2[1], nums2[2]] =&gt; [2,2,1]. \nThe subarray starting from index 0 and ending at index 1, [2,2], forms a non-decreasing subarray of length 2. \nWe can show that 2 is the maximum achievable length.<\/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 = [1,3,2,1], nums2 = [2,2,3,4]\n<strong>Output:<\/strong> 4\n<strong>Explanation:<\/strong> One way to construct nums3 is: \nnums3 = [nums1[0], nums2[1], nums2[2], nums2[3]] =&gt; [1,2,3,4]. \nThe entire array forms a non-decreasing subarray of length 4, making it the maximum achievable length.\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 = [1,1], nums2 = [2,2]\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong> One way to construct nums3 is: \nnums3 = [nums1[0], nums1[1]] =&gt; [1,1]. \nThe entire array forms a non-decreasing subarray of length 2, making it the maximum achievable length.\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 == nums2.length == n &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>1 &lt;= nums1[i], nums2[i] &lt;= 10<sup>9<\/sup><\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: DP<\/strong><\/h2>\n\n\n\n<p>Let dp1(i), dp2(i) denote the length of the Longest Non-decreasing Subarray ends with nums1[i] and nums2[i] respectively.<\/p>\n\n\n\n<p>init: dp1(0) = dp2(0) = 1<\/p>\n\n\n\n<p>dp1(i) = max(dp1(i &#8211; 1) + 1 if nums1[i] &gt;= nums1[i &#8211; 1] else 1, dp2(i &#8211; 1) + 1 if nums1[i] &gt;= nums2[i &#8211; 1] else 1)<br>dp2(i) = max(dp1(i &#8211; 1) + 1 if nums2[i] &gt;= nums1[i &#8211; 1] else 1, dp2(i &#8211; 1) + 1 if nums2[i] &gt;= nums2[i &#8211; 1] else 1)<\/p>\n\n\n\n<p>ans = max(dp1, dp2)<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(n) -&gt; O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">Python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"python\"># Author: Huahua\nclass Solution:\n  def maxNonDecreasingLength(self, nums1: List[int], nums2: List[int]) -&gt; int:\n    n = len(nums1)\n    nums = [nums1, nums2]\n\n    @cache\n    def dp(i: int, j: int):\n      if i == 0: return 1\n      ans = 1\n      for k in range(2):\n        if nums[j][i] >= nums[k][i - 1]:\n          ans = max(ans, dp(i - 1, k) + 1)\n      return ans\n\n    return max(max(dp(i, 0), dp(i, 1)) for i in range(n))\n<\/pre>\n<\/div><\/div>\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 maxNonDecreasingLength(vector<int>& nums1, vector<int>& nums2) {\n    int ans = 1;\n    for (int i = 1, dp1 = 1, dp2 = 1; i < nums1.size(); ++i) {\n      int t1 = max(nums1[i] >= nums1[i - 1] ? dp1 + 1 : 1, \n                   nums1[i] >= nums2[i - 1] ? dp2 + 1 : 1);\n      int t2 = max(nums2[i] >= nums1[i - 1] ? dp1 + 1 : 1, \n                   nums2[i] >= nums2[i - 1] ? dp2 + 1 : 1);\n      dp1 = t1;\n      dp2 = t2;\n      ans = max({ans, dp1, dp2});\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given two&nbsp;0-indexed&nbsp;integer arrays&nbsp;nums1&nbsp;and&nbsp;nums2&nbsp;of length&nbsp;n. Let&#8217;s define another&nbsp;0-indexed&nbsp;integer array,&nbsp;nums3, of length&nbsp;n. For each index&nbsp;i&nbsp;in the range&nbsp;[0, n &#8211; 1], you can assign either&nbsp;nums1[i]&nbsp;or&nbsp;nums2[i]&nbsp;to&nbsp;nums3[i]. Your&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[20,18,177],"class_list":["post-10063","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","tag-array","tag-dp","tag-medium","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/10063","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=10063"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/10063\/revisions"}],"predecessor-version":[{"id":10066,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/10063\/revisions\/10066"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=10063"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=10063"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=10063"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}