Imagine you have an integer array that's currently out of order. You have a special power: you can select any subarray (a contiguous portion) and replace it with a single element equal to the maximum value in that subarray!
Your mission is to use this operation strategically to create the longest possible non-decreasing array. You can perform this operation zero or more times.
Goal: Return the maximum possible size of the array after performing operations such that the resulting array is non-decreasing (each element โค next element).
Example: [3, 1, 5, 2, 4] โ You could replace [1, 5, 2] with 5 to get [3, 5, 4], then replace [5, 4] with 5 to get [3, 5].
Input & Output
Visualization
Time & Space Complexity
Memoization helps but still explores exponential states in worst case
Memoization table plus recursion stack
Constraints
- 1 โค nums.length โค 103
- 1 โค nums[i] โค 106
- The array contains only positive integers