You are given a 0-indexed integer array nums. Your goal is to create the longest possible non-decreasing array by strategically combining adjacent elements.
Operations Available:
โข Select any contiguous subarray
โข Replace it with the sum of its elements
Example: Given array [1, 3, 5, 6], if you select subarray [3, 5], the array becomes [1, 8, 6].
Challenge: What's the maximum length of a non-decreasing array you can achieve? A non-decreasing array means each element is greater than or equal to the previous one: arr[i] >= arr[i-1].
Key Insight: You can combine elements to make them larger, but this reduces the array length. The goal is to find the optimal balance!
Input & Output
Visualization
Time & Space Complexity
We process n elements, and for each element we do a binary search over at most n lengths
We store prefix sums and DP array, both of size n
Constraints
- 1 โค nums.length โค 105
- 1 โค nums[i] โค 109
- Array contains only positive integers
- Operations can be performed any number of times