2771. Longest Non-decreasing Subarray From Two Arrays Medium
1/**
2 * [2771] Longest Non-decreasing Subarray From Two Arrays
3 *
4 * You are given two 0-indexed integer arrays nums1 and nums2 of length n.
5 * 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].
6 * Your task is to maximize the length of the longest non-decreasing subarray in nums3 by choosing its values optimally.
7 * Return an integer representing the length of the longest non-decreasing subarray in nums3.
8 * Note: A subarray is a contiguous non-empty sequence of elements within an array.
9 *
10 * <strong class="example">Example 1:
11 *
12 * Input: nums1 = [2,3,1], nums2 = [1,2,1]
13 * Output: 2
14 * Explanation: One way to construct nums3 is:
15 * nums3 = [nums1[0], nums2[1], nums2[2]] => [2,2,1].
16 * The subarray starting from index 0 and ending at index 1, [2,2], forms a non-decreasing subarray of length 2.
17 * We can show that 2 is the maximum achievable length.
18 * <strong class="example">Example 2:
19 *
20 * Input: nums1 = [1,3,2,1], nums2 = [2,2,3,4]
21 * Output: 4
22 * Explanation: One way to construct nums3 is:
23 * nums3 = [nums1[0], nums2[1], nums2[2], nums2[3]] => [1,2,3,4].
24 * The entire array forms a non-decreasing subarray of length 4, making it the maximum achievable length.
25 *
26 * <strong class="example">Example 3:
27 *
28 * Input: nums1 = [1,1], nums2 = [2,2]
29 * Output: 2
30 * Explanation: One way to construct nums3 is:
31 * nums3 = [nums1[0], nums1[1]] => [1,1].
32 * The entire array forms a non-decreasing subarray of length 2, making it the maximum achievable length.
33 *
34 *
35 * Constraints:
36 *
37 * 1 <= nums1.length == nums2.length == n <= 10^5
38 * 1 <= nums1[i], nums2[i] <= 10^9
39 *
40 */
41pub struct Solution {}
42
43// problem: https://leetcode.com/problems/longest-non-decreasing-subarray-from-two-arrays/
44// discuss: https://leetcode.com/problems/longest-non-decreasing-subarray-from-two-arrays/discuss/?currentPage=1&orderBy=most_votes&query=
45
46// submission codes start here
47
48impl Solution {
49 pub fn max_non_decreasing_length(nums1: Vec<i32>, nums2: Vec<i32>) -> i32 {
50 0
51 }
52}
53
54// submission codes end
55
56#[cfg(test)]
57mod tests {
58 use super::*;
59
60 #[test]
61 fn test_2771() {
62 }
63}
64
Back
© 2025 bowen.ge All Rights Reserved.