3738. Longest Non-Decreasing Subarray After Replacing at Most One Element Medium

@problem@discussion
#Array#Dynamic Programming



1/**
2 * [3738] Longest Non-Decreasing Subarray After Replacing at Most One Element
3 *
4 * You are given an integer array nums.
5 * You are allowed to replace at most one element in the array with any other integer value of your choice.
6 * Return the length of the longest non-decreasing <span data-keyword="subarray">subarray</span> that can be obtained after performing at most one replacement.
7 * An array is said to be non-decreasing if each element is greater than or equal to its previous one (if it exists).
8 *  
9 * <strong class="example">Example 1:
10 * <div class="example-block">
11 * Input: <span class="example-io">nums = [1,2,3,1,2]</span>
12 * Output: <span class="example-io">4</span>
13 * Explanation:
14 * Replacing nums[3] = 1 with 3 gives the array [1, 2, 3, 3, 2].
15 * The longest non-decreasing subarray is [1, 2, 3, 3], which has a length of 4.
16 * </div>
17 * <strong class="example">Example 2:
18 * <div class="example-block">
19 * Input: <span class="example-io">nums = [2,2,2,2,2]</span>
20 * Output: <span class="example-io">5</span>
21 * Explanation:
22 * All elements in nums are equal, so it is already non-decreasing and the entire nums forms a subarray of length 5.
23 * </div>
24 *  
25 * Constraints:
26 * 
27 * 	1 <= nums.length <= 10^5
28 * 	-10^9 <= nums[i] <= 10^9​​​​​​​
29 * 
30 */
31pub struct Solution {}
32
33// problem: https://leetcode.com/problems/longest-non-decreasing-subarray-after-replacing-at-most-one-element/
34// discuss: https://leetcode.com/problems/longest-non-decreasing-subarray-after-replacing-at-most-one-element/discuss/?currentPage=1&orderBy=most_votes&query=
35
36// submission codes start here
37
38impl Solution {
39    pub fn longest_subarray(nums: Vec<i32>) -> i32 {
40        0
41    }
42}
43
44// submission codes end
45
46#[cfg(test)]
47mod tests {
48    use super::*;
49
50    #[test]
51    fn test_3738() {
52    }
53}
54

Back
© 2026 bowen.ge All Rights Reserved.