mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-11-12 15:25:48 +08:00
43 lines
1.6 KiB
HTML
43 lines
1.6 KiB
HTML
<p>You are given an integer array <code>nums</code>.</p>
|
||
|
||
<p>You are allowed to replace <strong>at most</strong> one element in the array with any other integer value of your choice.</p>
|
||
|
||
<p>Return the length of the <strong>longest non-decreasing <span data-keyword="subarray">subarray</span></strong> that can be obtained after performing at most one replacement.</p>
|
||
|
||
<p>An array is said to be <strong>non-decreasing</strong> if each element is greater than or equal to its previous one (if it exists).</p>
|
||
|
||
<p> </p>
|
||
<p><strong class="example">Example 1:</strong></p>
|
||
|
||
<div class="example-block">
|
||
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,1,2]</span></p>
|
||
|
||
<p><strong>Output:</strong> <span class="example-io">4</span></p>
|
||
|
||
<p><strong>Explanation:</strong></p>
|
||
|
||
<p>Replacing <code>nums[3] = 1</code> with 3 gives the array [1, 2, 3, 3, 2].</p>
|
||
|
||
<p>The longest non-decreasing subarray is [1, 2, 3, 3], which has a length of 4.</p>
|
||
</div>
|
||
|
||
<p><strong class="example">Example 2:</strong></p>
|
||
|
||
<div class="example-block">
|
||
<p><strong>Input:</strong> <span class="example-io">nums = [2,2,2,2,2]</span></p>
|
||
|
||
<p><strong>Output:</strong> <span class="example-io">5</span></p>
|
||
|
||
<p><strong>Explanation:</strong></p>
|
||
|
||
<p>All elements in <code>nums</code> are equal, so it is already non-decreasing and the entire <code>nums</code> forms a subarray of length 5.</p>
|
||
</div>
|
||
|
||
<p> </p>
|
||
<p><strong>Constraints:</strong></p>
|
||
|
||
<ul>
|
||
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
|
||
<li><code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li>
|
||
</ul>
|