Make Array Non-decreasing

You are given an integer array nums. In one operation, you can select a subarray and replace it with a single element equal to its maximum value.

Return the maximum possible size of the array after performing zero or more operations such that the resulting array is non-decreasing.

Example 1
Inputnums = [4,2,5,3,5]
Output3
One way to achieve the maximum size is to replace [2, 5] with 5 and [3, 5] with 5, producing the non-decreasing array [4, 5, 5] of size 3.
Example 2
Inputnums = [1,2,3]
Output3
No operation is needed because the array [1, 2, 3] is already non-decreasing.

Constraints

  • 1 <= nums.length <= 2 * 10^5
  • 1 <= nums[i] <= 2 * 10^5

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate