2771. Longest Non-decreasing Subarray From Two Arrays LeetCode Solution

In this guide, you will get 2771. Longest Non-decreasing Subarray From Two Arrays LeetCode Solution with the best time and space complexity. The solution to Longest Non-decreasing Subarray From Two Arrays problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Longest Non-decreasing Subarray From Two Arrays solution in C++
  4. Longest Non-decreasing Subarray From Two Arrays solution in Java
  5. Longest Non-decreasing Subarray From Two Arrays solution in Python
  6. Additional Resources
2771. Longest Non-decreasing Subarray From Two Arrays LeetCode Solution image

Problem Statement of Longest Non-decreasing Subarray From Two Arrays

You are given two 0-indexed integer arrays nums1 and nums2 of length n.
Let’s define another 0-indexed integer array, nums3, of length n. For each index i in the range [0, n – 1], you can assign either nums1[i] or nums2[i] to nums3[i].
Your task is to maximize the length of the longest non-decreasing subarray in nums3 by choosing its values optimally.
Return an integer representing the length of the longest non-decreasing subarray in nums3.
Note: A subarray is a contiguous non-empty sequence of elements within an array.

Example 1:

Input: nums1 = [2,3,1], nums2 = [1,2,1]
Output: 2
Explanation: One way to construct nums3 is:
nums3 = [nums1[0], nums2[1], nums2[2]] => [2,2,1].
The subarray starting from index 0 and ending at index 1, [2,2], forms a non-decreasing subarray of length 2.
We can show that 2 is the maximum achievable length.
Example 2:

See also  514. Freedom Trail LeetCode Solution

Input: nums1 = [1,3,2,1], nums2 = [2,2,3,4]
Output: 4
Explanation: One way to construct nums3 is:
nums3 = [nums1[0], nums2[1], nums2[2], nums2[3]] => [1,2,3,4].
The entire array forms a non-decreasing subarray of length 4, making it the maximum achievable length.

Example 3:

Input: nums1 = [1,1], nums2 = [2,2]
Output: 2
Explanation: One way to construct nums3 is:
nums3 = [nums1[0], nums1[1]] => [1,1].
The entire array forms a non-decreasing subarray of length 2, making it the maximum achievable length.

Constraints:

1 <= nums1.length == nums2.length == n <= 105
1 <= nums1[i], nums2[i] <= 109

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(1)

2771. Longest Non-decreasing Subarray From Two Arrays LeetCode Solution in C++

class Solution {
 public:
  int maxNonDecreasingLength(vector<int>& nums1, vector<int>& nums2) {
    int ans = 1;
    int dp1 = 1;  // the longest subarray that ends in nums1[i] so far
    int dp2 = 1;  // the longest subarray that ends in nums2[i] so far

    for (int i = 1; i < nums1.size(); ++i) {
      const int dp11 = nums1[i - 1] <= nums1[i] ? dp1 + 1 : 1;
      const int dp21 = nums2[i - 1] <= nums1[i] ? dp2 + 1 : 1;
      const int dp12 = nums1[i - 1] <= nums2[i] ? dp1 + 1 : 1;
      const int dp22 = nums2[i - 1] <= nums2[i] ? dp2 + 1 : 1;
      dp1 = max(dp11, dp21);
      dp2 = max(dp12, dp22);
      ans = max({ans, dp1, dp2});
    }

    return ans;
  }
};
/* code provided by PROGIEZ */

2771. Longest Non-decreasing Subarray From Two Arrays LeetCode Solution in Java

class Solution {
  public int maxNonDecreasingLength(int[] nums1, int[] nums2) {
    int ans = 1;
    int dp1 = 1; // the longest subarray that ends in nums1[i] so far
    int dp2 = 1; // the longest subarray that ends in nums2[i] so far

    for (int i = 1; i < nums1.length; ++i) {
      final int dp11 = nums1[i - 1] <= nums1[i] ? dp1 + 1 : 1;
      final int dp21 = nums2[i - 1] <= nums1[i] ? dp2 + 1 : 1;
      final int dp12 = nums1[i - 1] <= nums2[i] ? dp1 + 1 : 1;
      final int dp22 = nums2[i - 1] <= nums2[i] ? dp2 + 1 : 1;
      dp1 = Math.max(dp11, dp21);
      dp2 = Math.max(dp12, dp22);
      ans = Math.max(ans, Math.max(dp1, dp2));
    }

    return ans;
  }
}
// code provided by PROGIEZ

2771. Longest Non-decreasing Subarray From Two Arrays LeetCode Solution in Python

class Solution:
  def maxNonDecreasingLength(self, nums1: list[int], nums2: list[int]) -> int:
    ans = 1
    dp1 = 1  # the longest subarray that ends in nums1[i] so far
    dp2 = 1  # the longest subarray that ends in nums2[i] so far

    for i in range(1, len(nums1)):
      dp11 = dp1 + 1 if nums1[i - 1] <= nums1[i] else 1
      dp21 = dp2 + 1 if nums2[i - 1] <= nums1[i] else 1
      dp12 = dp1 + 1 if nums1[i - 1] <= nums2[i] else 1
      dp22 = dp2 + 1 if nums2[i - 1] <= nums2[i] else 1
      dp1 = max(dp11, dp21)
      dp2 = max(dp12, dp22)
      ans = max(ans, dp1, dp2)

    return ans
# code by PROGIEZ

Additional Resources

See also  1648. Sell Diminishing-Valued Colored Balls LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.