Class Solution
java.lang.Object
g2701_2800.s2771_longest_non_decreasing_subarray_from_two_arrays.Solution
2771 - Longest Non-decreasing Subarray From Two Arrays.<p>Medium</p>
<p>You are given two <strong>0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code> of length <code>n</code>.</p>
<p>Let’s define another <strong>0-indexed</strong> integer array, <code>nums3</code>, of length <code>n</code>. For each index <code>i</code> in the range <code>[0, n - 1]</code>, you can assign either <code>nums1[i]</code> or <code>nums2[i]</code> to <code>nums3[i]</code>.</p>
<p>Your task is to maximize the length of the <strong>longest non-decreasing subarray</strong> in <code>nums3</code> by choosing its values optimally.</p>
<p>Return <em>an integer representing the length of the <strong>longest non-decreasing</strong> subarray in</em> <code>nums3</code>.</p>
<p><strong>Note:</strong> A <strong>subarray</strong> is a contiguous <strong>non-empty</strong> sequence of elements within an array.</p>
<p><strong>Example 1:</strong></p>
<p><strong>Input:</strong> nums1 = [2,3,1], nums2 = [1,2,1]</p>
<p><strong>Output:</strong> 2</p>
<p><strong>Explanation:</strong></p>
<p>One way to construct nums3 is:</p>
<p>nums3 = [nums1[0], nums2[1], nums2[2]] => [2,2,1].</p>
<p>The subarray starting from index 0 and ending at index 1, [2,2], forms a non-decreasing subarray of length 2.</p>
<p>We can show that 2 is the maximum achievable length.</p>
<p><strong>Example 2:</strong></p>
<p><strong>Input:</strong> nums1 = [1,3,2,1], nums2 = [2,2,3,4]</p>
<p><strong>Output:</strong> 4</p>
<p><strong>Explanation:</strong></p>
<p>One way to construct nums3 is:</p>
<p>nums3 = [nums1[0], nums2[1], nums2[2], nums2[3]] => [1,2,3,4].</p>
<p>The entire array forms a non-decreasing subarray of length 4, making it the maximum achievable length.</p>
<p><strong>Example 3:</strong></p>
<p><strong>Input:</strong> nums1 = [1,1], nums2 = [2,2]</p>
<p><strong>Output:</strong> 2</p>
<p><strong>Explanation:</strong></p>
<p>One way to construct nums3 is:</p>
<p>nums3 = [nums1[0], nums1[1]] => [1,1].</p>
<p>The entire array forms a non-decreasing subarray of length 2, making it the maximum achievable length.</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums1.length == nums2.length == n <= 10<sup>5</sup></code></li>
<li><code>1 <= nums1[i], nums2[i] <= 10<sup>9</sup></code></li>
</ul>
-
Constructor Summary
Constructors -
Method Summary
-
Constructor Details
-
Solution
public Solution()
-
-
Method Details
-
maxNonDecreasingLength
public int maxNonDecreasingLength(int[] nums1, int[] nums2)
-